Lively understand numpy.sum() and its axis parameters

1. numpy.sum() method

When using the NumPy module, the numpy.sum() method is often used, such as calculating the sum of all elements of a multidimensional array (ndarray):

The calculation principle of the sum() method is Sum of array elements over a given axis. Sum the elements  on the specified dimension axis and return An array with the same shape as  a (Note: a is the input array of the sum function) , with the specified axis removed. The shape of the returned result is similar to the input, but the axis dimension is missing.

So, if the dimension of the input array a is 3 and the shape is (2,3,5), the array shape returned by numpy.sum(a, axis=0) is (3,5), numpy.sum(a, axis= 1) The returned array shape is (2,5), and the array shape returned by numpy.sum(a, axis=2) is (2,3).

Going back to my question just now, to calculate the sum of the elements of each column of a two-dimensional array, which dimension does the "column" correspond to? axis=0 or axis=1?

arr is an array with 2 rows and 3 columns. I want to calculate the sum of the elements in each column. Obviously the shape of the returned result should be (3,), so axis=0!

Another parameter in sum() is keepdims, and the default value is False. If we want the number of dimensions of the returned result (ndim) to be the same as the input, we can set keepdimes to True, and the result of sum() will become: the shape of the returned result is similar to the input, except that the dimension value of the axis is 1.

 

2. The meaning of axis in numpy

NumPy uses ndarray to represent multi-dimensional arrays. Multi-dimensional arrays, as the name suggests, are arrays with multiple dimensions. For example , a vector has only one dimension, it is a one-dimensional array, and a matrix has two dimensions, and it is a two-dimensional array. An array with more than three dimensions is mathematically called a tensor .

Each dimension corresponds to a coordinate axis. For example, the plane rectangular coordinate system is two-dimensional and has two coordinate axes .

The value range of axis corresponds to the dimension of the ndarray object: axis=0 for one-dimensional arrays, axis=0, 1 for two-dimensional arrays, the higher the dimension, the larger the value of axis can be. When the array is n-dimensional, axis=0 ,1,...,n-1 , the dimension number 0 represents the dimension of the outermost [] of the ndarray object. The larger the number, the more the dimension of the inner [] of the ndarray object .

axis = 0 means to divide according to the number of elements (shape) of the dimension of the outermost layer [], and perform operations between blocks. At the same time, if the keepdims parameter is False (default), the outermost layer is also removed [] ;
Axis = 1 means to divide the number of elements (shape) of the dimension of the second outer layer [], and perform operations between blocks. At the same time, if the keepdims parameter is False (default), remove the second outer layer [ ];
axis = 2, 3, 4, 5... The same is true for the analysis.

 

3. Give a more similar chestnut

In order to make the abstract thinking concrete, I simulated the area distribution map of a developer's real estate based on the distribution of houses in the community that I am familiar with in life, to assist my thinking:

3.1 . Each house in the figure has its own area (unit: square meter), and the area data of each house is stored in a scalar form in a 4-dimensional numpy.array object with shape=(5,4,3,2);

3.2. The four dimensions of axis3 , 2, 1, and 0 respectively represent room number, floor number, unit number, and district number;

3.3. Obviously, according to the structure of the cell, the status of these four dimensions are not equal, and there is a hierarchical relationship between them. In fact, this is exactly the same as the data structure of the numpy.array object . So, if you like, you can also assume that a city has several such communities, then a province has several such cities, a country has several such provinces, and a planet has several such countries... .What, ban dolls? Oh, yes, the dimensions of numpy.array are in the nesting dolls. The dimensions in it are not the same as the length, width and height;

3.4. Let's interpret the meaning of operations under different axis parameters (please pay attention to how the numbers in the white grid change):

3.4.1 If calculated by sum(axis=0), it means that according to the principle that the number of units, the number of floors, and the room number are the same (that is , the indexes of the three dimensions of axis3, axis2, and axis1 are the same), the number of different periods The total area of ​​the house becomes a new scalar:

As shown in the figure above, the meaning of the scalar has also changed. These new scalars form a new array with three dimensions of shape=(4,3,2) . Because the summary is the topmost dimension axis0 , there is only one new array generated . It replaced the original top-level array, became a big brother, and jumped out of the pit ;

