NumPy the transpose method Detailed

In the previous array transpose and change into the shaft, the shaft is used to change the time on the books transpose this method, and an array of inexplicable change, but people do not understand at all. So I went to Baidu for a long time usage of transpose function. The following summarizes the experience.

At first I thought transpose method simply put x and y each element of the three-dimensional array of grass-roots following swap, but later found wrong outrageous.

Note: The following four graphs have a little error, we have made a detailed explanation, careful look.

In NumPy documentation, go to numpy.transpose some explanation, his role is to change the sequence, the following are some of the documents of chestnuts:

x = np.arange(4).reshape((2,2))

#输出
array([[0, 1],
       [2, 3]])
import numpy as np
x.transpose()

#输出
array([[0, 2],
       [1, 3]])

For two-dimensional ndarray, transpose default when no argument is a matrix transpose. If setting out the parameters, there will be corresponding results as follows:

x.transpose((0,1))

#结果
# x 没有变化
array([[0, 1],
       [2, 3]])
x.transpose((1,0))


#结果
# x 转置了
array([[0, 2],
       [1, 3]])

To facilitate understanding

The first to write an expression like this

x[0][0] == 0
x[0][1] == 1
x[1][0] == 2
x[1][1] == 3

And position x can be seen that each of the correspondence between the array.

A first set square brackets "[]" is 0 second shaft axis 1 square brackets, can be established in a coordinate system 0-1.

Because it is to find online map, this picture is wrong, position 1 and 2 should be a change.

è¿ is ?? ???? ???? å å æ ?? ???? ???? ¾ç è¿ °

You can clearly see the location of each element of the base layer.

Since x.transpose ((0,1)) represents change sequence according to the original coordinate axes, i.e. remains unchanged, so that the same final result

And x.transpose ((1,0)) represents the exchange 'axis 0' and 'shaft', so to obtain the results shown below:

The figure also, 2 and 1 should be a change

è¿ is ?? ???? ???? å å æ ?? ???? ???? ¾ç è¿ °

No matter what, remember first bracketed 0 axis is the second axis is in square brackets, so you do not mess relationship transpose the very clear.

Difficulties come. Transpose dimensional array.

import numpy as np

# A是array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])
A = np.arange(16)

# 将A变换为三维矩阵
A = A.reshape(2,2,4)
print(A)


#结果
A = array([[[ 0,  1,  2,  3],
            [ 4,  5,  6,  7]],

           [[ 8,  9, 10, 11],
            [12, 13, 14, 15]]])

We A of the above-described three-dimensional coordinates of the form as follows:

FIG 0,1,2,3 written backwards, should 3,2,1,0 (top-down)

è¿ is ?? ???? ???? å å æ ?? ???? ???? ¾ç è¿ °

Then following the transition will be very good understanding.

A.transpose((0,1,2))  #保持A不变
A.transpose((1,0,2))  #将 0轴 和 1轴 交换

FIG 0,1,2,3 written backwards, should 3,2,1,0 (top-down)

The switching shaft 1 and the axis 0

è¿ is ?? ???? ???? å å æ ?? ???? ???? ¾ç è¿ °

This time the transpose had been easy to understand.

Refer to the original link: https://blog.csdn.net/u012762410/article/details/78912667

Guess you like

Origin www.cnblogs.com/chanyuli/p/11762419.html