What is the numpy.ndarray.T() attribute in Python?

Overview

The ndarray.T in Python is used to obtain the transpose of a given array.

Transpose: The transpose of a matrix is the process of interchanging the row of a matrix with the column and vice-versa.

How transpose works

Below is an illustration of how the transpose of an array works.

Taking transpose of an array

Syntax

Let's have a look at the syntax of ndarray.T.

ndarray.T
Syntax for the ndarray.T attribute

Parameter value

The ndarray.T attribute does not take any parameter value.

Return value

The ndarray.T return the array holding the transpose of an input array.

Example

import numpy as np
# creating an input array
myarray = np.array([[1,2,3], [4,5,6]])
# printing the input array
print(myarray)
# implementing the ndarray.T attribute
print(myarray.T)

Code explanation

  • Line 1: We import the numpy module.
  • Line 3: We create an input array, myarray, using the array() function.
  • Line 6: We print the array, myarray.
  • Line 9: We obtain and print the transpose of the input array using the ndarray.T attribute.

Free Resources