简体   繁体   English

使用sklearn进行多元线性回归

[英]Using sklearn for multiple linear regression

I have a timeseries that looks like this: 我有一个类似的时间序列:

        date       var1     var2       var3       var4       var5      var6
0 2004-09-30   6.252216  10.502101  4.965370  26.828754  3.321060  2.723686   
1 2004-10-29   6.861840   9.776618  4.719399  27.621344  2.281346  4.449510   
2 2004-11-30   8.171250  10.704045  4.949747  30.259377  2.064655  2.843745   
3 2004-12-31   9.702585  11.371383  5.422177  33.578991 -1.008974  2.768579   
4 2005-01-31  12.064022  10.628460  6.390097  35.135098 -0.385921  3.244204   

I want to use sklearn's linear regression function to calculate the slope, y-intercept, and error (r-squared) on this timeseries. 我想使用sklearn的线性回归函数来计算此时间序列上的斜率,y轴截距和误差(r平方)。 Note that all these values are already normalized via my own function, and there is no need for me to use sklearn's normalize parameter. 请注意,所有这些值已经通过我自己的函数进行了标准化,因此不需要使用sklearn的normalize参数。

This is my code so far to do the regression on one column: 到目前为止,这是我对一列进行回归的代码:

reg.fit(df.date.values.reshape(-1, 1), df.var1.values.reshape(-1, 1))
alpha = reg.intercept_[0]
beta = reg.coef_[0][0]
error = reg.score(df.date.values.reshape(-1, 1), df.var1.values.reshape(-1, 1))
values = {"alpha":alpha, "beta":beta, "error": error}

My issue is that I don't know how to do the regression considering every column at once. 我的问题是,我不知道如何同时考虑到每一列进行回归。 On top of that, the R^2 calculation does not work. 最重要的是,R ^ 2计算不起作用。

R^2 aside, my slope & intercept for some individual column is incredible small: 除了R ^ 2,我对某些独立列的斜率和截距非常小:

{'beta': -3.205305722098675e-17, 'alpha': 43.05076221170246}

How would I address these issues? 我将如何解决这些问题?

Instead of 代替

df.var1.values.reshape(-1, 1)

Just pass 刚过去

df.drop('date', axis=1)  # .values should be optional here also

in its place. 代替它。

This gives you df with all columns excluding date . 这使df具有除date之外的所有列。

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

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