Song Jiaming | 16 May 2020
Disclaimer: The structure and content referred from the github repository Data-Science-Notes. I wrote this post as my summary notes and it’s not for any business purpose.
To start using numpy, we
import numpy as np
array.ndimarray.shapearray.sizenp.array(any_dimension_list, dtype=np.int32)
np.int32 is the default data typenp.zeros((shape), dtype=atype)np.ones((3,4))np.empty((shape))arr.reshape((shape))np.arange(start, end+1, step)
np.arange(0,11,2) => [0,2,4,6,8,10]np.linespace(1,10,20) => [ 1 1.47368421 1.94736842 2.42105263 2.89473684 3.36842105
3.84210526 4.31578947 4.78947368 5.26315789 5.73684211 6.21052632
6.68421053 7.15789474 7.63157895 8.10526316 8.57894737 9.05263158
9.52631579 10. ]np.title(starting_array, (x,y)
np.title([0,1],(3,2)) resulting in [[0,1,0,1],[0,1,0,1],[0,1,0,1]a = np.array([10,20,30,40]) # [10,20,30,40]
b = np.arange(4) # [0,1,2,3]
a-b, a+b, a * b (0,20,60,120)a ** n each element in a to power of nnp.sin(a), sin(x) for x in ab < 2 => [True, True, False, False]a==b element wise check of if a==b => [False, False False, False]
a.dot(b) or np.dot(a,b)min(a), max(a)| 0 | 0 | 0 |
| 1 | 1 | 1 |
np.sum(arr, axis=1) => [0,1]np.sum(arr, axis=0) => [1,1,1]np.argmax(A), np.argmin(A)np.mean(A) or np.average(A)np.median(A)np.cumsum(A)
np.diff(B)
np.nonzero(A)
np.clip(A, min, max)
A[index], starts from index 0A[row_index][col_index] or A[row_index, col_index]A[row_start:row_end][col_start:col_end]A[(0,1,2),(4,5,6)]. this will get you elements at (0,4),(1,5),(2,6)a = np.array(['a','b','c','d]), a[mask] the resulting array is ['a','c']A.flatten()np.vstack((A,B))np.hstack((A,B))np.concatenate((A,B),axis=0) 0: vertical, 1: horizontalnp.newaxisA = [1,1,1], A[np.newaxis, :] changes to [[1,1,1]]. np.newaxis at row index means it creates a new row axis, while : means all elements in AA[:, np.newaxis] change it to [[1]],[1],[1]]np.split(A, n, axis=1) Cut A to n pieces vertically (similar to row), * Unequal splitnp.array_split(A,3, axis=1)np.vsplit(A,3) == np.split(A,3, axis=0) and np.hsplit((A,2)) == np.split(A,2,axis=1)= will let every assigning point to the same object
a = [0,1,2,3], b=a, c=b then , both b and c == [0,1,2,3]a[index] = a_number: set position index of a to a_numbera[0]=11 => all a,b,c ==[11,1,2,3]print(c is a) or print(b is a) ==> Truec[1:3] =[22,33] set index 1 and 2 of c to be 22 and 33, then print(a) ==> [11,22,33,3]copy() will get an deep copy, it does not relate to the original array
a = [0,1,2,3] and let b=a.copy()a[0] = 11, b will remain to be [0,1,2,3]a = array([[0,0,0,1,1,1,2,2]), b= array([10,10,10])a+b == [[10,10,10,11,11,11,12,12,12]]np.bincount(): count of frequency of elements which are equal to the index
x=np.array([1,2,0,1,4,1]) => np.bincount(x) == [1,3,1,0,1]np.bincount(x, minlength=7) ==> [1,3,1,0,1,0,0]np.bincount() with weights:
x=np.array([1,2,0,1,4,1]), w = [0.3,0.5,0.7,0.6,0.1,-0.9]np.bincount(x, weights=w) outputs [0.7,0,0.5,0,0.1]