Return rank of a rank-deficit matrix using Singular Value Decomposition method in Python


To return matrix rank of array using Singular Value Decomposition method, use the numpy.linalg.matrix_rank() method in Python. Rank of the array is the number of singular values of the array that are greater than tol. The 1st parameter, A is the input vector or stack of matrices.

The 2nd parameter, tol is the Threshold below which SVD values are considered zero. If tol is None, and S is an array with singular values for M, and eps is the epsilon value for datatype of S, then tol is set to S.max() * max(M, N) * eps. The 3rd parameter, hermitian, If True, A is assumed to be Hermitian, enabling a more efficient method for finding singular values. Defaults to False.

Steps

At first, import the required libraries-

import numpy as np
from numpy.linalg import matrix_rank

Create an array −

arr = np.eye(5)

Display the array −

print("Our Array...\n",arr)

Check the Dimensions −

print("\nDimensions of our Array...\n",arr.ndim)

Get the Datatype −

print("\nDatatype of our Array object...\n",arr.dtype)

Get the Shape −

print("\nShape of our Array object...\n",arr.shape)

To return matrix rank of array using Singular Value Decomposition method, use the numpy.linalg.matrix_rank() method −

print("\nRank...\n",matrix_rank(arr))
arr[-1,-1] = 0.
print("\nUpdated Rank (Rank-Deficit Matrix)...\n",matrix_rank(arr))

Example

import numpy as np
from numpy.linalg import matrix_rank

# Create an array
arr = np.eye(5)

# Display the array
print("Our Array...\n",arr)

# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)

# Get the Shape
print("\nShape of our Array object...\n",arr.shape)

# To Return matrix rank of array using Singular Value Decomposition method, use the numpy.linalg.matrix_rank() method in Python
print("\nRank...\n",matrix_rank(arr))
arr[-1,-1] = 0.
print("\nUpdated Rank (Rank-Deficit Matrix)...\n",matrix_rank(arr))

Output

Our Array...
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]]

Dimensions of our Array...
2

Datatype of our Array object...
float64

Shape of our Array object...
(5, 5)

Rank...
5

Updated Rank (Rank-Deficit Matrix)...
4

Updated on: 25-Feb-2022

130 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements