Numpy (Continued)
Reshape
Reshape is when you change the number of rows and columns which gives a new view to an object.
import numpy as np
a = np.array([(8,9,10),(11,12,13)])
print(a)
b = a.reshape(6,1)
print(b)
--------------------------------------------------------------------------------------------------------------------
Slicing
Slicing is basically extracting a particular set of elements from an array.
import numpy as np
a = np.array([(8,9),(10,11),(12,13)])
print(a[0:2,1])
# 2nd element from the zeroth and first index of the array
import numpy as np
a = np.array([(1,2,3,4),(3,4,5,6)])
print(a[0:, 2])
Few more example of slicing shown below:
---------------------------------------------------------------------------------------------------------------------
linspace
returns evenly spaced numbers over a specified interval.
#It has printed 10 values between 1 to 3
import numpy as np
a=np.linspace(1,3,10)
print(a)
--------------------------------------------------------------------------------------------------------------------
max/min
import numpy as np
a= np.array([1,2,3])
print(a.min())
print(a.max())
print(a.sum())
-------------------------------------------------------------------------------------------------------------------
axis
import numpy as np
a = np.array([(1,2,3),(3,4,5)])
print(a)
print(a.sum(axis=0))
---------------------------------------------------------------------------------------------------------------------
Square Root & Standard Deviation
How much each element varies from the mean value of the python numpy array
import numpy as np
a = np.array([(1,2,3),(3,4,5)])
print(np.sqrt(a))
print(np.std(a))
--------------------------------------------------------------------------------------------------------------------
Vertical & Horizontal Stacking
a1 = np.arange(1,13)
a2 = np.arange(13,25)
print(np.stack((a1,a2)))
print(np.hstack((a1,a2)))
print(np.vstack((a1,a2)))
print(np.stack((a1,a2), axis = 1))
-------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------
ravel
convert one numpy array into a single column i.e ravel.
import numpy as np
x= np.array([(1,2,3),(1,2,3)])
print(x.ravel())
------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment