python - 多元线性回归 scikit-learn 和 statsmodel

标签 python scikit-learn statsmodels

我正在尝试使用 scikit-learn 对数据集使用多元线性回归,但在获取正确的系数时遇到问题。我正在使用休伦湖数据,可以在此处找到:

https://vincentarelbundock.github.io/Rdatasets/datasets.html

转换后,我有以下一组值:

         x1        x2        y
0  0.202165  1.706366  0.840567
1  1.706366  0.840567  0.694768
2  0.840567  0.694768 -0.291031
3  0.694768 -0.291031  0.333170
4 -0.291031  0.333170  0.387371
5  0.333170  0.387371  0.811572
6  0.387371  0.811572  1.415773
7  0.811572  1.415773  1.359974
8  1.415773  1.359974  1.504176
9  1.359974  1.504176  1.768377
...  ...       ...       ...

使用

df = pd.DataFrame(nvalues, columns=("x1", "x2", "y"))
result = sm.ols(formula="y ~ x2 + x1", data=df).fit()

print(result.params)

产量

Intercept   -0.007852
y2           1.002137
y1          -0.283798

这是正确的值,但如果我最终使用 scikit-learn 我得到:

a = np.array([nvalues["x1"], nvalues["x2"]])
b = np.array(nvalues["y"])

a = a.reshape(len(nvalues["x1"]), 2)
b = b.reshape(len(nvalues["y"]), 1)

clf = linear_model.LinearRegression()
clf.fit(a, b)

print(clf.coef_)

我得到[[-0.18260922 0.08101687]]

为了完整性,我的代码

from sklearn import linear_model

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import statsmodels.formula.api as sm

def Main():
    location = r"~/Documents/Time Series/LakeHuron.csv"
    ts = pd.read_csv(location, sep=",", parse_dates=[0], header=0)

    #### initializes the data ####
    ts.drop("Unnamed: 0", axis=1, inplace=True)

    x = ts["time"].values
    y = ts["LakeHuron"].values

    x = x.reshape(len(ts), 1)
    y = y.reshape(len(ts), 1)

    regr = linear_model.LinearRegression()
    regr.fit(x, y)

    diff = []
    for i in range(0, len(ts)):
        diff.append(float(ts["LakeHuron"][i]-regr.predict(x)[i]))

    ts[3] = diff

    nvalues = {"x1": [], "x2": [], "y": []}

    for i in range(0, len(ts)-2):
        nvalues["x1"].append(float(ts[3][i]))
        nvalues["x2"].append(float(ts[3][i+1]))
        nvalues["y"].append(float(ts[3][i+2]))

    df = pd.DataFrame(nvalues, columns=("x1", "x2", "y"))
    result = sm.ols(formula="y ~ x2 + x1", data=df).fit()

    print(result.params)

    #### using scikit-learn ####
    a = np.array([nvalues["x1"], nvalues["x2"]])
    b = np.array(nvalues["y"])

    a = a.reshape(len(nvalues["x1"]), 2)
    b = b.reshape(len(nvalues["y"]), 1)

    clf = linear_model.LinearRegression()
    clf.fit(a, b)

    print(clf.coef_)

if __name__ == "__main__":
    Main()

最佳答案

问题出在线路上

a = np.array([nvalues["x1"], nvalues["x2"]])

因为它没有按照您想要的方式对数据进行排序。相反,它将生成一个数据集

x1_new    x2_new
-----------------
 x1[0]     x1[1]
 x1[2]     x1[3]
[...]
 x1[94]    x1[95]
 x2[0]     x2[1]
[...]

尝试一下

ax1 = np.array(nvalues["x1"])
ax2 = np.array(nvalues["x2"])
ax1 = ax1.reshape(len(nvalues["x1"]), 1)
ax2 = ax2.reshape(len(nvalues["x2"]), 1)
a = np.hstack([ax1,ax2])

可能有一种更简洁的方法可以做到这一点,但这种方法是有效的。现在回归也给出了所有正确的结果。

编辑: 更简洁的方法是使用 transpose():

a = a.transpose()

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

相关文章:

python - 了解 statsmodels grangercausalitytests 的输出

python - 如何创建一个包含 n 个字典的列表

python - 检查 sitecustomize 中的 python 交互模式

python - 如何在 scikit-learn 中使用交叉验证获得预测概率

使用 Scikit-learn 进行拟合时出现 Python MemoryError

python - 在 Python : statsmodel. api vs sklearn 中使用简单线性回归包的不同结果

python - 无法正确读取 html 页面中的日期列并设置其格式

python - 很难使用 'findscu' 然后使用 'movescu' 然后在多个登录号上迭代重复

python - Scikit-Learn:所有训练示例中都存在标签 not x

python - 如何在 logit statsmodel python 中包含交互变量?