# calculation along row or column index, calculate by the index of the axis which is set
print(np.sum(a, axis = 0)) #[23 25 27]
print(np.sum(a, axis = 1)) #[33 42]
很多时候可以声明axis。axis=0 or 'index',表示沿着第0轴进行操作,即对每一列进行操作, apply function to each column;axis=1 or 'columns',表示沿着第1轴进行操作,即对每一行进行操作, apply function to each row。
秩(rank):维数,一维数组的秩为1,二维数组的秩为2,以此类推。即轴的个数。
用True/False可以很容易的为np array 做masking 比如
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
a_idx1 = (a < 5)
a_idx2 = (a >=5) & (a <= 10)
print(a[a_idx1]) # return the corresponding array
print(a[a_idx2]) # return the corresponding array
print(a[(a > 5) & (a < 10)]) # single statement
a = np.array([[10, 11, 12], [13, 14, 15]])
a += np.array([-1, -2, -3]) # add [-1, -2, -3] to all columns for each row
a += np.array([[-1], [-2]]) # add [[-1], [-2]] to all rows for each column
numpy里的random,可以用来有放回抽样、无放回抽样、按照一定概率抽样、生成两个数据点范围内的random number
np.random.seed(42) # same seed can repro the same scenario so it is deterministic
print(np.random.rand(2,2)) # Random numbers between [0,1) of shape 2,2
print(np.random.randn(2,2)) # Normal distribution with mean=0 and variance=1 of shape 2,2
print(np.random.randint(0, 10, size=[2,2])) # Random integers between [0, 10) of shape 2,2
print(np.random.random()) # One random number between [0,1)
print(np.random.random(size=[2,2])) # Random numbers between [0,1) of shape 2,2
print(np.random.choice(['a', 'e', 'i', 'o', 'u'], size=10)) # Pick 10 items from a given list, with equal probability
print(np.random.choice(['a', 'e', 'i', 'o', 'u'], size=10, p=[0.3, .1, 0.1, 0.4, 0.1])) # Pick 10 items from a given list with a predefined probability 'p'
print(np.random.choice(np.arange(100),size=[10,3],replace=True)) # get 10x3 random samples from [0-99] with replacement