Python Numpy matrix.T() Function

NumPy Library 

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it into our program and then we can easily use the functionality of NumPy in our program.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

Numpy matrix.T() Function:

We can build a Transpose of any matrix of dimension one or more than one by using the numpy.matrix.T() method of the Numpy module.

Syntax:

 numpy.matrix.T()

Return Value:

The transpose of every matrix is returned by the matrix.T() function.

Transpose of a Matrix

The transpose of a matrix is one of the most often used methods in matrix transformation in linear algebra.

The transpose of a matrix for a given matrix is obtained by swapping/interchanging rows into columns or columns to rows.

For Example:

Let the matrix be: 
[1  2]
[3  4]
Transpose of a matrix is:
[1  3]
[2  4]

Numpy matrix.T() Function in Python

For 1-Dimensional (1D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(1-Dimensional) using the matrix() function of numpy module by passing some random 1D matrix as an argument to it and store it in a variable
  • Apply getT() function on the given matrix to get the transpose of a given matrix.
  • Store it in another variable
  • Print the transpose of a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(1-Dimensional) using the matrix() function of numpy module by passing 
# some random 1D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 1, 6, 3]')
            
# Apply getT() function on the given matrix to get the transpose of a given matrix.
# Store it in another variable
rslt = gvn_matrx.getT()
# Print the transpose of a given matrix.
print("The transpose of a given matrix:")
print(rslt)

Output:

The transpose of a given matrix:
[[2]
 [1]
 [6]
 [3]]

For 3-Dimensional (3D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing some random 3D matrix as an argument to it and store it in a variable
  • Apply getT() function on the given matrix to get the transpose of a given matrix.
  • Store it in another variable
  • Print the transpose of a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing 
# some random 3D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 4, 1; 8, 7, 3; 10, 9, 5]')
            
# Apply getT() function on the given matrix to get the transpose of a given matrix.
# Store it in another variable
rslt = gvn_matrx.getT()
# Print the transpose of a given matrix.
print("The transpose of a given matrix:")
print(rslt)

Output:

The transpose of a given matrix:
[[ 2 8 10]
 [ 4 7 9]
 [ 1 3 5]]