Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the meaning of the axis parameter when performing sum calculation in numpy multidimensional array

Shulou Source: shulou.com Published: 2022-06-02 07:33:54 09月26日 Update

This article mainly introduces "what is the meaning of axis parameters when numpy multi-dimensional array performs sum calculation". In daily operations, I believe that many people have doubts about the meaning of axis parameters when numpy multi-dimensional arrays perform sum calculation. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "what is the meaning of axis parameters when numpy multi-dimensional arrays perform sum calculation?" Next, please follow the editor to study!

The axis parameter, which ostensibly means the number axis, is interpreted on the official website as "Axis or axes along which a sum is performed. The axis along which the summation is performed."

I think saying it is the same as not saying it, how can you follow the summation method?

For a two-digit array, we can simply write down that axis=0 is the sum of columns and axis=1 is the sum of rows.

What about more dimensional arrays? What if axis is a tuple?

Look at the code:

Import numpy as npn = np.array ([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[2, 4, 6], [8, 10, 12], [14, 16, 18], [[1, 3, 5], [7, 9, 11], [13, 15] 17]]) print (n) print ("= sum axis=None=") sum = 0for i in range (3): for j in range (3): for k in range (3): sum + = n [k] [I] [j] print (sum) # 216print ('-') print (np.sum (n)) # 216print ("= sum axis=0 =") for i in range (3): for J in range (3): sum = 0 for axis in range (3): sum + = n [axis] [I] [j] print (sum End='') print () print ('-') print ("sum [0] [0] =% d"% (n [0] [0] [0] + n [1] [0] [0] + n [2] [0] [0] [0])) print ("sum [1] [1] =% d"% (n [0] [1] [1] + n [1] [1]) [1] + n [2] [1] [1]) print ("sum [2] [2] =% d"% (n [0] [2] [2] + n [1] [2] [2] + n [2] [2] [2])) print ('-') print (np.sum (n) Axis=0) print ("= sum axis=1 =") for i in range (3): for j in range (3): sum = 0 for axis in range (3): sum + = n [I] [axis] [j] print (sum End='') print () print ('-') print ("sum [0] [0] =% d"% (n [0] [0] [0] [0] + n [0] [1] [0] + n [0] [2] [0])) print ("sum [1] [1] =% d"% (n [1] [0] [1] + n [1] [1]) [1] + n [1] [2] [1]) print ("sum [2] [2] =% d"% (n [2] [0] [2] + n [2] [1] [2] + n [2] [2] [2]])) print ('-') print (np.sum (n) Axis=1) print ("= sum axis=2 =") for i in range (3): for j in range (3): sum = 0 for axis in range (3): sum + = n [I] [j] [axis] print (sum End='') print () print ('-') print ("sum [0] [0] =% d"% (n [0] [0] [0] [0] + n [0] [0] [1] + n [0] [0] [2])) print ("sum [1] [1] =% d"% (n [1] [1] [0] + n [1] [1] [1]) [1] + n [1] [1] [2]) print ("sum [2] [2] =% d"% (n [2] [2] [0] + n [2] [2] [1] + n [2] [2] [2]])) print ('-') print (np.sum (n) Axis=2)) print ("= sum axis= (0Power1)) =") for i in range (3): sum = 0 for axis1 in range (3): for axis2 in range (3): sum + = n [axis1] [axis2] [I] print (sum) End='') print () print ('-') print ("sum [1] =% d"% (n [0] [0] [0] [1] + n [0] [1] [1] + n [0] [2] [1] + n [1] [0] [0] [1] + n [1] [1] [1] + n [1] [2] [1] +) N [2] [0] [1] + n [2] [1] [1] + n [2] [2] [2] [1]) print ('-') print (np.sum (n) Axis= (0Pol 1)

Output:

[12 3] [4 56] [7 8 9]] [[24 6] [8 10 12] [14 16 18]] [[13 5] [7 9 11] [13 15 17] = sum axis=None=216-216 = sum axis=0 = 4 9 14 19 24 29 34 39 44-sum [0] = 4sum [1] [1] = 24sum [2] [2] = 44-[[49 14] [19 24 29] [34 39 44]] = sum axis=1 = 12 15 18 24 30 36 21 27 33-sum [0] [0] = 12sum [1] [1] = 30sum [2] [2] = 33- -[[12 15 18] [24 30 36] [21 27 33]] = sum axis=2 = 6 15 24 12 30 48 9 27 45-sum [0] = 6sum [1] [1] = 30sum [2] [2] = 45-[6 15 24] [12 30 48] [9 27 45] = sum Axis= (0Jing 1) = 57 72 87-sum [1] = 72-[57 72 87]

If you understand the code and output, I think you already understand.

Finally, describe it in terms of poor language ability:

The original array is N-dimensional, and the axis specified by axis will be reduced by several dimensions. The remaining dimension array is the result array. The value of each element of the result array = the result coordinate is fixed, and the dimension specified by axis is traversed and summed.

For example, a 5-dimensional array n [I] [j] [k] [x] [y] axis = (1Magne3) then the resulting array is three-dimensional, its element r [I] [k] [y] = iPersonality is fixed, and the sum of jPerry x ergodic

At this point, the study on "what is the meaning of axis parameters when numpy multidimensional array performs sum calculation" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Arrays parameters meaning multidimensions results learning more dimensions code elements that is axes help output utility and then digits coordinates situation meaning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Apple Shulou Technology Microsoft Huawei