numpy-2
April 19, 2025
[1]: import numpy as np
[3]: ar1 = np.arange(1,25)
ar1 #1D data
[3]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24])
[4]: ar1.ndim # number of dimensions
[4]: 1
[5]: ar1.size # number of elements in array
[5]: 24
[ ]: rows * columns = size
1*24
2*12
3*8
4*6
[6]: ar1.reshape(6,4) # 6 rows and 4 columns
[6]: array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20],
[21, 22, 23, 24]])
[8]: ar1.reshape(4,4)
# ar1 size is 24 elements but re shape requires only 16 elements , this cant be␣
↪done
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
1
Cell In[8], line 1
----> 1 ar1.reshape(4,4)
ValueError: cannot reshape array of size 24 into shape (4,4)
[10]: ar1.reshape(5,5)
# ar1 size is 24 elements but re shape requires 25 (5*5) so this cant be done
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[10], line 1
----> 1 ar1.reshape(5,5)
ValueError: cannot reshape array of size 24 into shape (5,5)
[12]: ar2 = ar1.reshape(6,4)
ar2
[12]: array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20],
[21, 22, 23, 24]])
[13]: ar2.ndim
[13]: 2
[15]: ar3 = ar1.reshape(3,2,4) # create 3 small arrays with 2 rows and 4 columns
ar3.ndim
[15]: 3
[16]: ar3
[16]: array([[[ 1, 2, 3, 4],
[ 5, 6, 7, 8]],
[[ 9, 10, 11, 12],
[13, 14, 15, 16]],
[[17, 18, 19, 20],
[21, 22, 23, 24]]])
[ ]:
2
[17]: ar2
[17]: array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20],
[21, 22, 23, 24]])
[18]: ar2.ndim
[18]: 2
[19]: ar2.size
[19]: 24
[20]: ar2.shape
[20]: (6, 4)
[21]: ar2.dtype #32/64 it doesnt matter
# int --> data consists of number
[21]: dtype('int32')
[22]: ar2.astype(float)
[22]: array([[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.],
[13., 14., 15., 16.],
[17., 18., 19., 20.],
[21., 22., 23., 24.]])
[23]: ar2
[23]: array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20],
[21, 22, 23, 24]])
[ ]:
Array Mathematics
3
[24]: np.array([[10,20],[30,40]])
[24]: array([[10, 20],
[30, 40]])
[25]: np.sum([[10,20],[30,40]]) # add every element
[25]: 100
[26]: np.sum([[10,20],[30,40]], axis = 0) # axis = 0 --> apply the function in row␣
↪level
[26]: array([40, 60])
[27]: np.sum([[10,20],[30,40]], axis = 1) # axis = 1 --> apply the functioni in␣
↪column level
[27]: array([30, 70])
[31]: np.array([[10,20,1],[30,40,2],[50,60,3]])
[31]: array([[10, 20, 1],
[30, 40, 2],
[50, 60, 3]])
[30]: np.sum([[10,20,1],[30,40,2],[50,60,3]], axis = 0) # 10+30+50, 20+40+60, 1+2+3
[30]: array([ 90, 120, 6])
[32]: np.sum([[10,20,1],[30,40,2],[50,60,3]], axis = 1) # 10+20+1, 30+40+2, 50+60+3
[32]: array([ 31, 72, 113])
[ ]: import numpy as np
[36]: # help(np.exp)
[34]: # these functions doesnt support 2D (no axis parameter) --> it will take␣
↪numbers
print(np.subtract(10,20))
print(np.multiply(10,20))
print(np.divide(10,20))
print(np.log(10))
print(np.exp(10))
print(np.sqrt(10))
print(np.sin(10))
4
-10
200
0.5
2.302585092994046
22026.465794806718
3.1622776601683795
-0.5440211108893698
[ ]:
Indexing and Slicing
[ ]: # 1. Index will always starts of zero
# 2. end is exclude
[37]: list1 = [1,2,3,4,5,6]
list1[0]
[37]: 1
[38]: list1[1:4] # 1,2,3 excluding end value (4)
[38]: [2, 3, 4]
[ ]: # slicing --> take a small portion from original
[39]: ar1 = np.arange(1,25).reshape(6,4) # 6 rows and 4 columns
ar1
[39]: array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20],
[21, 22, 23, 24]])
[41]: ar1[1:6,1:4]
[41]: array([[ 6, 7, 8],
[10, 11, 12],
[14, 15, 16],
[18, 19, 20],
[22, 23, 24]])
[42]: ar1[1:,1:] # python will consider from starting 1st row and 1st column till the␣
↪last of data
5
[42]: array([[ 6, 7, 8],
[10, 11, 12],
[14, 15, 16],
[18, 19, 20],
[22, 23, 24]])
[43]: ar1[:,1:] # by default start : 0 and end : till last
# execute all the rows and from column1 till last column
[43]: array([[ 2, 3, 4],
[ 6, 7, 8],
[10, 11, 12],
[14, 15, 16],
[18, 19, 20],
[22, 23, 24]])
[44]: ar1[1:4,:] # execute only 1,2,3 rows and all the columns
[44]: array([[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]])
[45]: ar1[1:4] # execute only 1,2,3 rows and all the columns
[45]: array([[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]])
[40]: ar1[1:5,1:3] # excluding end values
[40]: array([[ 6, 7],
[10, 11],
[14, 15],
[18, 19]])
[ ]:
Array Manipulation
[46]: ar1 = np.array([[10,20],[30,40]])
ar2 = np.array([[50,60],[70,80]])
ar2
[46]: array([[50, 60],
[70, 80]])
[57]: ar1
6
[57]: array([[10, 20],
[30, 40]])
[48]: np.concatenate([ar1,ar2], axis = 0) # combine ar1 and ar2 in row level
[48]: array([[10, 20],
[30, 40],
[50, 60],
[70, 80]])
[50]: np.vstack([ar1,ar2]) # same as np.concatenate([ar1,ar2], axis = 0)
[50]: array([[10, 20],
[30, 40],
[50, 60],
[70, 80]])
[51]: np.row_stack([ar1,ar2]) # same as np.concatenate([ar1,ar2], axis = 0)
[51]: array([[10, 20],
[30, 40],
[50, 60],
[70, 80]])
[49]: np.concatenate([ar1,ar2], axis = 1) # combine ar1 and ar2 in column level
[49]: array([[10, 20, 50, 60],
[30, 40, 70, 80]])
[52]: np.column_stack([ar1,ar2])
[52]: array([[10, 20, 50, 60],
[30, 40, 70, 80]])
[53]: np.hstack([ar1,ar2])
[53]: array([[10, 20, 50, 60],
[30, 40, 70, 80]])
[ ]:
[54]: ar3 = np.concatenate([ar1,ar2,ar1], axis = 1)
ar3
[54]: array([[10, 20, 50, 60, 10, 20],
[30, 40, 70, 80, 30, 40]])
7
[56]: np.hsplit(ar3,3)# split array in 3 equal portion
[56]: [array([[10, 20],
[30, 40]]),
array([[50, 60],
[70, 80]]),
array([[10, 20],
[30, 40]])]