๐Ÿ€Zerve chosen as NCAA's Agentic Data Platform for 2026 Hackathonยท๐ŸงฎMeet the Zerve Team at Data Decoded Londonยท๐Ÿ“ˆWe're hiring โ€” awesome new roles just gone live!
Back
Numpy

LinAlgError: Singular Matrix - How to Fix It

Answer

This error means your matrix cannot be inverted because it has linearly dependent rows or columns (determinant is zero). Fix it by checking for collinearity in your data, using pseudoinverse with np.linalg.pinv() instead of np.linalg.inv(), or adding regularization to make the matrix invertible.

Why This Happens

A singular matrix has no inverse because at least one row or column can be expressed as a combination of others. This commonly occurs with duplicate features, perfectly correlated variables, more features than samples, or data with zero variance columns. Operations like solving linear equations or computing inverses fail on singular matrices.

Solution

The rule: check determinant or rank before inverting. Use pinv() for a robust pseudoinverse, lstsq() for solving equations, or add regularization when you need a true inverse.

import numpy as np

# โŒ Problematic: singular matrix (row 2 = 2 * row 1)
A = np.array([[1, 2], [2, 4]])
np.linalg.inv(A)
# LinAlgError: Singular matrix

# โœ… Fixed: check if matrix is singular first
det = np.linalg.det(A)
print(f"Determinant: {det}")  # 0.0 means singular

# โœ… Fixed: use pseudoinverse instead
A_pinv = np.linalg.pinv(A)  # works on singular matrices

# โœ… Fixed: add small regularization (ridge-style)
epsilon = 1e-6
A_reg = A + epsilon * np.eye(A.shape[0])
np.linalg.inv(A_reg)

# โœ… Fixed: use least squares instead of direct solve
b = np.array([1, 2])
# Instead of: x = np.linalg.inv(A) @ b
x, residuals, rank, s = np.linalg.lstsq(A, b, rcond=None)

# โœ… Check matrix rank to diagnose the problem
rank = np.linalg.matrix_rank(A)
print(f"Rank: {rank}, Shape: {A.shape}")
# If rank < min(rows, cols), matrix is singular

# โœ… For data matrices: check for duplicate/correlated columns
correlation = np.corrcoef(A.T)
print(correlation)  # look for values near 1 or -1

Better Workflow

In Zerve, you can visualize matrix properties in real-time: heatmaps showing correlations, eigenvalue distributions, condition numbers. Test multiple fixes in parallel blocks (pseudoinverse, regularization, least squares, SVD) and compare results side by side. Each block's status shows exactly which step failed, with full error context. No more hunting through long scripts to find the problem.

Better workflow

Related Topics

Decision-grade data work

Explore, analyze and deploy your first project in minutes