How To Transpose Numpy Array Examples

Numpy provides 4 methods to transpose array objects. They are rollaxis(), swapaxes(), transpose(), ndarray.T. This article will show you some examples of how to transpose a Numpy array.

1. numpy.rollaxis().

  1. When the input array is a multiple-dimensional array, then you can use this method to move the specified array axis to the specified position.
  2. Below is the method format.
    numpy.rollaxis(arr, axis, start)
  3. arr: the passed in multi-dimensional Numpy array.
  4. axis: the index of the multi-dimensional array axis to be moved. The relative position of the other axes does not change.
  5. start: the new index of the moving axis.
  6. But from Numpy version 1.11.0, you should use the method moveaxis(array, source, destination) to replace the rollaxis() method.
  7. Below is the example source code, you can see the comments for a detailed explanation.
    import numpy as np
    
    def transpose_numpy_array_rollaxis():
        
        # create the original 3 dimensional array that has 5 rows (axis 0), 2 columns(axis 1), and each element is an array that has 3 values(axis 2).
        # there are 3 axis in the array axis_0 - 5 rows, axis_1 - 2 columns, axis_2 - 3 values.
        array = np.arange(30).reshape(5,2,3)
        print('********** original array **********\r')
        print(array)
        print('\r array.ndim = ', array.ndim)
        
        
        # move the third axis (axis_2) to the second axis ( axis_1), that means swap the 2 axis.
        array_rolled_1 = np.rollaxis(array, axis = 2, start = 1)
        print('\n********** after roll the array\'s third axis to the second axis **********\r')
        # after the above swap, the array has 5 rows (axis 0), 3 columns ( axis 1), each element array has 2 values(axis 2).
        print(array_rolled_1)
        print('\r array_rolled_1.ndim = ', array_rolled_1.ndim)
        
        # move the third axis (axis_2) to the first axis ( axis_0), that means swap the 2 axis.
        array_rolled_2 = np.rollaxis(array, axis = 2, start = 0)
        print('\n********** after roll the array\'s third axis to the first axis **********\r')
        # after the above swap, the array has 3 rows (axis 0), 5 columns ( axis 1), each element array has 2 values(axis 2).
        print(array_rolled_2)
        print('\r array_rolled_2.ndim = ', array_rolled_2.ndim)
        
        
        array1 = np.arange(10).reshape(5,2)
        print(array1)
        print('\n original array = ', array1)
        # move the axis 1 to the first axis that means swap the axis 1 and axis 0.
        array1_rolled = np.moveaxis(array1, source = 1, destination = 0)
        print('\n np.moveaxis(array1, source = 1, destination = 0) = ', array1_rolled)
                                                 
    
    if __name__ == '__main__':
        
        numpy_ndarray_ravel_example()
    
  8. Below is the above example source code execution result.
    ********** original 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]]
    
     [[24 25 26]
      [27 28 29]]]
    
     array.ndim =  3
    
    ********** after roll the array's third axis to the second axis **********
    
    [[[ 0  3]
      [ 1  4]
      [ 2  5]]
    
     [[ 6  9]
      [ 7 10]
      [ 8 11]]
    
     [[12 15]
      [13 16]
      [14 17]]
    
     [[18 21]
      [19 22]
      [20 23]]
    
     [[24 27]
      [25 28]
      [26 29]]]
    
     array_rolled_1.ndim =  3
    
    ********** after roll the array's third axis to the first axis **********
    
    [[[ 0  3]
      [ 6  9]
      [12 15]
      [18 21]
      [24 27]]
    
     [[ 1  4]
      [ 7 10]
      [13 16]
      [19 22]
      [25 28]]
    
     [[ 2  5]
      [ 8 11]
      [14 17]
      [20 23]
      [26 29]]]
    
     array_rolled_2.ndim =  3
    [[0 1]
     [2 3]
     [4 5]
     [6 7]
     [8 9]]
    
     original array =  [[0 1]
     [2 3]
     [4 5]
     [6 7]
     [8 9]]
    
     np.moveaxis(array1, source = 1, destination = 0) =  [[0 2 4 6 8]
     [1 3 5 7 9]]
    

2. numpy.swapaxes().

  1. This method is used to swap two axes of an array.
  2. The method syntax is as follows.
    numpy.swapaxes(arr, axis1, axis2)
  3. Below is an example.
    import numpy as np
    
    def transpose_numpy_array_swapaxes():
        
        # create the original 2 dimensional array. 
        array = np.arange(10).reshape(5,2)
        
        print('\n source array = ', array)
        
        # swap the 2 axis of the 2 dimensional array.
        array_1 = np.swapaxes(array,1,0)
        
        print('\n np.swapaxes(array,1,0) = ', array_1)
                                             
    
    if __name__ == '__main__':
        
        transpose_numpy_array_swapaxes()
  4. Below is the example execution output.
     source array =  [[0 1]
     [2 3]
     [4 5]
     [6 7]
     [8 9]]
    
     np.swapaxes(array,1,0) =  [[0 2 4 6 8]
     [1 3 5 7 9]]
    

3. numpy.transpose().

  1. The Numpy.transpose() method is used to swap dimensions for multidimensional arrays.
  2. Below is the Numpy.transpose() method syntax.
    numpy.transpose(arr, axes)
  3. arr: the array to operate on.
  4. axes: optional parameter, tuple, or integer list, it will transpose the array according to this parameter.
  5. Below is the example source code.
    import numpy as np
    
    def transpose_numpy_array_transpose():
        
        # create the original 2 dimensional array. 
        array = np.arange(10).reshape(5,2)
        
        print('\n source array = ', array)
        
        # swap the 2 axis of the 2 dimensional array.
        array_1 = np.transpose(array)
        
        print('\n np.transpose() = ', array_1)                                             
    
    if __name__ == '__main__':
        
        transpose_numpy_array_transpose()
  6. Below is the above example execution output.
    source array =  [[0 1]
     [2 3]
     [4 5]
     [6 7]
     [8 9]]
    
     np.transpose() =  [[0 2 4 6 8]
     [1 3 5 7 9]]

4. ndarray.T.

  1. This attribute is similar to the method numpy.transpose().
    import numpy as np
    
    def transpose_numpy_array_t():
        
        # create the original 2 dimensional array. 
        array = np.arange(10).reshape(5,2)
        
        print('\n source array = ', array)
        
        # swap the 2 axis of the 2 dimensional array.
        array_1 = array.T
        
        print('\n array.T = ', array_1)                                                 
    
    if __name__ == '__main__':
        
        transpose_numpy_array_t()
  2. Below is the above example execution output.
    source array =  [[0 1]
     [2 3]
     [4 5]
     [6 7]
     [8 9]]
    
     array.T =  [[0 2 4 6 8]
     [1 3 5 7 9]]

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.