Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

linalg.pinv

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.

Since
1.7.0

How it works

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.

Syntax

scipy.linalg.pinv(a, *, atol=None, rtol=None, return_rank=False, check_finite=True)

Parameters

NameTypeDescription
a(..., M, N) array_likeMatrix to be pseudo-inverted.
atolfloat, optionalAbsolute threshold term; default is 0.
rtolfloat, optionalRelative threshold term; default is max(M, N) * eps, where eps is machine precision for the input datatype.
return_rankbool, optionalIf True, return the effective rank of the matrix.
check_finitebool, optionalWhether to check that the input matrix contains only finite numbers.

Returns

B (..., N, M) ndarray: the pseudo-inverse of matrix a. rank int: the effective rank, returned when return_rank is True.

Key points

  • Use pinv when an ordinary inverse is unavailable or unsuitable, including rectangular or rank-deficient matrices.
  • The computation is based on an economy-mode singular-value decomposition.
  • A singular value is retained according to a cutoff formed from the absolute threshold, relative threshold, and largest singular value.
  • The input a accepts an array-like matrix, including leading batch dimensions in the documented current API.
  • Set return_rank=True to receive the effective rank alongside the pseudo-inverse.
  • The primary result is an ndarray pseudo-inverse with the last two dimensions reversed from the input.

Common mistakes

  • Singular values below the combined absolute and relative cutoff are discarded, so changing atol or rtol can change the effective rank and result.
  • Use check_finite=False only when the input is already known to contain finite values.

Example: Pseudo-inverse and effective rank

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.

When to use it

Generalized linear solves

Solving least-squares problems with rectangular matrices.

Stable inverse-like computations

Handling matrices that are singular or rank-deficient.

Rank estimation

Estimating numerical rank from singular-value filtering.

Interview questions

QHow does 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.

QWhat does the function return and how are shapes handled?

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.

QWhich parameters control thresholding and input validation?

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.svd
  • scipy.linalg.lstsq
  • scipy.linalg.pinvh

Source: scipy.linalg.pinv. Checked 2026-10-15.