python - Python中的多元线性回归

标签 python numpy statistics scipy linear-regression

我似乎找不到任何执行多重回归的 python 库。我发现的唯一东西只做简单的回归。我需要根据几个自变量(x1、x2、x3 等)对因变量 (y) 进行回归。

例如,使用以下数据:

print 'y        x1      x2       x3       x4      x5     x6       x7'
for t in texts:
    print "{:>7.1f}{:>10.2f}{:>9.2f}{:>9.2f}{:>10.2f}{:>7.2f}{:>7.2f}{:>9.2f}" /
   .format(t.y,t.x1,t.x2,t.x3,t.x4,t.x5,t.x6,t.x7)

(上面的输出:)

      y        x1       x2       x3        x4     x5     x6       x7
   -6.0     -4.95    -5.87    -0.76     14.73   4.02   0.20     0.45
   -5.0     -4.55    -4.52    -0.71     13.74   4.47   0.16     0.50
  -10.0    -10.96   -11.64    -0.98     15.49   4.18   0.19     0.53
   -5.0     -1.08    -3.36     0.75     24.72   4.96   0.16     0.60
   -8.0     -6.52    -7.45    -0.86     16.59   4.29   0.10     0.48
   -3.0     -0.81    -2.36    -0.50     22.44   4.81   0.15     0.53
   -6.0     -7.01    -7.33    -0.33     13.93   4.32   0.21     0.50
   -8.0     -4.46    -7.65    -0.94     11.40   4.43   0.16     0.49
   -8.0    -11.54   -10.03    -1.03     18.18   4.28   0.21     0.55

我将如何在 python 中回归这些,以获得线性回归公式:

Y = a1x1 + a2x2 + a3x3 + a4x4 + a5x5 + a6x6 + +a7x7 + c

最佳答案

sklearn.linear_model.LinearRegression会做的:

from sklearn import linear_model
clf = linear_model.LinearRegression()
clf.fit([[getattr(t, 'x%d' % i) for i in range(1, 8)] for t in texts],
        [t.y for t in texts])

那么clf.coef_就会得到回归系数。

sklearn.linear_model也有类似的接口(interface)来对回归进行各种正则化。

关于python - Python中的多元线性回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11479064/

相关文章:

python - python(numpy、scipy 等)中是否有 t 测试表?

python - 查找 NumPy 数组中大于 N 的值的开始/停止索引范围

c - 检测我的 C 源代码中的冗长(行数)函数

database - Oracle 数据库中的写入次数

python - 处理来自多个进程的单个文件

python - 聚合前的算术运算

python - 如何有效地创建子索引?

python - 为什么 all() 不在第一个 False 元素上停止?

python - 这是一个系列还是数据框?

R - 切割一个向量