Troubleshoot transpose in Numpy

1. 1D and 2D data

.T is equivalent to .transopse

2. 3D and more dimensional data

For the transformation of the z-axis and the x-axis

In [40]: arr = np.arange(16).reshape((2, 2, 4)) In [41]: arr Out[41]: array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7]], [[ 8, 9, 10, 11], [12, 13, 14, 15]]]) In [42]: arr.transpose((1, 0, 2)) Out[42]: array([[[ 0, 1, 2, 3], [ 8, 9, 10, 11]], [[ 4, 5, 6, 7], [12, 13, 14, 15]]])

The transformation of transpose is based on shape

The shape before conversion is (0, 1, 2)

[[(0,0,0), (0,0,1), (0,0,2), (0,0,3)] // [[[ 0, 1, 2, 3], 
[(0,1,0), (0,1,1), (0,1,2), (0,1,3)], // [ 4, 5, 6, 7]], 
[(1,0,0), (1,0,1), (1,0,2), (1,0,3)] // [[ 8, 9, 10, 11], 
[(1,1,0), (1,1,1), (1,1,2), (1,1,3)]]. //[12, 13, 14, 15]]]

The converted shape is (1, 0, 2), that is, the shape on the z-axis and the x-axis is swapped

[[(0,0,0), (0,0,1), (0,0,2), (0,0,3)] 
(1,0,0), (1,0,1), (1,0,2), (1,0,3)], 
[(0,1,0), (0,1,1), (0,1,2), (0,1,3)] 
[(1,1,0), (1,1,1), (1,1,2), (1,1,3)]]

Fill in the value corresponding to the shape before conversion to get

[1,2,3,4]
[8,9,10,11]
[4,5,6,7]
[12,13,14,15]

so perfect just corresponds to the output

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325536764&siteId=291194637