简体   繁体   English

Python numpy的线性回归

[英]Linear Regression with Python numpy

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. 然后它在linalg.solve线上出错。 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? 什么是奇异矩阵误差?

As explained in the other answer linalg.solve expects a full rank matrix. 正如在另一个答案中解释的那样, linalg.solve期望一个完整的秩矩阵。 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. 只需使用numpy.linalg.lstsq The documentation including an example is here . 包含示例的文档就在这里

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. 我将使用numpy的linalg.solve示例来演示。 Here is the doc's example: 这是doc的例子:

>>> 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使它变得单数。

>>> 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. 这是一个非常明显的例子,因为第一行只是第二行的两倍,但希望你明白这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM