NUMPY

Functions or Methods Description
np.array(object, dtype=None, copy=True, order=’K’, subok=False, ndmin=0) Creating a new numpy array, Order: C – row major, F- Column major
ndarray[3, 4] equivalent to ndarray[3][4]
ndarray.ndim Dimension of array, (rows, columns)
ndarray.itemsize Size of each element datatype
ndarray.dtype Data type of elements
ndarray.size Number of elements in the array
ndarray.shape (rows, columns)
ndarray.reshape(row, column) Change the current dimension to given dimensions
ndarray.sum(axis=0) Total sum if axis not given else 0 – column wise, 1 – row wise
ndarray.ravel() Converting ndarray to one dimensional array
ndarray.dot(ndarray2) Dot product of two matrix
ndarray.T Transpose of ndarray matrix
ndarray.max() / min() return max/min value of array
ndarray.argmax() / argmin() return index of max / min value in array
arr_copy = ndarray.copy() copy array to another variable
# arr2 = ndarray[0:n] => it doesnt copy part of ndarray and put in arr2, but just reference, so any change in arr2 will reflect in ndarray
np.linespace(start, end, values) Equally separated numbers from start to end, values is no of elements required
np.logspace(start, end, values) Equally separated log(numbers) from start to end
np.arange(start, stop, steps) like range method, ‘steps’ separated values from start to stop
np.zeros((rows, columns)) Zero matrix, all elements are 0, inside is a tuple
np.ones((rows, columns)) Ones matrix (all elements are one)
np.eye(rows, column) Identity matrix, has diagonal of 1
np.empty((rows, column)) Matrix with random numbers
np.std(ndarray) Standard deviation of each element of ndarray
np.nditer(ndarray) Iterate through each element of ndarray
np.sort(ndarray, axis=-1, kind=’quicksort’, order=None) Sorting numpy array
np.sqrt(ndarray)
np.exp(ndarray)
np.max(ndarray)
np.sin(ndarray)
basic operations
np.random.rand(row, col) 2D matrics of row*col random numbers from 0 to 1, can pass single arg for 1D array
np.random.randn(2)
# can be randn(row, col)
2 numbers equally distant from 0 => (0.1748905, -0.917962014)