Tutorials Exercises Get Certified Services Bootcamps Spaces Sign Up Log in
Dark mode
Dark code
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP BOOTSTRAP HOW TO W3.CSS C C++ C# REACT R JQUERY DJANGO
NumPy Tutorial ADVERTISEMENT
NumPy HOME
NumPy Intro
NumPy Getting Started
NumPy Creating Arrays
NumPy Creating Arrays
NumPy Array Indexing
NumPy Array Slicing
NumPy Data Types ❮ Previous Next ❯
NumPy Copy vs View
NumPy Array Shape
NumPy Array Reshape
NumPy Array Iterating
Create a NumPy ndarray Object
NumPy Array Join
NumPy is used to work with arrays. The array object in NumPy is called ndarray .
NumPy Array Split
NumPy Array Search We can create a NumPy ndarray object by using the array() function.
NumPy Array Sort
NumPy Array Filter
Example Get your own Python Server
NumPy Random
Random Intro import numpy as np
Data Distribution
arr = np.array([1, 2, 3, 4, 5])
Random Permutation
Seaborn Module
print(arr)
Normal Distribution
Binomial Distribution print(type(arr))
Try it Yourself »
type(): This built-in Python function tells us the type of the object passed to it. Like in above code it shows that arr is
numpy.ndarray type.
To create an ndarray , we can pass a list, tuple or any array-like object into the array() method, and it will be converted into an
ndarray :
COLOR PICKER
Example
Use a tuple to create a NumPy array:
import numpy as np
arr = np.array((1, 2, 3, 4, 5))
print(arr)
Try it Yourself »
Dimensions in Arrays
A dimension in arrays is one level of array depth (nested arrays).
nested array: are arrays that have arrays as their elements.
ADVERTISEMENT
0-D Arrays
0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.
Example ADVERTISEMENT
Create a 0-D array with value 42
import numpy as np
arr = np.array(42)
print(arr)
Try it Yourself »
1-D Arrays
An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array.
These are the most common and basic arrays.
Example
Create a 1-D array containing the values 1,2,3,4,5:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Try it Yourself »
2-D Arrays
An array that has 1-D arrays as its elements is called a 2-D array.
These are often used to represent matrix or 2nd order tensors.
NumPy has a whole sub module dedicated towards matrix operations called numpy.mat
Example
Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
Try it Yourself »
3-D arrays
An array that has 2-D arrays (matrices) as its elements is called 3-D array.
These are often used to represent a 3rd order tensor.
Example
Create a 3-D array with two 2-D arrays, both containing two arrays with the values 1,2,3 and 4,5,6:
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(arr)
Try it Yourself »
Check Number of Dimensions?
NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array have.
Example
Check how many dimensions the arrays have:
import numpy as np
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)
Try it Yourself »
Higher Dimensional Arrays
An array can have any number of dimensions.
When the array is created, you can define the number of dimensions by using the ndmin argument.
Example
Create an array with 5 dimensions and verify that it has 5 dimensions:
import numpy as np
arr = np.array([1, 2, 3, 4], ndmin=5)
print(arr)
print('number of dimensions :', arr.ndim)
Try it Yourself »
In this array the innermost dimension (5th dim) has 4 elements, the 4th dim has 1 element that is the vector, the 3rd dim has 1
element that is the matrix with the vector, the 2nd dim has 1 element that is 3D array and 1st dim has 1 element that is a 4D
array.
Test Yourself With Exercises
Exercise:
Insert the correct method for creating a NumPy array.
arr = np. ([1, 2, 3, 4, 5])
Submit Answer »
Start the Exercise
❮ Previous Log in to track progress Next ❯
ADVERTISEMENT
ADVERTISEMENT
Spaces Upgrade Newsletter Get Certified Report Error
Top Tutorials Top References Top Examples Get Certified
HTML Tutorial HTML Reference HTML Examples HTML Certificate
CSS Tutorial CSS Reference CSS Examples CSS Certificate
JavaScript Tutorial JavaScript Reference JavaScript Examples JavaScript Certificate
How To Tutorial SQL Reference How To Examples Front End Certificate
SQL Tutorial Python Reference SQL Examples SQL Certificate
Python Tutorial W3.CSS Reference Python Examples Python Certificate
W3.CSS Tutorial Bootstrap Reference W3.CSS Examples PHP Certificate
Bootstrap Tutorial PHP Reference Bootstrap Examples jQuery Certificate
PHP Tutorial HTML Colors PHP Examples Java Certificate
Java Tutorial Java Reference Java Examples C++ Certificate
C++ Tutorial Angular Reference XML Examples C# Certificate
jQuery Tutorial jQuery Reference jQuery Examples XML Certificate
FORUM | ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.
W3Schools is Powered by W3.CSS.