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.
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 arraymyarray = np.array([[1,2,3], [4,5,6]])# printing the input arrayprint(myarray)# implementing the ndarray.T attributeprint(myarray.T)
Code explanation
- Line 1: We import the
numpymodule. - Line 3: We create an input array,
myarray, using thearray()function. - Line 6: We print the array,
myarray. - Line 9: We obtain and print the transpose of the input array using the
ndarray.Tattribute.