The method of using the transpose function in numpy

two-dimensional matrix transpose function:

  I do not know how to start, directly on dry goods.

  TRANSPOSE () simply, equivalent mathematical transpose of the matrix, transpose the rows and columns is to exchange positions with each other;

  For example: a randomly generated three rows and five columns two-dimensional matrix:

  

arr = np.arange(15).reshape((3, 5))   
arr       
array([[ 0,  1,  2,  3,  4],          
           [ 5,  6,  7,  8,  9],   
           [10, 11, 12, 13, 14]])
>> arr.T
array([[ 0,  5, 10],
       [ 1,  6, 11],
       [ 2,  7, 12],  
       [ 3,  8, 13], 
       [ 4,  9, 14]])

  Action reshape randomly generated a matrix of rows and columns;

  0 represents the position of elements of 0; 1 indicates a first position, and so forth; a total number of 15;

  Then arr.T equivalent transpose of a matrix;

  transpose transpose (X, Y) is a matrix function and meaning equivalent to the behavior of the X-axis, Y-axis as, the X-axis and Y-axis exchange position;

  X-axis is represented by 0, Y axis denoted by 1;

  For example: if the transport (1,0) represents the position of rows and columns swapped;

  

>> arr.transpose(1, 0)    
  array([[ 0,  5, 10],        
            [ 1,  6, 11],         
            [ 2,  7, 12],      
            [ 3,  8, 13],        
            [ 4,  9, 14]])

 

 

Three-dimensional tensor transpose function:

  Earlier we talked about the two-dimensional matrix transpose function is actually transposed matrix and is a concept; now we are speaking about the three-dimensional tensor;

     A three-dimensional tensor name suggests, it has three dimensions; the equivalent of X axis, Y axis, Z axis; interconversion among the three shafts;

    Similarly, X-axis is represented by 0, Y axis denoted by 1; Z-axis is represented by 2;

   

arr = np.arange(24).reshape((2, 3, 4))  
 arr   
 array([[[ 0,  1,  2,  3],         
            [ 4,  5,  6,  7],         
            [ 8,  9, 10, 11]],         
           [[12, 13, 14, 15],        
            [16, 17, 18, 19],        
            [20, 21, 22, 23]]])

     

   Corresponding to the three-dimensional tensor axis transformation do, do the following figure:

    

   And between each axis conversion represented also vary:

   transpose (1,0,2) represents a transformation after the occurrence of X and Y axes;

import numpy as np
arr = np.arange(24).reshape((2,3,4))
vc = arr.transpose(1,0,2)
print(vc)
>>>结果
[[[ 0  1  2  3]
  [12 13 14 15]]

 [[ 4  5  6  7]
  [16 17 18 19]]

 [[ 8  9 10 11]
  [20 21 22 23]]]

   transport (0,2,1): Y-axis represents the Z axis after axis conversion occurs;

import numpy as np
arr = np.arange(24).reshape((2,3,4))
vc = arr.transpose(0,2,1)
print(vc)
[[[ 0  4  8]
  [ 1  5  9]
  [ 2  6 10]
  [ 3  7 11]]

 [[12 16 20]
  [13 17 21]
  [14 18 22]
  [15 19 23]]]

 

   transport (2,1,0): indicates the Z axis after the X-axis axis conversion occurs;

import numpy as np
arr = np.arange(24).reshape((2,3,4))
vc = arr.transpose(2,1,0)
print(vc)
[[[ 0 12]
  [ 4 16]
  [ 8 20]]

 [[ 1 13]
  [ 5 17]
  [ 9 21]]

 [[ 2 14]
  [ 6 18]
  [10 22]]

 [[ 3 15]
  [ 7 19]
  [11 23]]]

   Well, here, about the same transport function is also more comprehensive understanding of, and go write code!

   

 

    

  

Guess you like

Origin www.cnblogs.com/caizhou520/p/11227986.html