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

NumPy · Function

np.linalg.pinv

Computes the Moore–Penrose pseudo-inverse of a matrix or stack of matrices using singular-value decomposition. Small singular values are discarded according to a cutoff.

How it works

pinv uses singular-value decomposition to construct a generalized inverse, retaining singular values above a cutoff and setting smaller ones to zero. It accepts a single matrix or a stack, and returns an array whose final dimensions reverse the input matrix dimensions.

Syntax

numpy.linalg.pinv(a, rcond=None, hermitian=False, *, rtol=<no value>)

Parameters

NameTypeDescription
a(..., M, N) array_likeMatrix or stack of matrices to be pseudo-inverted.
rcond(...) array_like of float, optionalCutoff for small singular values. Values less than or equal to rcond * largest_singular_value are set to zero; it broadcasts against matrix stacks. Default: 1e-15.
hermitianbool, optionalAssume a is Hermitian, or symmetric when real-valued, enabling a more efficient singular-value method. Defaults to False.
rtolarray_like, optionalAlternative array-like cutoff parameter; the current stable page lists it in the signature as rtol=<no value>.

Returns

B, an (..., N, M) ndarray containing the pseudo-inverse of a; a matrix input produces a matrix output.

Key points

  • Use pinv when an ordinary inverse is unavailable or when a generalized inverse is required.
  • The decomposition-based method handles rank-deficient matrices by filtering small singular values.
  • The input may be one matrix or an array containing a stack of matrices.
  • rcond may broadcast over matrix stacks, so compatible per-matrix cutoffs are supported.
  • When omitted, rcond uses a default cutoff of 1e-15.
  • The Hermitian option can make singular-value computation more efficient when its assumption is valid.

Common mistakes

  • The cutoff directly controls which singular values are discarded; changing rcond can materially change the result.
  • Set hermitian=True only when the input really has the required Hermitian or real-symmetric structure.

Example: Pseudo-inverse of a singular matrix

import numpy as np

A = np.array([[1.0, 2.0], [2.0, 4.0]])
B = np.linalg.pinv(A)
print(B)
print(np.allclose(A @ B @ A, A))

Computes a pseudo-inverse for a rank-deficient matrix and checks one Moore–Penrose identity.

When to use it

Least-squares solutions

Solving underdetermined or overdetermined linear systems.

Generalized inversion

Matrices that are singular or rank-deficient.

Stacked matrix processing

Batch numerical workflows.

Interview questions

QHow does numpy.linalg.pinv compute its result?

It computes the Moore–Penrose pseudo-inverse using singular-value decomposition. Singular values at or below the cutoff are treated as zero, while the remaining singular values contribute to the generalized inverse.

Follow-up: How does the output shape relate to the input? For an input shaped (..., M, N), the result is shaped (..., N, M), preserving any leading stack dimensions.

QWhat does rcond control?

rcond is the threshold multiplier applied to the largest singular value. Singular values no greater than rcond times that largest value are zeroed; its default is 1e-15.

Follow-up: What happens for a stack? The cutoff can broadcast against the stack of matrices, allowing different thresholds where compatible.

QWhen is hermitian=True appropriate?

hermitian=True tells NumPy to assume the matrix is Hermitian, or symmetric for real input, and enables a more efficient singular-value calculation. The default is False.

Follow-up: When should it remain false? If the assumption is not valid, leave it false rather than requesting the specialized path.

  • numpy.linalg.svd
  • numpy.linalg.inv
  • numpy.linalg.matrix_rank

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