Working with numpy:
-
import numpy as np
-
a = np.array([0,1,2])
-
a.ndim - gives dimension of array
-
a.shape : gives the shape of the array
-
a.reshape((3,2)) new order of matrix to be formed
- changing a element of an array we reshaped is also going to change from the original since numpy package avoids copies
-
so if copy needed: c= a.reshape((3,2)).copy()
-
Contrast to general array operations:
-
a*2 multiplies each element by 2
-
alok >4
-
-
alok[alok>4] trim out using a condition for the array
-
alok.clip(0,4) clipping the data between the range with both ends taken into consideration
-
Handle non existing values:
- c= np.array([1,2,np.NAN,3])
-
np.isnan(c) return true false on the basis of the data element in array is number or not
-
c[~np.isnan(c)] remove nan’s from the array
-
np.mean(c[~np.isnan(c)]) calculate the mean of the data feed that are only numbers
-
use of dot functions for numpy speeds by almost 25 times the average naive numpy rate
-
by taking list items, numpy auto makes the best possible common datatype for those members