Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
---
id: 0a7e9c723a
question: 'LinAlgError: Singular matrix'
question: 'np.linalg.inv(XTX) throws LinAlgError: Singular matrix — what does this
mean?'
sort_order: 3
---

It’s possible that when you follow the videos, you’ll get a Singular Matrix error. This will be explained in the Regularization video. Don’t worry, it’s normal to encounter this.
A `LinAlgError: Singular matrix` from `np.linalg.inv(XTX)` means `XTX` is not invertible—typically because its rows/columns are linearly dependent (often due to redundant features or insufficient independent information).

You might also receive this error if you invert matrix `X` more than once in your code.
To diagnose, check whether the matrix is rank-deficient:

- Compute `np.linalg.matrix_rank(XTX)` and compare it to the number of columns of `XTX` (if the rank is smaller, it’s singular).

Common fixes:

- Drop or combine redundant features so you get a full-rank `XTX`.
- Instead of the exact inverse, use the pseudo-inverse: `np.linalg.pinv(XTX)`. The pseudo-inverse works even when the matrix is singular, and is often the preferred approach in ML when you can’t guarantee full rank.

If you still get the error, also verify you’re forming `XTX` correctly (e.g., for linear regression it should be based on `X.T @ X`, not `X @ X.T`).