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 -1Better 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.
)
&w=1200&q=75)
&w=1200&q=75)
&w=1200&q=75)