About numpy operation (b)

numpy.sum

d = np.array([
    [1, 2, 1],
    [3, 0, 2]
])
print(d.shape)  # (2, 3)

axis parameter can not exceed the dimension of the array, which represents the dimension used to compress, from the following code and its operation principle is apparent

print (np.sum (d))   # 1 + 3 + 2 + 0 + 1 + 2 = 9 
print (np.sum (d, axis = 0))   # [1 + 3, 2 + 0, 1 + 2] = [4, 2, 3] 
print (np.sum (d, axis = 1))   # [1 + 2 + 1, 3 + 0 + 2] = [4, 5]

Again a three-dimensional array

c = np.array([
    [
        [2, 3, 1],
        [4, 1, 0]
    ],
    [
        [0, 3, 1],
        [0, 1, 0]
    ]
])
print(c.shape)  # (2, 2, 3)
print (np.sum (c))   # 2 + 3 + 1 + 4 + 1 + 0 + 0 + 3 + 1 + 0 + 1 + 0 = 16 
print (np.sum (c axis = 0))   # [ [2, 3, 1], [4, 1, 0]] + [[0, 3, 1], [0, 1, 0]] = [[2, 6, 2], [4, 2, 0 ]] 
print (np.sum (c axis = 1))   # [[2, 3, 1] + [4, 1, 0]] + [[0, 3, 1] + [0, 1, 0] ] = [[6, 4, 1], [0, 4, 1]] 
print (np.sum (c axis = 2))   # [[2 + 3 + 1, 4 + 1 + 0] [0 + 3 + 1, 0 + 1 + 0]] = [[6, 5], [4, 1]]

np.max, np.min, np.mean Similarly other (in Example 2-dimensional array)

print (np.max (d))   print # 3
 (np.max (d, axis = 0))   # [3, 2, 2] print (np.max (d, axis = 1))   # [2, 3 print ] (np.min (d))   # 0 print (np.min (d, axis = 0))   # [1, 0, 1] print (np.min (d, axis = 1))   # [1, 0] print (np.mean (d))   # print 1.5 (np.mean (d, axis = 0))   # [2. 1. 1.5] print (np.mean (d, axis = 1))   # [1.33333333 1.66666667]








 

Guess you like

Origin www.cnblogs.com/answerThe/p/11369030.html