numpy.transpose() in Python

The numpy.transpose() function is one of the most important functions in matrix multiplication. This function permutes or reserves the dimension of the given array and returns the modified array.

The numpy.transpose() function changes the row elements into column elements and the column elements into row elements. The output of this function is a modified array of the original one.

Syntax

snippet
numpy.transpose(arr, axis=None)

Parameters

arr: array_like

It is an ndarray. It is the source array whose elements we want to transpose. This parameter is essential and plays a vital role in numpy.transpose() function.

axis: List of ints()

If we didn't specify the axis, then by default, it reverses the dimensions otherwise permute the axis according to the given values.

Return

This function returns a ndarray. The output array is the source array, with its axis permuted. A view is returned whenever possible.

Example 1: numpy.transpose()

snippet
import numpy as np
a= np.arange(6).reshape((2,3))
a
b=np.transpose(a)
b

Output:

Output
array([[0, 1, 2], [3, 4, 5]]) array([[0, 3], [1, 4], [2, 5]])

In the above code

  • We have imported numpy with alias name np.
  • We have created an array 'a' using np.arange() function and gave a shape using reshape() function.
  • We have declared the variable 'b' and assigned the returned value of np.transpose() function.
  • We have passed the array 'a' in the function.
  • Lastly, we tried to print the value of b.

In the output, the transposed array of the original array has been shown.

Example 2: numpy.transpose() with axis

snippet
import numpy as np
a= np.array([[1, 2], [4, 5], [7, 8]])
a
b=np.transpose(a, (1,0))
b

Output:

Output
array([[1, 2], [4, 5], [7, 8]]) array([[1, 4, 7], [2, 5, 8]])

In the above code

  • We have imported numpy with alias name np.
  • We have created an array 'a' using np.array() function.
  • We have declared the variable 'b' and assigned the returned value of np.transpose() function.
  • We have passed the array 'a' and the axis in the function.
  • Lastly, we tried to print the value of b.

In the output, the transposed array of the original array has been shown.

Example 3: Reposition elements using numpy.transpose()

snippet
import numpy as np
a=np.ones((12,32,123,64))
b=np.transpose(a,(1,3,0,2)).shape
b
c=np.transpose(a,(0,3,1,2)).shape
c

Output:

Output
(32L, 64L, 12L, 123L) (12L, 64L, 32L, 123L)
  • We have imported numpy with alias name np.
  • We have created an array 'a' using np.ones() function.
  • We have declared the variable 'b' and 'c' and assigned the returned value of np.transpose() function.
  • We have passed the array 'a' and the positions of the array elements in the function.
  • Lastly, we tried to print the value of b and c.

In the output, an array has been shown whose elements are located at the defined position in the array.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents