The understanding of axis in matrix operations, and the explanation of axis=-1

Before reading this article, please understand the concept of channels in three-dimensional arrays ( (36 messages) [numpy] argmax parameter analysis (axis=0, axis=1, axis=-1)_Hu Kanyoumaterial blog-CSDN blog_ axis=1 )

1. Two-dimensional understanding axis

axis two-dimensional

coordinate

Two-dimensional data has two axes: the 0th axis (axis=0) goes vertically down the row, and the 1st axis (axis=1 or axis=-1) extends horizontally along the column.

Maybe it is simple to remember that axis=0 means cross-row (down), and axis=1 means cross-column (across)

2. High-dimensional understanding axis

Explain three-dimensional here, and higher dimensions can be understood

Set axis=i, then operate along the direction of the i-th subscript change!

embeddings is a matrix of shape=[3,4,5]

3D matrix

Take the subscript of the maximum value of the matrix according to the axis (0, 1, -1)

Take the subscript of the maximum value according to the axis

The result is as follows

result

Do you understand why this is the result? Now to explain from the beginning

First mark the coordinates of embedding

coordinate

Take axis=0 as an example, operate along the direction of the change of the 0th subscript (the leftmost subscript), that is to say, except for the 0th axis, the coordinates of the 1st axis and the 2nd axis must be the same to operate together.

Coordinates are grouped by axis=0

axis=0 means to change along the 0th axis (the coordinates of the 0th axis are 0, 1, 2), and the coordinates of other axes are grouped together. For example, a000, a100, and a200 are a group (because the coordinate values ​​of the three other axes are all 00); a001, a101, a201 are a group, and a024, a124, a224 are a group. In that case, it is divided into 4*5 groups. So operate according to axis=0, the resulting matrix size is [4,5].

Coordinates are grouped by axis=1

axis=1, that is, change along the first axis (the coordinates of the first axis are 0, 1, 2, 3), and the coordinates of other axes are grouped into one group. For example, a000, a010, a020, a030 are a group; a103, a113, a123, a133 are a group. In that case, it is divided into 3*5 groups. So operate according to axis=1, the resulting matrix size is [3,5].

Coordinates are grouped by axis=2

axis=-1 means axis=2, which is to change along the second axis (the coordinate values ​​of the second axis are 0, 1, 2, 3, 4), and the coordinates of other axes are grouped into one group. For example, a000, a001, a002, a003, a004 are a group; a120, a121, a122, a123, a124 are a group. In that case, it is divided into 3*4 groups. So operate according to axis=-1, the resulting matrix size is [3,4].

So the core principles are:

1. Group first and then operate.

2. When grouping, the coordinate value of the axis=i axis changes, and the coordinate values ​​of other axes are the same, so they are divided into one group.

Guess you like

Origin blog.csdn.net/qq_42514371/article/details/123705554