LinAlgError: Singular matrix usually appears when NumPy tries to invert or solve a linear system with a matrix that has no unique inverse. In practical terms, one row or column is dependent on another, the determinant is zero or very close to zero, or the data is rank-deficient.
NumPy’s numpy.linalg.inv() documentation says a LinAlgError is raised if a matrix is not square or inversion fails, and it specifically notes that singular matrices are detected as an error.
What Is LinAlgError: Singular Matrix?

A matrix is singular when it does not have an inverse. For a square matrix, a determinant of zero is the usual sign. When you pass such a matrix to np.linalg.inv(), NumPy cannot compute a valid inverse and raises numpy.linalg.LinAlgError: Singular matrix.
import numpy as np
A = np.array([
[1, 2],
[2, 4],
])
print(np.linalg.det(A))
print(np.linalg.inv(A))
Output:
0.0 numpy.linalg.LinAlgError: Singular matrix
The second row is just the first row multiplied by 2, so the matrix does not contain enough independent information to have an inverse.
Why Does LinAlgError: Singular Matrix Happen?
- Duplicate or dependent columns: one feature is a linear combination of another feature.
- Zero determinant: a square matrix with determinant zero is singular.
- Rank deficiency: the matrix rank is smaller than the number of columns or rows needed for a unique solution.
- Too many related features in a model: common in regression and machine learning data with multicollinearity.
- Ill-conditioned data: the matrix is almost singular, so results may be unstable even if no error is raised.
Check Whether a Matrix Is Singular
You can start with the determinant for small square matrices. NumPy provides numpy.linalg.det() for this.
import numpy as np
A = np.array([
[1, 2],
[2, 4],
])
det = np.linalg.det(A)
if np.isclose(det, 0):
print("The matrix is singular or close to singular")
else:
print(np.linalg.inv(A))
For numerical code, use np.isclose() instead of checking det == 0 directly. Floating-point calculations may produce tiny values close to zero instead of exactly zero. See also NumPy allclose() for tolerance-based comparison.
Use matrix_rank() for Rank Deficiency
For larger matrices, rank is often more useful than determinant. NumPy’s matrix_rank() uses singular values to estimate rank.
import numpy as np
A = np.array([
[1, 2],
[2, 4],
])
rank = np.linalg.matrix_rank(A)
rows, columns = A.shape
print(rank)
print(min(rows, columns))
1 2
The rank is lower than the matrix dimension, so the matrix is rank-deficient and cannot be inverted.
How to Fix LinAlgError: Singular Matrix
1. Remove Dependent Features or Columns
If a column duplicates another column or can be calculated from other columns, remove one of them. This is common when one-hot encoding categories and keeping every dummy column plus an intercept term.
2. Use solve() Instead of inv() When Solving Ax = b
If your goal is solving a linear system, avoid manually computing inv(A) @ b. Use np.linalg.solve(A, b) for square, non-singular systems. It is clearer and numerically better.
import numpy as np
A = np.array([
[3, 1],
[1, 2],
])
b = np.array([9, 8])
x = np.linalg.solve(A, b)
print(x)
3. Use pinv() When a Pseudo-Inverse Is Acceptable
If the matrix is singular but your problem allows a least-squares-style generalized inverse, use numpy.linalg.pinv(). It computes the Moore-Penrose pseudo-inverse using singular value decomposition.
import numpy as np
A = np.array([
[1, 2],
[2, 4],
])
print(np.linalg.pinv(A))
Do not blindly replace every inverse with pinv(). Use it when a pseudo-inverse matches the mathematics of the problem.
4. Check the Condition Number
A matrix can be close to singular even when NumPy does not raise an error. Use np.linalg.cond(A) to inspect the condition number. A very large condition number means the inverse may be numerically unstable.
LinAlgError: Singular Matrix in Pandas and Machine Learning
Pandas itself usually is not the source of this error. The error appears when pandas data is passed into NumPy, statsmodels, scikit-learn, or another library that performs linear algebra. Common causes include duplicate columns, perfectly correlated features, too many dummy variables, or a model matrix with no full-rank solution.
Practical fixes include dropping duplicate columns, removing one dummy variable per category, scaling or regularizing features, checking for constant columns, and using a solver or model that matches the data. For related matrix basics, see matrix addition in Python.
FAQs
Sometimes. numpy.linalg.pinv() computes a pseudo-inverse and can be appropriate for least-squares or rank-deficient problems. It should not be used blindly when your problem requires a true inverse.
Related NumPy and Python Guides
- Matrix addition in Python
- NumPy cross product
- NumPy allclose()
- NumPy ndarray object is not callable
- Convert NumPy array to Pandas DataFrame
Conclusion
LinAlgError: Singular matrix means the matrix cannot be inverted or solved in the way the code requested. Check the determinant for small matrices, use matrix_rank() to find rank deficiency, remove dependent columns, prefer solve() over explicit inverse for linear systems, and use pinv() only when a pseudo-inverse is appropriate.
![[Fixed] nameerror: name Unicode is not defined](https://www.pythonpool.com/wp-content/uploads/2024/01/Fixed-nameerror-name-Unicode-is-not-defined-300x157.webp)
![[Fixed] typeerror can’t compare datetime.datetime to datetime.date](https://www.pythonpool.com/wp-content/uploads/2024/01/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp)
![[Solved] runtimeerror: cuda error: invalid device ordinal](https://www.pythonpool.com/wp-content/uploads/2024/01/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp)
![[Fixed] typeerror: type numpy.ndarray doesn’t define __round__ method](https://www.pythonpool.com/wp-content/uploads/2024/01/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-300x157.webp)