Python - sum in Numpy

When doing calculations today, it is necessary to calculate the sum of all elements in an array. There are many np.sum() on the Internet, but they all need to set the rows and columns for step-by-step calculations.

In fact, you can directly use np.sum() to perform calculations

The test code is as follows:

import numpy as np
a = [[1,2,3] ,[4, 5, 6], [7, 8, 9]]
b = np.sum(a,axis=0)
c = np.sum(b,axis=0)
aa = np.sum(a)
print(aa,b,c)

result:

45 [12 15 18] 45

It can be seen that the results of direct operation and step-by-step calculation are the same

Guess you like

Origin blog.csdn.net/kakangel/article/details/84579807