Numpy axis and numpy array conversion permutation axis

Numpy axes

insert image description here

import numpy as np
数组=np.array([[[1,2],[4,5],[7,8]],[[8,9],[11,12],[14,15]],[[10,11],[13,14],[16,17]],[[19,20],[22,23],[25,26]]])
print(数组.shape)  # 返回 (4, 3, 2)

The innermost pair of [ ] can represent a 1-dimensional array.
The bold pair of [ ] contains 3 1-dimensional arrays, that is, the
outermost pair of 2-dimensional arrays [ ] contains 3 2-dimensional arrays. The 0-axis of a 3-dimensional array
is the row, the 1-axis is the column, and the 2-axis is the depth.
The shape dimension of the array is (4,3,2), and the index of the tuple is [0,1,2]
assuming that the dimension is (2,3) , the index of the tuple is [0,1]
assuming that the dimension is (4,) The index of the tuple is [0]
You can see that the axis number and the index of the shape tuple are equivalent, so this number can be understood as high-dimensional The index of the tuple generated by nd.array.shape
We know that shape (4,3,2) represents the dimension of the array. Since the index of shape can be regarded as the axis number, then an axis is actually a dimension. The
0 axis corresponds to the highest dimension. 3-dimensional, 1-axis corresponds to 2-dimensional, 2-axis corresponds to the lowest dimension of 1-dimensional

Summary: When it comes to axes, first look at the dimensions of the array. There are as many axes as there are dimensions.

slice along axis

import numpy as np
数组=np.array([  [1,2,3] , [4,5,6] , [7,8,9]  ])
print(数组)
print(数组.shape)

The dimension of the array is (3, 3), and the index of this tuple is [0,1], which means that this 2-dimensional array has two axes: 0 axis and 1 axis

First look at the slice operation of 1 parameter:
print(array[0:2])

There is a very important concept here. :2 is the first parameter of the slice. By convention, the first parameter represents the 0-axis and the
0-axis represents 2 dimensions, so this slice is cut on the 2-dimensional dimension, also called "along 0 axis cut".

insert image description here

This 2-dimensional data is composed of 3 1-dimensional arrays. Of course, these 3 1-dimensional arrays also have index numbers [0,1,2], and [:2] means that it wants to cut 3 Index [ 0 ] and index [ 1 ] in a 1-dimensional array, so two 1-dimensional arrays ([ 1, 2, 3 ]) and ([ 4, 5, 6 ]) are obtained.

First look at the slice operation of 2 parameters:
print(array[:2,1:])

It is to cut one knife on each of the two dimensions (axis), the first parameter is the 2-dimensional (0-axis), :2 means to cut the index [ 0 ] and index [ 1 ] on the 2-dimensional (0-axis), ie ([ 1, 2, 3 ]) and ([ 4, 5, 6 ])
The second parameter of the two 1-dimensional arrays is 1-dimensional (1-axis), 1: means to cut the index on 1-dimensional (1-axis) [ 1 ] and index [ 2 ], that is, ([ 2,3 ]) for array ([ 1, 2, 3 ]) and ([ 5,6 ]) for array ([ 4, 5, 6 ])
insert image description here

How to understand the incoming axis number

import numpy as np
数组=np.arange(16).reshape((2, 2, 4))
print(数组)
print(数组.shape)

Dimensions of the array: (2, 2, 4) Tuple index (subscript): [0,1,2]

We convert it:
1 dimension (2 axis) of 3D array is 4 1D arrays, each 1D array has an index consisting of 0,1 two axis numbers [ 0,0 ] , [ 0, 1 ] , [ 1,0 ] , [ 1,1 ], the parameter passed in by the transpose method is the axis number (1, 0, 2) is to change the index order of the tuple to [ 1,0,2 ] that is Turn the one-dimensional array of array[0,1] into array[1,0]
insert image description here

numpy array to permutation axis

insert image description here

insert image description here

transpose method [row and column transposition]

import numpy as np
数组=np.arange(24).reshape((4,6))
print(数组)
print("-"*30)
print(数组.transpose())

insert image description here

swapaxes method [axis transpose]

mport numpy as np
数组=np.arange(24).reshape((4,6))
print(数组)
print("-"*30)
print(数组.swapaxes(1,0))

insert image description here

Guess you like

Origin blog.csdn.net/m0_66106755/article/details/128713022