NumPy is used to work with arrays. The array object in NumPy is called ndarray. We can create a NumPy ndarray object by using the array() function. In code below; we are taking list as an argument to a function array() and converting it into ndarray.
import numpy as np
list=[1,2,3,4,5]
arr=np.array(list)
print(arr)
print(type(arr))
We can also take multiple lists as an argument to the function array().
import numpy as np
list1=[1,2,3,4,5]
list2=[6,7,8,9,10]
list3=[11,12,13,14,15]
arr=np.array([list1,list2,list3])
print(arr)
print(type(arr))
Here, type() is an built-in Python function tells us the type of the object passed to it. 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
.
Lets consider an code of “tuple” as an input.
import numpy as np
tuple= (1,2,3,4,5)
arr = np.array(tuple)
print(arr)
print(type(arr))
Now, lets take “set” as an input to the array() function.
import numpy as np
set = {1,2,3,4,5}
arr = np.array(set)
print(arr)
print(type(arr))
We can also take “dictionaries” as an input to the array() function.
import numpy as np
dic = {"Name":"Nirmal", "Surname": "Gaud"}
arr = np.array(dic)
print(arr)
print(type(arr))
In all the cases; its mandatory to take elements type homogeneous in nature.
Dimension in Numpy Arrays
A dimension in arrays is one level of array depth (nested arrays). Nested array are arrays that have arrays as their elements.
0-D Arrays: 0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array. Lets create a 0-D array with value 10.
import numpy as np
arr = np.array(10)
print(arr)
1-D Arrays: An array that has 0-D arrays as its elements is called one-dimensional or 1-D array. These are the most common and basic arrays. Create a 1-D array containing the values 6,7,8,9,10.
import numpy as np
list=[6,7,8,9,10]
arr = np.array(list)
print(arr)
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
. Create a 2-D array containing two arrays with the list1 and list2.
import numpy as np
list1= [1,2,3]
list2=[4,5,6] arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
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. Create a 3-D array with two 2-D arrays, both containing two arrays with the list1 and list2.
import numpy as np
list1=[1,2,3]
list2=[4,5,6]
arr = np.array([[list1, list2], [list1, list2]])
print(arr)
Check Number of Dimensions?
NumPy Arrays provides the “ndim”
attribute that returns an integer that tells us 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("Dimension of a is:", a.ndim)
print("Dimension of b is:", b.ndim)
print("Dimension of c is:", c.ndim)
print("Dimension of d is:", d.ndim)
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. 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)
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. Lets consider one more code.
import numpy as np
arr = np.array([[1, 2, 3, 4], [5,6,7,8]], ndmin=5)
print(arr)
print('number of dimensions :', arr.ndim)