What does the error Numpy error: Matrix is singular
mean specifically (when using the linalg.solve
function)? I have looked on Google but couldn't find anything that made it clear when this error occurs.
A singular matrix is one that is not invertible. This means that the system of equations you are trying to solve does not have a unique solution; linalg.solve
can't handle this.
You may find that linalg.lstsq
provides a usable solution.
This function inverts singular matrices as well using numpy.linalg.lstsq
:
def inv(m):
a, b = m.shape
if a != b:
raise ValueError("Only square matrices are invertible.")
i = np.eye(a, a)
return np.linalg.lstsq(m, i)[0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With