# Numpy linalg matrix_rank: How to Use np linalg matrix_rank()

Numpy linalg matrix_rank() method is used to calculate the Matrix rank of a given matrix using the SVD method.

## Numpy linalg matrix_rank()

The matrix_rank() method returns the matrix rank of the array using the SVD method. The matrix_rank() method is calculated by the number of singular values of the Matrix that are greater than tol.

### Syntax

```numpy.linalg.matrix_rank(array, tol)
```

### Parameters

The matrix_rank() function takes mainly two parameters:

1. Array: This is the array whose rank we want to find.
2. tol: 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.shape) * eps.

### Note

The default limit for detecting rank errors is m. K is a test on the magnitude of the single values. By default, we found smasher values ​​lower than s.max () * max (mHap) * EPS. .

This default range is designed to detect rank reduction accounting for numerical errors of SVD calculation. Hold a column M in M, which is the exact combination (floating point) of the other columns in M. Calculating SVD on M usually does not produce a single value equal to 0: the smallest SVD value.

Any difference from 0 due to numerical disturbances in SVD calculation. Our range for small SVD values ​​takes this numerical disturbance into account, and the default threshold recognizes the absence of such numeric rank.

Although the linear combination of some columns of M is not equal to the second column of M, although numerically very close to the second column of M, the entry matrix M can declare a decrease in rank.

We chose our default limit because it is in widespread use. Other limitations are possible. For example, the 2007 version of the consonant consonant has the optional array smacks () * np.info (M.dtype) elsewhere .eps / 2. * np.sqrt (m + n + 1.). The authors describe this limitation based on the “round off error”.

SVD calculations have the above limitations to deal with the floating-point roundoff error. However, you may have more information about the origins of the error in M, which takes into account other tolerance values ​​to determine the effective rank error.

The most useful measure of patience depends on the functions you want to use on your Matrix. For example, if your data comes from uncertain measurements with greater uncertainty than the floating-point Epsilon, it is better to choose a tolerance close to that uncertainty. Patience is perfect if uncertainty is absolute rather than relative.

### Return Value

The matrix_rank() function returns an integer value, which denotes the rank of the given Matrix.

## Calculate the Matrix rank of the 1D Matrix:

See the following code.

```# Programming example to find matrix rank of the 1D matrix
from numpy import linalg as LA
import numpy as np

arr1 = np.array([4, 5, 0, 1])
print("Matrix rank of the 1st array is: ", LA.matrix_rank(arr1, 0))

arr2 = np.array(np.zeros(4))
print("The Matrix is: ", arr2)
print("Matrix rank of the 2nd array is: ", LA.matrix_rank(arr2, 0))```

#### Output

```Matrix rank of the 1st array is:  1
The Matrix is:  [0. 0. 0. 0.]
Matrix rank of the 2nd array is:  0```

#### Explanation

In this program, we have first imported numpy and numpy.linalg to compute the matrix rank.

We have declared two 1D arrays, and then we had passed tol value when we called matrix_rank() function. We got matrix rank according to the value given.

## Matrix rank of the 2D Matrix

See the following code.

```# Programming example to find matrix rank of 3D matrix
from numpy import linalg as LA
import numpy as np

arr1 = np.array([[1, 2, 3], [6, 5, 4]])
print("The arr1 is :\n", arr1)
print("Matrix Rank is:\n", LA.matrix_rank(arr1, 1))

arr2 = np.array(np.zeros((4, 4)))
print("Arr2 is: \n: ", arr2)
print("Matrix Rank is:\n", LA.matrix_rank(arr2, 2))```

#### Output

```The arr1 is :
[[1 2 3]
[6 5 4]]
Matrix Rank is:
2
Arr2 is:
:  [[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
Matrix Rank is:
0```

#### Explanation

In this program, we have first imported numpy and numpy.linalg to compute the matrix rank.

We have declared two 2D arrays, and then we had passed tol value when we called matrix_rank() function. We got matrix rank according to the value passed.

That is it for the numpy linalg matrix_rank() method.

This site uses Akismet to reduce spam. Learn how your comment data is processed.