Numpy is used to create array in python. Arrays can be 1D ,2D,3D and so on. D stands for dimensional.
1D array– contains only list of numbers and only one row.e.g.
array([1,2,3,4])
2D array-contains list of list and more than one numbers of rows and column.e.g.2*2 matrix or array contains 2 row and 2 column matrix shown below
(array([[1, 2], [2, 3]])
3D-array-more list will be nested it will return 3D array. e.g.(2,2,2) array’s length is 2 and contains 2 rows and 2 column matrix or array.
array([[[1, 2], [2, 3]], [[3, 4], [4, 5]]])
Convert List to array in Python:
import numpy as np
a is list
a=[1,2,3,4]
np.array(a)
Output:
array([1,2,3,4])
List will be converted into 1-dimensional array.
b=np.array([[1,2],[2,3]])
b.shape
(2,2) => 2 rows and 2 columns array
Create 3D Array in Python
Similarly 3D array can be created as shown below
c=np.array([[[1,2],[2,3]],[[3,4],[4,5]]])
c.shape=> (2,2,2) means length 2 contains 2 rows and columns