Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of axis parameters of numpy function

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "axis parameter instance analysis of numpy function". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Let's talk about the conclusion first.

Let the input matrix of numpy.sum be a. The return matrix of numpy.sum is rst.

Then the shape of the matrix an is: sp=numpy.shape (a), for example, sp= [m ~ ~ n ~ p, Q]

The shape of rst is to set the axis element of sp to 1, that is:

Sp'=spsp' [axis] = 1numpy.shape (rst) = = sp' is true.

For example: axis=2

If an is a matrix:

The shape of rst should be: [mrecom nphio 1, Q]

For the element rst of rst, the calculation method is as follows:

[note axis] the subscript can only be 1.

The internal calculation of numpy.sum (a recording axisym2) is actually:

For i in range (sp [axis]): rst [m'] [n'] [1] [Q'] [] + = a [m'] [n'] [I] [Q'] []

As a result, it is found that the axis dimension becomes 1, and the calculation process actually traverses the axis to merge the sp [axis] elements into one element.

If an is an array, then:

The shape of rst should be: [mmae nje Q,]

Notice that the axis dimension is directly missing.

The Internal Computation of numpy.sum (a recording axisym2)

For i in range (sp [axis]): rst [m'] [n'] [q'] [] + = a [m'] [n'] [I] [q'] []

It turns out that the axis dimension is gone, and the calculation process actually traverses the axis so that the sp [axis] elements are merged into one element.

An example is given to illustrate the simpler import numpy as npa=np.mat ([[1mem2pd3], [4p5jp6]))

Shape of a:

Print (np.shape (a))

Output: (2, 3)

Calculation: np.sum (a recording axisym0)

> s0=np.sum (a recording axisym0) > s0matrix ([[5,7,9]])

According to the method of [first conclusion]:

Axis=0

The shape of a: (2p3)

So the shape of rst is: (1)

For each element of rst, pmaeq:

The calculation method of rst [p] [Q] is (where p can only be equal to 0, 0, 0, 1, 2):

For i in range (np.shape (a) [axis]): rst [0] [Q] + = a [I] [Q]

So:

Rst [0] [0] = a [0] [0] + a [1] [0] = 1+4=5rst [0] [1] = a [0] [1] [1] = 2+5=7rst [0] [2] = a [0] [2] + a [1] [2] = 3'6'9

So rst is [5, 7, 7, 9].

Calculate numpy.sum (a _

A = [1, 2, 3], [4, 5, 5, 6]

> s1=np.sum (a recording axisymmetric 1) > s1matrix ([[6], [15]]) > np.shape (S1) (2,1) >

The same analysis method:

According to the method of [first conclusion]:

Axis=1

The shape of a: (2p3)

So the shape of rst is: (2)

For each element of rst, pmaeq:

The calculation method of rst [p] [Q] is as follows (where pendant 0d1 and Q can only be 0):

For i in range (np.shape (a) [axis]): rst [p] [0] + = a [p] [I]

So:

Rst [0] [0] = a [0] [0] + a [0] [1] + a [0] [2] = 1+2+3=6rst [1] [0] = a [1] [1] + a [1] [1] + a [1] [2] = 4'5'6'15

So rst is [[6], [15]].

More complicated: > b=np.array ([1pje 2je 3], [4je 5pje 6], [7pr 8je 9]]) > > barray ([1rect 2pr 3], [4mei 5pr 6], [7pr 8je 9]) > > np.shape (b) (1,3,3)

B is 1x3x3, is an array.

So how much is np.sum (bjournal axisym2)?

Standard answer:

> print (np.sum (bmae axisym2) [[6 15 24]]

Analysis results:

The return value should be array in the shape of 1x3, for element RST [p] [Q].

Rst [p] [Q] = a [p] [Q] [0] + a [p] [Q] [1] + a [p] [Q] [2]

For example, rst [0] [1] = a [0] [1] [0] + a [0] [1] [1] + a [0] [1] [2] = 8, 5, 6, 15.

And the second element in the first line of np.sum is 15. 5.

About axis default valu

Generally speaking, such functions for matrix and array all have an axis parameter, and this defaults to None. When axis is None so that the representation operation is to traverse each element of the array, it is calculated element by element.

Supplement: understanding of axis parameters of some functions in python

To sum up in one sentence:

If axis=i is set, the numpy operates in the direction in which the I subscript changes.

Of course, this I is counted from 0, and as a programmer, you can't be mistaken.

Axis means "axis", which specifies the direction in which the function operates on the tensor (matrix, etc.).

For example, there is a ndarray called A. shape3 (3, 8, 5, 7).

Then the shape of the result calculated by np.sum (A, axis=2) is (3, 8, 7).

Assuming that this shape is the ndarray variable named B, which is (3p8p7), then in fact:

B [I] [j] [k] = A [I] [j] [0] [k] + A [I] [j] [1] [k] + A [I] [j] [2] [k] + A [I] [j] [3] [k] + A [I] [j] [4] [k]

You can try the following code by yourself: import numpy as npA=np.random.randn (3, 8, 5, 7) print ("A. shape2, A.shape) B=np.sum (" B.shape2) print ("B. shape2, B.shape).

The expected output is:

A.Shape = (3, 8, 5, 7)

B.Shape = (3,8,7)

This is the end of the content of "instance Analysis of axis parameters of numpy function". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report