Python in Array.sum (axis=?) Use of

Source: Internet
Author: User

According to the results of the operation in the code, mainly by the following:

1. SUM (): The result of adding each element in an array

2. Axis corresponds to the addition of the dimensions.

Like what:

1, when the axis=0, to drink is the first dimension elements of the addition,

[[0,1,2,3],[4,5,6,7]] and [[1,2,3,4],[5,6,7,8]] corresponding elements are added [[0+4,1+2,2+3,3+4],[4+5,5+6,7+7,7+8]]=[[1,3,5,7],[9,11,14,16] ]


2, when the Axis=1, the corresponding is the second dimension element addition, this time retains the first dimension's structure (number of first dimension element),

The number of the first dimension element is 2. respectively is

[[0,1,2,3],[4,5,6,7]] and [[1,2,3,4],[5,6,7,8]]

Structure unchanged, continue to split down, can be
(1) [0,1,2,3] and [4,5,6,7], the corresponding elements are added, merged into an array, [4,6,8,10]
(2) [1,2,3,4] and [5,6,7,8], the corresponding elements are added, merged into an array, 6,8,10,12]

3, when axis=2, because the element is three-dimensional, this is the last dimension, the smallest unit of the array elements can be added.

[0+1+2+3,4+5+6+7],[[1+2+3+4],[5+6+7+8]]=[[6,22],[10,26]]


Python code:

Import NumPy as NP
data=np.array ([[[[[0,1,2,3],[4,5,6,7]],[[1,2,3,4],[5,6,7,8]]])
sum=data.sum ()
sum0= Data.sum (axis=0)
sum1=data.sum (Axis=1)
sum2=data.sum (axis=2)
print "sum:", sum
print "axis=0:", SUM0
print "Axis=1:", sum1
print "axis=2:", sum2

Results:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.