Generalized linear solves
Solving least-squares problems with rectangular matrices.
SciPy · Function
Computes the Moore–Penrose pseudo-inverse of a matrix using an economy-mode singular-value decomposition. Singular values below atol + rtol * s are treated as insignificant, and the effective rank can optionally be returned.
pinv computes a generalized inverse through an economy-mode singular-value decomposition. It keeps singular-value components whose values meet the cutoff atol + rtol * s, where s is the largest singular value, and returns an array whose final two dimensions are transposed relative to the input matrix.
scipy.linalg.pinv(a, *, atol=None, rtol=None, return_rank=False, check_finite=True)| Name | Type | Description |
|---|---|---|
a | (..., M, N) array_like | Matrix to be pseudo-inverted. |
atol | float, optional | Absolute threshold term; default is 0. |
rtol | float, optional | Relative threshold term; default is max(M, N) * eps, where eps is machine precision for the input datatype. |
return_rank | bool, optional | If True, return the effective rank of the matrix. |
check_finite | bool, optional | Whether to check that the input matrix contains only finite numbers. |
B (..., N, M) ndarray: the pseudo-inverse of matrix a. rank int: the effective rank, returned when return_rank is True.
pinv when an ordinary inverse is unavailable or unsuitable, including rectangular or rank-deficient matrices.a accepts an array-like matrix, including leading batch dimensions in the documented current API.return_rank=True to receive the effective rank alongside the pseudo-inverse.atol or rtol can change the effective rank and result.check_finite=False only when the input is already known to contain finite values.import numpy as np
from scipy.linalg import pinv
A = np.array([[1.0, 2.0], [2.0, 4.0]])
A_pinv, rank = pinv(A, return_rank=True)
print(A_pinv)
print(rank)Computes a pseudo-inverse for a rank-deficient matrix and returns its effective rank.
Solving least-squares problems with rectangular matrices.
Handling matrices that are singular or rank-deficient.
Estimating numerical rank from singular-value filtering.
pinv decide which singular values to retain?It computes the Moore–Penrose pseudo-inverse using an economy-mode singular-value decomposition. Singular values below atol + rtol * s, with s as the largest singular value, are treated as insignificant.
Follow-up: How would you identify numerical rank? Call pinv with return_rank=True; the second returned value is the effective rank determined by that singular-value cutoff.
For an input with shape (..., M, N), the pseudo-inverse has shape (..., N, M). If return_rank=True, the function additionally returns the effective rank.
Follow-up: What does return_rank=False change? It makes the return value just the pseudo-inverse array rather than a tuple containing the array and rank.
a is the array-like matrix to invert. atol is an absolute threshold term, rtol is a relative threshold term, check_finite controls validation of finite inputs, and return_rank requests the effective rank.
Follow-up: Why retain finite-value checking? Disabling it can improve performance, but invalid values can cause crashes or non-termination.
scipy.linalg.svdscipy.linalg.lstsqscipy.linalg.pinvh