NumPy ndarray.T Function:
Numpy .t: The ndarray.T in NumPy module is an array attribute which it is used to get the transposed array.
Syntax:
numpy.ndarray.T
Parameters:
This method doesn’t accept any parameters
Return Value:
The transposed array is returned by this function.
NumPy ndarray.T Function in Python
Example1
Approach:
- Import numpy module using the import keyword.
- Pass some random list as an argument to the array() function of the numpy module to create an array.
- Store it in a variable.
- Apply .T attribute on the given array to get a transpose of the given array(interchange rows& columns).
- Store it in another variable.
- Print the given array.
- Print the above-obtained transposed Array.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Pass some random list as an argument to the array() function of the # numpy module to create an array. # Store it in a variable. gvn_arry = np.array([[1,2,3],[4, 5, 6]]) # Apply .T attribute on the given array to get a transpose # of the given array(interchange rows& columns) # Store it in another variable. trnspos_arry = gvn_arry.T # Print the given array print("The given array is:") print(gvn_arry) print() # Print the above-obtained transposed Array print("The above obtained transposed Array is:") print(trnspos_arry)
Output:
The given array is: [[1 2 3] [4 5 6]] The above obtained transposed Array is: [[1 4] [2 5] [3 6]]
Example2: For 1 dimensional array
Here it remains unchanged
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Pass some random list as an argument to the array() function of the # numpy module to create an array. # Store it in a variable. gvn_arry = np.array([1,2,3]) # Apply .T attribute on the given array to get a transpose # of the given array(interchange rows& columns) # Store it in another variable. trnspos_arry = gvn_arry.T # Print the given array print("The given array is:") print(gvn_arry) print() # Print the above-obtained transposed Array print("The above obtained transposed Array is:") print(trnspos_arry)
Output:
The given array is: [1 2 3] The above obtained transposed Array is: [1 2 3]