使用 Python numpy 进行线性回归

发布于 2024-09-27 08:58:13 字数 840 浏览 7 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不可一世的女人 2024-10-04 08:58:13

正如另一个答案中所解释的,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.

卷耳 2024-10-04 08:58:13

奇异矩阵是行列式为零的矩阵。这表明您的矩阵具有不线性独立的行。例如,如果其中一行不是与其他行线性独立的,那么它可以通过其他行的线性组合来构造。我将使用 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.])

现在,我将更改 a 以使其成为单数。

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

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

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:

>>> 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 a to make it singular.

>>> 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文