numpy - Jupyter Notebook http://localhost:8888/notebooks/numpy/numpy.ip
import
In [2]: import numpy as np
1D array
In [45]: a1 = np.array([1,2,3]) # 1 2 3 1D array 1 row and 3 columns shape=(1,3)
type(a1)
Out[45]: numpy.ndarray
In [7]: a1.shape
Out[7]: (3,)
2D array
In [46]: a2= np.array([[1,1,1],[2,2,2]]) # 1 1 1
# 2 2 2 2rows and 3 columns shape = (2,3
a2.shape
Out[46]: (2, 3)
3D array
In [29]: a3 = np.array([[[1,1,1], # This is how it looks
[2,2,2], # 4 4 4 # back 2D
[3,3,3]], # 5 5 5
# 6 6 6
[[4,4,4], # 1 1 1
[5,5,5], # 2 2 2
[6,6,6] # 3 3 3 # front 2D
]])
a3.shape
Out[29]: (2, 3, 3)
1 of 18 1/6/2023, 5:24
,numpy - Jupyter Notebook http://localhost:8888/notebooks/numpy/numpy.ip
In [10]: a3.dtype
Out[10]: dtype('int32')
to check the number of dimensions
In [11]: a1.ndim,a2.ndim,a3.ndim
Out[11]: (1, 2, 3)
to get the size of the array (number of
elements)
In [12]: a1.size,a2.size,a3.size
Out[12]: (3, 6, 18)
converting an arrray to a DF
In [13]: import pandas as pd
df= pd.DataFrame(a2)
In [14]: df
Out[14]: 0 1 2
0 1 1 1
1 2 2 2
creating an arrays
zeros/ones
2 of 18 1/6/2023, 5:24
,numpy - Jupyter Notebook http://localhost:8888/notebooks/numpy/numpy.ip
In [15]: zeros =np.zeros((2,4))
zeros
Out[15]: array([[0., 0., 0., 0.],
[0., 0., 0., 0.]])
In [16]: zeros.shape
Out[16]: (2, 4)
In [17]: type(zeros
)
Out[17]: numpy.ndarray
In [18]: zeros.dtype
Out[18]: dtype('float64')
In [19]: ones=np.ones((2,4))
ones
Out[19]: array([[1., 1., 1., 1.],
[1., 1., 1., 1.]])
In [20]: ones.size
Out[20]: 8
Creating an array with a cerain range
In [21]: array_range = np.arange(0,10,2) # creates an array from 0 to 10(not included )
array_range
Out[21]: array([0, 2, 4, 6, 8])
Creating a random array of a given shape
In [3]: random_array= np.random.randint(0,10,size=(2,4)) # creates a 2D array of shpe
random_array
Out[3]: array([[3, 5, 3, 2],
[4, 7, 2, 0]])
3 of 18 1/6/2023, 5:24
import
In [2]: import numpy as np
1D array
In [45]: a1 = np.array([1,2,3]) # 1 2 3 1D array 1 row and 3 columns shape=(1,3)
type(a1)
Out[45]: numpy.ndarray
In [7]: a1.shape
Out[7]: (3,)
2D array
In [46]: a2= np.array([[1,1,1],[2,2,2]]) # 1 1 1
# 2 2 2 2rows and 3 columns shape = (2,3
a2.shape
Out[46]: (2, 3)
3D array
In [29]: a3 = np.array([[[1,1,1], # This is how it looks
[2,2,2], # 4 4 4 # back 2D
[3,3,3]], # 5 5 5
# 6 6 6
[[4,4,4], # 1 1 1
[5,5,5], # 2 2 2
[6,6,6] # 3 3 3 # front 2D
]])
a3.shape
Out[29]: (2, 3, 3)
1 of 18 1/6/2023, 5:24
,numpy - Jupyter Notebook http://localhost:8888/notebooks/numpy/numpy.ip
In [10]: a3.dtype
Out[10]: dtype('int32')
to check the number of dimensions
In [11]: a1.ndim,a2.ndim,a3.ndim
Out[11]: (1, 2, 3)
to get the size of the array (number of
elements)
In [12]: a1.size,a2.size,a3.size
Out[12]: (3, 6, 18)
converting an arrray to a DF
In [13]: import pandas as pd
df= pd.DataFrame(a2)
In [14]: df
Out[14]: 0 1 2
0 1 1 1
1 2 2 2
creating an arrays
zeros/ones
2 of 18 1/6/2023, 5:24
,numpy - Jupyter Notebook http://localhost:8888/notebooks/numpy/numpy.ip
In [15]: zeros =np.zeros((2,4))
zeros
Out[15]: array([[0., 0., 0., 0.],
[0., 0., 0., 0.]])
In [16]: zeros.shape
Out[16]: (2, 4)
In [17]: type(zeros
)
Out[17]: numpy.ndarray
In [18]: zeros.dtype
Out[18]: dtype('float64')
In [19]: ones=np.ones((2,4))
ones
Out[19]: array([[1., 1., 1., 1.],
[1., 1., 1., 1.]])
In [20]: ones.size
Out[20]: 8
Creating an array with a cerain range
In [21]: array_range = np.arange(0,10,2) # creates an array from 0 to 10(not included )
array_range
Out[21]: array([0, 2, 4, 6, 8])
Creating a random array of a given shape
In [3]: random_array= np.random.randint(0,10,size=(2,4)) # creates a 2D array of shpe
random_array
Out[3]: array([[3, 5, 3, 2],
[4, 7, 2, 0]])
3 of 18 1/6/2023, 5:24