Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 最小二乘法scipy.sparse.linalg.lsqr返回假精确解_Python_Scipy_Least Squares - Fatal编程技术网

Python 最小二乘法scipy.sparse.linalg.lsqr返回假精确解

Python 最小二乘法scipy.sparse.linalg.lsqr返回假精确解,python,scipy,least-squares,Python,Scipy,Least Squares,我试图找到最小范数(b-A*x)的最小二乘解x 及 所以我做了以下代码 import numpy as np from scipy.sparse import csc_matrix from scipy.sparse.linalg import lsqr b=np.array([1.,1.,-1.], dtype=float) A=csc_matrix([[-1., 1.,0.], [0., -1., 1.], [-1., 0., 1.]], dtype=float) x=lsqr(A,b)[

我试图找到最小范数(b-A*x)的最小二乘解x

所以我做了以下代码

import numpy as np
from scipy.sparse import csc_matrix
from scipy.sparse.linalg import lsqr

b=np.array([1.,1.,-1.], dtype=float)
A=csc_matrix([[-1., 1.,0.], [0., -1., 1.], [-1., 0., 1.]], dtype=float)
x=lsqr(A,b)[0]
我得到

The exact solution is  x = 0                              
array([0., 0., 0.])
显然,Ax=b允许一个精确解作为零向量,但这里Ax=[0,0,0],这显然不是b。 当我用其他的b值尝试这个方法时,我得到了一些似乎可以接受的结果,但是这里我真的不理解输出。
我觉得我遗漏了一些非常明显的东西。

你的矩阵A是奇异的(A的行列式是0),因此非齐次线性系统Ax没有非平凡解=b@joni是的,我知道系统Ax=b没有精确解,因为我的矩阵是奇异的,这就是为什么我尝试使用最小二乘法来近似x并最小化范数(Ax-b),这并不是说没有精确解,如果你有
deta=0
,那么你将有大量的解&计算机不喜欢这样。请参阅数学堆栈上的这个问题-@DrBwts根据您的链接,如果我们的情况是“b在A的列空间中,但det(A)=0”,则有无穷多个解。在我的情况下,b不在A的列中,因此我们没有解。
import numpy as np
from scipy.sparse import csc_matrix
from scipy.sparse.linalg import lsqr

b=np.array([1.,1.,-1.], dtype=float)
A=csc_matrix([[-1., 1.,0.], [0., -1., 1.], [-1., 0., 1.]], dtype=float)
x=lsqr(A,b)[0]
The exact solution is  x = 0                              
array([0., 0., 0.])