使用 Python numpy 进行线性回归

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3920571/
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 13:21:01  来源:igfitidea点击:

Linear Regression with Python numpy

pythonnumpylinear-regression

提问by Jonathan

I'm trying to make a simple linear regression function but continue to encounter a

我正在尝试制作一个简单的线性回归函数,但继续遇到

numpy.linalg.linalg.LinAlgError: Singular matrix error

numpy.linalg.linalg.LinAlgError:奇异矩阵错误

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.

然后它在 linalg.solve 行上出错。这是一个教科书线性回归函数,我似乎无法弄清楚它为什么失败。

What is the singular matrix error?

什么是奇异矩阵误差?

采纳答案by Muhammad Alkarouri

As explained in the other answer linalg.solveexpects 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.

正如另一个答案中所解释的,linalg.solve需要一个满秩矩阵。这是因为它试图求解矩阵方程,而不是对所有等级都适用的线性回归。

There are a few methods for linear regression. The simplest one I would suggest is the standard least squares method. Just use numpy.linalg.lstsqinstead. The documentation including an example is here.

线性回归有几种方法。我建议的最简单的方法是标准最小二乘法。换用就好了numpy.linalg.lstsq。包含示例的文档在此处

回答by Justin Peel

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:

奇异矩阵是行列式为零的矩阵。这表明您的矩阵具有非线性无关的行。例如,如果其中一行与其他行不是线性独立的,则可以通过其他行的线性组合来构造它。我将使用 numpy 的 linalg.solve 示例来演示。这是文档的示例:

>>> import numpy as np
>>> a = np.array([[3,1], [1,2]])
>>> b = np.array([9,8])
>>> x = np.linalg.solve(a, b)
>>> x
array([ 2.,  3.])

Now, I'll change ato make it singular.

现在,我将更改a以使其单一。

>>> a = np.array([[2,4], [1,2]])
>>> x = np.linalg.solve(a, b)
...
LinAlgError: Singular matrix

This is a very obvious example because the first row is just double the second row, but hopefully you get the point.

这是一个非常明显的例子,因为第一行是第二行的两倍,但希望你明白这一点。