Open In App

NumPy ndarray.T | Get View of Transposed Array

The NumPy ndarray.T attribute finds the view of the transposed Array. 

It can transpose any array having a dimension greater than or equal to 2. It works similarly to the numpy.transpose() method but it is easy and concise to use.



Syntax

Syntax: ndarray.T 

Returns



  • Transpose of given array

Examples

Let’s look at how to use the ndarray.T attribute of the Python’s NumPy library.

Example 1




import numpy as np
  
arr = np.array([[1, 2, 3], [4, 5, 6]])     
# applying ndarray.T object
transposed_array = arr.T
   
print(transposed_array)

Output
[[1 4]
 [2 5]
 [3 6]]

Example 2




# import the important module in python 
import numpy as np 
          
# make an array with numpy 
gfg = np.array([[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 0]]) 
          
# applying ndarray.T object 
geeks = gfg.T 
  
print(geeks) 

Output:

[[1 4 7]
 [2 5 8]
 [3 6 9]
 [4 7 0]]

The ndarray.T attribute finds its use in machine learning applications where input data needs to be formatted in a certain way for further processing. 

It does not modify the original array and only returns the view of the transposed array.


Article Tags :