3.4.2 If calculated by sum (axis=1), it means that according to the principle that the number of floors and room numbers are the same (that is, the indexes of the two dimensions of axis3 and axis2 are the same) , different units in the districts of the respective phases The total area of ​​the house becomes a new scalar:

As shown in the figure above, the meaning of the scalar has also changed. These new scalars form a new array with two dimensions of shape=(3,2) . Because the aggregated dimension is axis1, and the upper layer has dimension axis0 with size=5 , there are 5 new arrays generated . They cut off their immediate bosses with shape=(4,3,2), and became middle-level leaders with shape=(3,2). But unfortunately, there is still a big brother-level dimension named axis0 with size=5 in the upper level, so they are honestly squatting in the 5 pits of axis0 ;

3.4.3 If calculated by sum(axis=2), it means that according to the principle that the house numbers are all the same (that is , the index of one dimension of axis3 is the same), the area of ​​houses of different floors in each unit is aggregated into a new one Scalar:

As shown in the figure above, the meaning of the scalar has also changed. These new scalars form a new array with one dimension of shape=(2) .

Because the dimension of the summary is axis2, the upper layer also has the dimension axis1 of size=4 and the dimension axis0 of size=5 , so there are 5x4 new arrays generated , and they have cut off their respective shape=(3,2) direct lines. Boss, he became a small leader of shape=(2). But unfortunately they have the dimension of axis1 with size=4 in the upper layer, and the dimension of axis0 with size=5 in the upper layer has not been cut, so they squat in the 5x4 pit formed by axis0 and axis1 ;

3.4.4 If calculated by sum(axis=3), it means that the area of ​​houses with different house numbers in each number of floors is aggregated into a new scalar:

As shown in the figure above, the meaning of the scalar has also changed. The bottom-level array axis3 with dimension 1 can only become a scalar after dimensionality reduction summary .

Because the aggregated dimension is axis3, the upper layer also has the dimension axis2 with size=3, the dimension axis1 with size=4, and the dimension axis0 with size=5 , so there are 5x4x3 new scalars generated , which have their respective shape= (2)'s immediate boss, but Goose still has no subordinates himself, and is still a scalar brother. And they have dimension axis2 with size=3 in the upper layer, axis1 with size=4 in the upper layer, and dimension axis0 with size=5 in the upper layer, so they squat on the 5x4x3 formed by axis0 and axis1 and axis2. Can't turn over in the pit.

 

4. Summary:

Do you see any pattern from the above? First of all, it is a hierarchical structure. The status of each dimension in the numpy.array object is not equal. If you consider the calculation of different axis parameters in the way of thinking about equal status, then I guess your head will be blown up. There is a hierarchical relationship between the various dimensions in the numpy.array object. This is an important way of thinking. Because of the hierarchical relationship between total and points, the transformation of many dimensions conforms to the way of thinking of the human brain. This is in line with the tree data structure and divide and conquer. Algorithms and other ideas coincide.

Secondly, when a value is selected for the parameter axis (assuming d), then the numpy.array.sum() method will only specify k (assuming k is the number of elements in dimension d) elements in the specified d dimension (it can be Scalar, matrix, or tensor) are combined and summed, and the structure of higher dimensions (that is, dimensions 0, 1, ..., d-1) will not change .

As for the operations between elements, if the elements are scalars, would you be able to sum them? This is a skill in the first grade of elementary school. What if the matrices are summed? Wouldn't please refer to the undergraduate linear algebra textbook. Does tensor summation? Don't be kidding, if you know how to sum matrices, you know how to sum tensors. If not, then look back at the above example, or find a few more examples yourself.

Except for the sum function, functions such as argmin in numpy that contain axis parameters work in the same way.

 

5. Reference materials:

https://numpy.org/doc/stable/reference/generated/numpy.sum.html#numpy.sum

https://zhuanlan.zhihu.com/p/48871067

https://blog.csdn.net/sky_kkk/article/details/79725646

Guess you like

Origin blog.csdn.net/yocencyy/article/details/113548315