Python

Matrix Transpose Using Numpy 

In this post, we see how matrix transpose operation can be performed using NumPy. The transpose operation is an operation on a matrix such that it flips the matrix over the diagonal. The matrix transpose on a 2-D array of dimension n * m produces an output matrix of dimension m * n.

$ python3

Python 3.8.5 (default, Mar  8 2021, 13:02:45)

[GCC 9.3.0] on linux2

Type “help”, “copyright”, “credits” or “license” for more information.

>>> import numpy as np

>>> a = np.array([[1, 2, 3],

...           [4, 5, 6]])

>>> a.shape

(2, 3)

>>> c = a.transpose()

>>> c

array([[1, 4],

           [2, 5],

           [3, 6]])

>>> c.shape

(3, 2)

A matrix transpose on a 1-D array has no effect since the transpose is the same as the original array.

>>> a = np.ones(3)

>>> a

array([1., 1., 1.])

>>> a.shape

(3,)

>>> a_transpose = a.transpose() # transpose of 1-D array

>>> a_transpose

array([1., 1., 1.])

>>> a_transpose.shape

(3,)

To convert a 1-D array to its transpose as a 2-D vector, an additional axis has to be added. Continuing from the previous example, the np.newaxis can create a new 2-D column vector from a 1-D vector.

>>> a

array([1., 1., 1.])

>>> a[np.newaxis, :]

array([[1., 1., 1.]])

>>> a[np.newaxis, :].shape

(1, 3)

>>> a[:, np.newaxis]

array([[1.],

           [1.],

           [1.]])

>>> a[:, np.newaxis].shape

(3, 1)

The transpose operation on an array also takes an argument axes. If the argument axes are none, the transpose operation reverses the order of axes.

>>> a = np.arange(2 * 3 * 4).reshape(2, 3, 4)

>>> a

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]]])

>>> a_t = a.transpose()

>>> a_t

array([[[ 0, 12],

            [ 4, 16],

            [ 8, 20]],


           [[ 1, 13],

            [ 5, 17],

            [ 9, 21]],


           [[ 2, 14],

            [ 6, 18],

            [10, 22]],


           [[ 3, 15],

            [ 7, 19],

            [11, 23]]])

>>> a.shape

(2, 3, 4)

>>> a_t.shape

(4, 3, 2)

In the above example, the dimension of matrix A was (2, 3, 4), and after transpose, it became (4, 3, 2). The default transpose rule reverses the axis of the input matrix i.e AT[i, j, k] = A[k, j, i].

This default permutation can be changed by passing a tuple of integers as an input argument to transpose. In the below example, the j in the ith place of the tuple means that A’s ith axis will become A.transpose()’s jth axis. Continuing from the previous example, we pass the arguments (1, 2, 0) to a.transpose(). The transpose rule thus followed here is AT[i, j, k] = A[j, k,  i].

>>> a_t = a.transpose((1, 2, 0))

>>> a_t.shape

(3, 4, 2)

>>> a_t

array([[[ 0, 12],

            [ 1, 13],

            [ 2, 14],

            [ 3, 15]],


           [[ 4, 16],

            [ 5, 17],

            [ 6, 18],

            [ 7, 19]],


           [[ 8, 20],

            [ 9, 21],

            [10, 22],

            [11, 23]]])

About the author

Arun Palaniappan