我想用 numpy 计算多元线性回归。我需要根据几个自变量(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
20.64, 0.0, 296, 54.7, 0, 519, 2, 24.0
25.12, 0.0, 387, 54.7, 1, 678, 2, 24.0
19.22, 0.0, 535, 54.7, 0, 296, 2, 24.0
18.99, 0.0, 519, 18.97, 0, 296, 2, 54.9
18.89, 0.0, 296, 18.97, 0, 535, 2, 54.9
25.51, 0.0, 678, 18.97, 1, 387, 2, 54.9
20.19, 0.0, 296, 25.51, 0, 519, 2, 54.9
20.75, 0.0, 535, 25.51, 0, 296, 2, 54.9
24.13, 0.0, 387, 25.51, 1, 678, 2, 54.9
19.24, 0.0, 519, 0, 0, 296, 2, 55.0
20.90, 0.0, 296, 0, 0, 535, 2, 55.0
25.30, 0.0, 678, 0, 1, 387, 2, 55.0
20.78, 0.0, 296, 0, 0, 519, 2, 55.2
23.01, 0.0, 535, 0, 0, 296, 2, 55.2
25.20, 0.0, 387, 0, 1, 678, 2, 55.2
19.12, 0.0, 519, 0, 0, 296, 2, 55.3
20.03, 0.0, 296, 0, 0, 535, 2, 55.3
25.22, 0.0, 678, 0, 1, 387, 2, 55.3
我创建了这个函数,我认为它给出了 Y = a1x1 + a2x2 + a3x3 + a4x4 + a5x5 + a6x6++a7x7 + c
的系数 A。
def calculate_linear_regression_numpy(xx, yy):
""" calculate multiple linear regression """
import numpy as np
from numpy import linalg
A = np.column_stack((xx, np.ones(len(xx))))
coeffs = linalg.lstsq(A, yy)[0] # obtaining the parameters
return coeffs
xx
是一个包含每一行 x 的列表,yy
是一个包含所有 y
的列表。
A
是这样的:
00 = {ndarray} [ 0. 296. 519. 2. 0. 24. 54.7 1. ]
01 = {ndarray} [ 0. 296. 535. 2. 0. 24. 54.7 1. ]
02 = {ndarray} [ 0. 387. 678. 2. 1. 24. 54.7 1. ]
03 = {ndarray} [ 0. 296. 519. 2. 0. 54.9 18.97957206 1. ]
04 = {ndarray} [ 0. 296. 535. 2. 0. 54.9 18.97957206 1. ]
05 = {ndarray} [ 0. 387. 678. 2. 1. 54.9 18.97957206 1. ]
06 = {ndarray} [ 0. 296. 519. 2. 0. 54.9 25.518085 1. ]
07 = {ndarray} [ 0. 296. 535. 2. 0. 54.9 25.518085 1. ]
08 = {ndarray} [ 0. 387. 678. 2. 1. 54.9 25.518085 1. ]
09 = {ndarray} [ 0. 296. 519. 2. 0. 55. 0. 1.]
10 = {ndarray} [ 0. 296. 535. 2. 0. 55. 0. 1.]
11 = {ndarray} [ 0. 387. 678. 2. 1. 55. 0. 1.]
12 = {ndarray} [ 0. 296. 519. 2. 0. 55.2 0. 1. ]
13 = {ndarray} [ 0. 296. 535. 2. 0. 55.2 0. 1. ]
14 = {ndarray} [ 0. 387. 678. 2. 1. 55.2 0. 1. ]
15 = {ndarray} [ 0. 296. 519. 2. 0. 55.3 0. 1. ]
16 = {ndarray} [ 0. 296. 535. 2. 0. 55.3 0. 1. ]
17 = {ndarray} [ 0. 387. 678. 2. 1. 55.3 0. 1. ]
np.dot(A,coeffs)
是这样的:
[ 19.69873196 20.33871176 24.95249051 19.59198545
20.23196525 24.845744 19.41602911 20.05600891 24.66978766
20.09928377 20.73926357 25.35304232 20.09237109 20.73235089
25.34612964 20.08891474 20.72889454 25.34267329]
在函数的返回值中,coeffs
包含这 8 个值。
[0.0, -0.0010535377771944548, 0.039998737474281849, 0.62111016637058492, -1.0101687709958682, -0.034563440146209781, -0.026910757873959575, 0.31055508318529385]
我不知道 coeffs[0]
或 coeffs[7]
是上面定义的等式 Y 中的 c
.
我用这个系数计算新的 Ŷ 乘以新的 ẍ 系数,如下所示:
Ŷ=a1ẍ1 + a2ẍ2 + a3ẍ3 + a4ẍ4 + a5ẍ5 + a6ẍ6 + +a7ẍ7 + c
我是否正确计算了 Ŷ?当我得到一个负数的 Ŷ
时我该怎么办? c
(a[0]
或 a[7]
)是哪个术语?
最佳答案
列保持您指定的顺序,否则您将无法使用系数!
请记住,根据最小二乘问题的矩阵形式,您对 Y 的估计由 A 点 C 给出,其中 C 是您的系数向量/矩阵。
所以,打印出A,它应该是X1....X7 [Column of Ones]的形式。
无论哪个列号包含您的列号,都是您的偏移系数的系数向量中的等效项。
仅从参数 coeff[7] 的大小来看,它看起来就是偏移量,因为它大了几个数量级,在给定您提供的 X 和 Y 值的情况下,这看起来不符合逻辑。
关于python - 使用 numpy 进行多元线性回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34785851/