Axis in Numpy is a very confusing concept for beginners. This article hopes to give you an intuitive understanding of Axis by means of pictures and pictures.

## The combination of numbers and shapes is good in every way

When the number lacks form, it is less intuitive, and when the form is few, it is difficult to be subtle. The combination of numbers and shapes is good, and all things rest in isolation. – hua luogeng

Axis itself means “axis”. It actually represents the axis on which the sum is to be summed, so when using the sum function, it is necessary to have a spatial concept, so it is very easy to understand the result of the calculation.

### A two-dimensional

So when we say [[0, 4, 2], [-2, 5, 3], we’re really saying:

A two-dimensional array corresponds to a two-dimensional table with two axes, axis 0 for rows and Axis 1 for columns.

When specifying axis 0 to sum, we do the following:

That is, sum along axis 0 to get a one-dimensional array:

``````>>> np.sum([[0.4.2], [2 -.5.3]], axis=0)
array([2 -.9.5])
Copy the code``````

Similarly, when specifying Axis 1, it looks like this:

That is:

``````>>> np.sum([[0.4.2], [2 -.5.3]], axis=1)
array([6.6])
Copy the code``````

### The three dimensional

What about going up to three dimensions? In fact, it’s the same thing. Let’s create a three-dimensional array:

``````>>> array_3d = [
.    [
.            [1.2]..            [3.4]
.]..    [
.            [5.6]..            [7.8]
.]..    [
.            [9.10]..            [11.12]
.    ]
.]
Copy the code``````

Then you have a three-dimensional space in your mind!

The sum of Axis 0, in fact, is the sum along axis 0, and the final result is a two-dimensional array:

That is:

``````>>> np.sum(array_3d, axis=0)
array([[15.18],
[21.24]])
Copy the code``````

The same is true for Axis 1 and Axis 2. Here is axis 2 as an example. You can try to draw your own axis 1:

``````>>> np.sum(array_3d, axis=2)
array([[ 3.7],
[11.15],
[19.23]])
Copy the code``````

### Four dimensions to do

What about four dimensions? Four dimensions are really hard to draw, but the purpose of this article is to get an intuitive understanding of what the calculation is going to be, so I’m not going to draw them. Hahaha

## The determination of the shaft

All of this, of course, assumes that we have the correct orientation of the axes. The key question is, how do you know which is Axis 0 and which is Axis 1?

In fact, the order of the axes can be determined according to the nesting relationship, and the order of the axes is the outer to the inside of the parentheses:

But if you think about it this way, you don’t have a lot of understanding of shape, so it’s a little bit harder to understand, so look at Python · Numpy · Axis.

## reference

• THE PARAMETERS OF NUMPY SUM
• NUMPY AXES EXPLAINED
• docs.scipy.org/doc/numpy
• Python · numpy · axis