Deep Dive Numpy Package in Python
SIMANTA JYOTI MEDHI
Software development is my passion . A full stack developer love to upgrade myself with dynamic world.
Numpy is a fundamental package for scientific computing with python.
It mainly contains
- Powerful N dimensional array object
- We can create both vector as well as matrix
- Sophisticated functions
- Useful linear algebra, Fourier transform and random number capabilities
Installation Command
pip install numpy
In [2]:
import numpy as np
In [6]:
list =[1,2,3] array_ex= np.array(list) print(array_ex) # Print array print(array_ex.ndim) # check dimension of this array [1 2 3] 1
In [11]:
list_of_list=[[1,2,3],[4,5,6],[7,8,9]] array_list_of_list = np.array(list_of_list) print(array_list_of_list) # Print array print(array_list_of_list.ndim) # Check dimension of the array [[1 2 3] [4 5 6] [7 8 9]] 2
In [12]:
np.arange(0,10) #Print range of Values (Start, Stop+1)
Out[12]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [13]:
np.arange(0,23,5) #Print range of Values (Start, Stop+1, Step)
Out[13]:
array([ 0, 5, 10, 15, 20])
In [16]:
np.zeros(100, dtype=int) #Generate Zeroes for data manipulation
Out[16]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
In [18]:
np.ones((4,5),dtype=int) # Generate One for data manipulation
Out[18]:
array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]])
linspace
linspace function returns evenly space numbers over a specified Interval
In [20]:
np.linspace(0,20,10, dtype=int)
Out[20]:
array([ 0, 2, 4, 6, 8, 11, 13, 15, 17, 20])
Identity Matrix
Its a 2d array with diagonal value as 1 and others 0
In [4]:
np.eye(5, dtype=int)
Out[4]:
array([[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]])
Rand
Rand function creates an array of given shape and populates it with random variable derives from uniform distribution between 0 and 1
In [23]:
np.random.rand(3,2) #uses random logic on a Uniform distributed values
Out[23]:
array([[0.54270628, 0.08408696], [0.9829652 , 0.07977181], [0.0665784 , 0.95932648]])
In [5]:
a =np.random.randn(2,3) #uses random logic on a normal distributed values
In [29]:
np.random.randint(1,20,10) #Start, Stop, Number of Integers Required #Note in this example 1 is inclusive but 20 is not and with 10 random integer
Out[29]:
array([10, 19, 16, 15, 8, 17, 10, 16, 18, 13])
Vector 1 Dimensional array
In [44]:
vector_array = np.arange(30) vector_array
Out[44]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29])
Matrix
In [33]:
#Arange array reshape to 5 rows and 6 coluns array_arange= np.arange(30).reshape(5,6) array_arange
Out[33]:
array([[ 0, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29]])
In [35]:
#Randint array reshape to 5 rows and 4 columns array_randint = np.random.randint(0,100,20).reshape(5,4) array_randint
Out[35]:
array([[51, 13, 56, 6], [87, 26, 16, 58], [10, 85, 41, 95], [28, 97, 10, 34], [99, 72, 25, 97]])
In [36]:
#RandInt maximum values array_randint.max()
Out[36]:
99
In [37]:
#Randint minimum Values array_randint.min()
Out[37]:
6
In [38]:
#Position or index of maximum element array_randint.argmax()
Out[38]:
16
In [40]:
#To check the dimension of the array array_randint.shape
Out[40]:
(5, 4)
In [42]:
#To check the data Type of the array print(array_randint.dtype) int32
In [6]:
a
Out[6]:
array([[ 0.49509865, 0.61459719, 0.28281204], [-0.63663716, 1.29077901, 1.54169348]])
In [7]:
a.T
Out[7]:
array([[ 0.49509865, -0.63663716], [ 0.61459719, 1.29077901], [ 0.28281204, 1.54169348]])
In [8]:
sample_array = np.arange(10,21)
In [13]:
sample_array
Out[13]:
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
In [10]:
#Value select with particular Index sample_array[8]
Out[10]:
18
In [11]:
#Select range of Values sample_array[2: 5]
Out[11]:
array([12, 13, 14])
In [12]:
# Select particular index mutiple values sample_array[[2,5]]
Out[12]:
array([12, 15])
In [17]:
#Broadcasting the value sample_array[1]=100 sample_array
Out[17]:
array([ 10, 100, 12, 13, 14, 15, 16, 17, 18, 19, 20])
In [27]:
sample_array = np.arange(10,21) sample_array
Out[27]:
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
In [30]:
#SubSet view of original array subset_sample_array=sample_array[0:7] subset_sample_array
Out[30]:
array([10, 11, 12, 13, 14, 15, 16])
In [31]:
#Broadcast all values subset_sample_array[:] = 1001 subset_sample_array
Out[31]:
array([1001, 1001, 1001, 1001, 1001, 1001, 1001])
In [32]:
sample_array
Out[32]:
array([1001, 1001, 1001, 1001, 1001, 1001, 1001, 17, 18, 19, 20])
In [33]:
#Create copy of sample array copy_sample_array = sample_array.copy()
In [40]:
#Broadcasting Values copy_sample_array[:] = 1002 copy_sample_array
Out[40]:
array([1002, 1002, 1002, 1002, 1002, 1002, 1002, 1002, 1002, 1002, 1002])
In [38]:
#Broadcasted value is not coming sample array sample_array
Out[38]:
array([1001, 1001, 1001, 1001, 1001, 1001, 1001, 17, 18, 19, 20])
Matrix Index
In [41]:
# Regradless of how many rows or columns datatype will be same sample_matrix= np.array(([23,1, 78],[34,47,89],[78,5,90])) sample_matrix
Out[41]:
array([[23, 1, 78], [34, 47, 89], [78, 5, 90]])
In [43]:
#Selecting Values in a matrix print(sample_matrix[0][2]) print(sample_matrix[0,2]) 78 78
In [45]:
#Slicing of a matrix sample_matrix[2,:]
Out[45]:
array([78, 5, 90])
In [46]:
#Fancy Index or Custom Index of a Matrix fancy_sample_matrix= np.array(([23,1,59, 78],[34,47,29,89],[78,5,60,90],[34,70,29,40])) fancy_sample_matrix
Out[46]:
array([[23, 1, 59, 78], [34, 47, 29, 89], [78, 5, 60, 90], [34, 70, 29, 40]])
In [48]:
#Select second and fourth column fancy_sample_matrix[:,(1,3)]
Out[48]:
array([[ 1, 78], [47, 89], [ 5, 90], [70, 40]])
In [49]:
fancy_sample_matrix[:,(3,1)]
Out[49]:
array([[78, 1], [89, 47], [90, 5], [40, 70]])
Selection Techniques
In [50]:
selection_array_sample= np.arange(1,31) selection_array_sample
Out[50]:
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, 25, 26, 27, 28, 29, 30])
In [51]:
selection_array_sample<15
Out[51]:
array([ True, True, True, True, True, True, True, True, True, True, True, True, True, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False])
In [52]:
#Filter out and array selection_array_sample[selection_array_sample<15]
Out[52]:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
In [53]:
#Sum of vector array selection_array_sample+ selection_array_sample
Out[53]:
array([ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60])
In [54]:
#Minus of vector array selection_array_sample - selection_array_sample
Out[54]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
In [55]:
#Divide of vector array selection_array_sample/selection_array_sample
Out[55]:
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
In [56]:
# Multiplication of vector array selection_array_sample * selection_array_sample
Out[56]:
array([ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900])
Universal Function
In [58]:
np.sqrt(selection_array_sample)
Out[58]:
array([1. , 1.41421356, 1.73205081, 2. , 2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. , 3.16227766, 3.31662479, 3.46410162, 3.60555128, 3.74165739, 3.87298335, 4. , 4.12310563, 4.24264069, 4.35889894, 4.47213595, 4.58257569, 4.69041576, 4.79583152, 4.89897949, 5. , 5.09901951, 5.19615242, 5.29150262, 5.38516481, 5.47722558])
In [59]:
np.exp(selection_array_sample)
Out[59]:
array([2.71828183e+00, 7.38905610e+00, 2.00855369e+01, 5.45981500e+01, 1.48413159e+02, 4.03428793e+02, 1.09663316e+03, 2.98095799e+03, 8.10308393e+03, 2.20264658e+04, 5.98741417e+04, 1.62754791e+05, 4.42413392e+05, 1.20260428e+06, 3.26901737e+06, 8.88611052e+06, 2.41549528e+07, 6.56599691e+07, 1.78482301e+08, 4.85165195e+08, 1.31881573e+09, 3.58491285e+09, 9.74480345e+09, 2.64891221e+10, 7.20048993e+10, 1.95729609e+11, 5.32048241e+11, 1.44625706e+12, 3.93133430e+12, 1.06864746e+13])
In [60]:
np.log(selection_array_sample)
Out[60]:
array([0. , 0.69314718, 1.09861229, 1.38629436, 1.60943791, 1.79175947, 1.94591015, 2.07944154, 2.19722458, 2.30258509, 2.39789527, 2.48490665, 2.56494936, 2.63905733, 2.7080502 , 2.77258872, 2.83321334, 2.89037176, 2.94443898, 2.99573227, 3.04452244, 3.09104245, 3.13549422, 3.17805383, 3.21887582, 3.25809654, 3.29583687, 3.33220451, 3.36729583, 3.40119738])
In [61]:
np.max(selection_array_sample)
Out[61]:
30
In [62]:
np.min(selection_array_sample)
Out[62]:
1
In [63]:
np.argmax(selection_array_sample)
Out[63]:
29
In [64]:
np.argmin(selection_array_sample)
Out[64]:
0
In [65]:
np.sin(selection_array_sample)
Out[65]:
array([ 0.84147098, 0.90929743, 0.14112001, -0.7568025 , -0.95892427, -0.2794155 , 0.6569866 , 0.98935825, 0.41211849, -0.54402111, -0.99999021, -0.53657292, 0.42016704, 0.99060736, 0.65028784, -0.28790332, -0.96139749, -0.75098725, 0.14987721, 0.91294525, 0.83665564, -0.00885131, -0.8462204 , -0.90557836, -0.13235175, 0.76255845, 0.95637593, 0.27090579, -0.66363388, -0.98803162])
In [66]:
np.cos(selection_array_sample)
Out[66]:
array([ 0.54030231, -0.41614684, -0.9899925 , -0.65364362, 0.28366219, 0.96017029, 0.75390225, -0.14550003, -0.91113026, -0.83907153, 0.0044257 , 0.84385396, 0.90744678, 0.13673722, -0.75968791, -0.95765948, -0.27516334, 0.66031671, 0.98870462, 0.40808206, -0.54772926, -0.99996083, -0.53283302, 0.42417901, 0.99120281, 0.64691932, -0.29213881, -0.96260587, -0.74805753, 0.15425145])
In [67]:
np.tan(selection_array_sample)
Out[67]:
array([ 1.55740772e+00, -2.18503986e+00, -1.42546543e-01, 1.15782128e+00, -3.38051501e+00, -2.91006191e-01, 8.71447983e-01, -6.79971146e+00, -4.52315659e-01, 6.48360827e-01, -2.25950846e+02, -6.35859929e-01, 4.63021133e-01, 7.24460662e+00, -8.55993401e-01, 3.00632242e-01, 3.49391565e+00, -1.13731371e+00, 1.51589471e-01, 2.23716094e+00, -1.52749853e+00, 8.85165604e-03, 1.58815308e+00, -2.13489670e+00, -1.33526407e-01, 1.17875355e+00, -3.27370380e+00, -2.81429605e-01, 8.87142844e-01, -6.40533120e+00])
In [68]:
np.square(selection_array_sample)
Out[68]:
array([ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900], dtype=int32)
In [72]:
array = np.random.randn(6,6) np.round(array, decimals=2)
Out[72]:
array([[-0.83, -0.87, 0.78, -0.22, 0.26, 0.74], [-1.25, 1.97, 0.9 , -0.71, 0.58, -0.31], [ 0.31, -0.61, 0.54, 1.09, -1.34, 0.42], [ 0.9 , 0.6 , 0.47, 0.28, -1. , 0.95], [ 1.83, 1.09, -1.59, -2.58, 1.63, 0.53], [-0.15, 0.47, 1.11, -1.52, -0.4 , -0.03]])
In [77]:
np.round(np.std(selection_array_sample))
Out[77]:
9.0
In [75]:
np.round(np.var(selection_array_sample))
Out[75]:
75.0
In [78]:
np.mean(array)
Out[78]:
0.11160870639084781
In [79]:
sports= np.array(['golf','cric','fball','cric','Cric']) np.unique(sports)
Out[79]:
array(['Cric', 'cric', 'fball', 'golf'], dtype='<U5')
Save in directory
In [80]:
np.save("Array_Sample",selection_array_sample)
In [82]:
save_multiple_array = np.arange(1,20) save_multiple_array
Out[82]:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
In [83]:
np.savez("2_arrays.npz",a=selection_array_sample, b=save_multiple_array)
In [89]:
#Save as text file np.savetxt("array-text_file.txt",selection_array_sample, delimiter=",")
Load save data to perform operations
In [84]:
np.load("Array_Sample.npy")
Out[84]:
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, 25, 26, 27, 28, 29, 30])
In [85]:
archive= np.load("2_arrays.npz")
In [88]:
#Retrieve data using saving variable name archive['a']
Out[88]:
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, 25, 26, 27, 28, 29, 30])
In [87]:
archive['b']
Out[87]:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
In [90]:
np.loadtxt("array-text_file.txt")
Out[90]:
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., 25., 26., 27., 28., 29., 30.])