【发布时间】:2018-06-25 13:30:27
【问题描述】:
我一直在寻找在给定 Pandas Dataframe 的情况下创建线性回归模型的最新方法。
DF 看起来像:
+---------------------+-------------+--------------------+--------------------+
| Date | YearWeekNum | Dependent_Variable | Bonus_Grouping_Int |
+---------------------+-------------+--------------------+--------------------+
| 2017-07-01 00:12:07 | 2017-Wk26 | 35.4 | 1 |
| 2017-07-01 00:12:07 | 2017-Wk26 | 33.3 | 2 |
| 2018-01-05 25:12:07 | 2018-Wk0 | 28.2 | 1 |
| 2018-01-05 25:12:07 | 2018-Wk0 | 24.2 | 2 |
+---------------------+-------------+--------------------+--------------------+
我创建了 YearWeekNum 列:
df['YearWeekNum'] = df['Date'].dt.strftime('%Y-Wk%U')
我希望喜欢能够创建一个线性回归,它使用YearWeekNum 作为独立(预测)变量,Dependent Variable 作为(你猜对了)依赖(响应) 多变的。最后的情节是这样的:
我尝试了this question,使用result = sm.ols(formula="Dependent_Variable ~ YearWeekNum", data=df).fit(),但它创建了一个以每个 YearWeekNum 作为其自变量的模型(对每个周期间进行回归。
从这个one,我也试过了:
from pandas.stats.api import ols
但是得到了:
ImportError: cannot import name 'ols'
似乎 ols 已被弃用。所以,我的问题是:如何使用 Pandas 以年份和周数作为自变量对数据框进行线性回归?
Cherry on top:将基于分组 int 创建两个回归模型(红线是 Grouping int 1 的值,靛蓝线是 Grouping int 2 的值)
提前致谢!
【问题讨论】:
标签: python pandas linear-regression