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.

 

 

 

list to array conversion in python
1.List to array in Python
To create 2D array in python add one more list with separated by comma and enclose them with square brackets as shown below 2D array will be generated and shape of array can be checked through .shape method.

b=np.array([[1,2],[2,3]])

b.shape

(2,2) => 2 rows and 2 columns array

create 2D array in python
2.Create 2D array in python

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

Create 3D array in python
3.Create 3D array in Python

 

By SC

Leave a Reply

Your email address will not be published. Required fields are marked *