Python Numpy 错误:奇异矩阵

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13795682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:38:31  来源:igfitidea点击:

Numpy error: Singular matrix

pythonnumpy

提问by KaliMa

What does the error Numpy error: Matrix is singularmean specifically (when using the linalg.solvefunction)? I have looked on Google but couldn't find anything that made it clear when this error occurs.

错误Numpy error: Matrix is singular具体是什么意思(使用该linalg.solve函数时)?我已经在谷歌上查看过,但在发生此错误时找不到任何明确的信息。

采纳答案by Michael J. Barber

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.solvecan't handle this.

奇异矩阵是不可逆的。这意味着您要求解的方程组没有唯一解;linalg.solve不能处理这个。

You may find that linalg.lstsqprovides a usable solution.

您可能会发现它linalg.lstsq提供了一个可用的解决方案。

回答by Procope

This function inverts singular matrices as well using numpy.linalg.lstsq:

此函数也使用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]