Detect rank deficiency
Check whether a matrix is full rank before solving or estimating parameters.
NumPy · Function
Returns the rank of an array using an SVD-based method. It supports vectors, matrices, and stacks of matrices, with optional tolerance and Hermitian optimization.
The function determines rank through singular values computed with SVD. It counts values above the selected threshold; by default, that threshold scales with the largest singular value, matrix dimensions, and floating-point precision. Hermitian input can use a faster singular-value route.
numpy.linalg.matrix_rank(A, tol=None, hermitian=False, *, rtol=None)| Name | Type | Description |
|---|---|---|
A | array_like | Input vector, matrix, or stack of matrices with shape (M,), (M, N), or (..., M, N). |
tol | array_like, float, optional | Threshold below which singular values are treated as zero. If None, NumPy derives it from the largest singular value, matrix dimensions, and dtype precision. |
hermitian | bool, optional | Assume A is Hermitian, or real-symmetric; defaults to False. |
rtol | array_like, float, optional | Relative-tolerance parameter; defaults to max(M, N) × eps. Cannot be supplied with tol. |
rank — array_like: rank of A.
import numpy as np
A = np.array([[1., 2.], [2., 4.]])
print(np.linalg.matrix_rank(A)) # 1
print(np.linalg.matrix_rank([[[1., 0.], [0., 1.]],
[[1., 1.], [2., 2.]]])) # [2 1]Computes the rank of one matrix and of a stack of matrices.
Check whether a matrix is full rank before solving or estimating parameters.
Evaluate several related matrices in one call.
Control numerical rank classification for noisy or ill-conditioned data.
Speed up rank computation for known real-symmetric or Hermitian matrices.
It uses an SVD-based test and counts singular values greater than the threshold. The result is the matrix rank, or one rank value for each matrix in a stack.
Follow-up: How does the default threshold scale? It uses the largest singular value multiplied by the larger matrix dimension and machine epsilon for the singular-value dtype.
A can be a vector, a matrix, or a stack whose trailing dimensions are M by N. For a stack, the function computes ranks independently across the leading dimensions.
Follow-up: What changed for stacked inputs? The function supports stacks and broadcasts the tolerance against the stack dimensions.
hermitian=True declares that A is Hermitian, or symmetric when its values are real, and enables a more efficient singular-value computation. The default is False.
Follow-up: When should it remain False? Keep it false unless the declared structure is guaranteed, because the optimization relies on that assumption.
numpy.linalg.svdnumpy.linalg.normnumpy.linalg.matrix_powernumpy.linalg.solve