使用 Python numpy 进行线性回归
我正在尝试制作一个简单的线性回归函数,但继续遇到
numpy.linalg.linalg.LinAlgError:奇异矩阵错误
现有函数(带有调试打印):
def makeLLS(inputData, targetData):
print "In makeLLS:"
print " Shape inputData:",inputData.shape
print " Shape targetData:",targetData.shape
term1 = np.dot(inputData.T, inputData)
term2 = np.dot(inputData.T, targetData)
print " Shape term1:",term1.shape
print " Shape term2:",term2.shape
#print term1
#print term2
result = np.linalg.solve(term1, term2)
return result
带有我的测试数据的控制台输出是:
In makeLLS:
Shape trainInput1: (773, 10)
Shape trainTargetData: (773, 1)
Shape term1: (10, 10)
Shape term2: (10, 1)
然后它在 linalg.solve 行上出错。这是教科书线性回归函数,我似乎无法弄清楚它失败的原因。
什么是奇异矩阵误差?
I'm trying to make a simple linear regression function but continue to encounter a
numpy.linalg.linalg.LinAlgError: Singular matrix error
Existing function (with debug prints):
def makeLLS(inputData, targetData):
print "In makeLLS:"
print " Shape inputData:",inputData.shape
print " Shape targetData:",targetData.shape
term1 = np.dot(inputData.T, inputData)
term2 = np.dot(inputData.T, targetData)
print " Shape term1:",term1.shape
print " Shape term2:",term2.shape
#print term1
#print term2
result = np.linalg.solve(term1, term2)
return result
The output to the console with my test data is:
In makeLLS:
Shape trainInput1: (773, 10)
Shape trainTargetData: (773, 1)
Shape term1: (10, 10)
Shape term2: (10, 1)
Then it errors on the linalg.solve line. This is a textbook linear regression function and I can't seem to figure out why it's failing.
What is the singular matrix error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如另一个答案中所解释的,
linalg.solve
需要一个满秩矩阵。这是因为它尝试求解矩阵方程,而不是进行适用于所有等级的线性回归。线性回归有几种方法。我建议的最简单的方法是标准最小二乘法。只需使用 numpy.linalg.lstsq 即可。包含示例的文档位于 此处。
As explained in the other answer
linalg.solve
expects a full rank matrix. This is because it tries to solve a matrix equation rather than do linear regression which should work for all ranks.There are a few methods for linear regression. The simplest one I would suggest is the standard least squares method. Just use
numpy.linalg.lstsq
instead. The documentation including an example is here.奇异矩阵是行列式为零的矩阵。这表明您的矩阵具有不线性独立的行。例如,如果其中一行不是与其他行线性独立的,那么它可以通过其他行的线性组合来构造。我将使用 numpy 的 linalg.solve 示例来演示。这是文档的示例:
现在,我将更改
a
以使其成为单数。这是一个非常明显的例子,因为第一行只是第二行的两倍,但希望您明白这一点。
A singular matrix is one for which the determinant is zero. This indicates that your matrix has rows that aren't linearly independent. For instance, if one of the rows is not linearly independent of the others, then it can be constructed by a linear combination of the other rows. I'll use numpy's linalg.solve example to demonstrate. Here is the doc's example:
Now, I'll change
a
to make it singular.This is a very obvious example because the first row is just double the second row, but hopefully you get the point.