Python DataFrame:从线性回归模型向Pandas Dataframe添加预测列

时间:2017-12-21 17:43:56

标签: python pandas output regression statsmodels

我有一个包含三列的数据框:

import statsmodels.api as sm

X = df_adhoc_1_final["MIN_SPAN"] ## X usually means our input variables (or independent variables)
y = df_adhoc_1_final["SPAN_DAYS"] ## Y usually means our output/dependent variable
X = sm.add_constant(X) ## let's add an intercept (beta_0) to our model

# Note the difference in argument order
model = sm.OLS(y, X).fit() ## sm.OLS(output, input)
predictions = model.predict(X)

我使用以下代码创建了一个线性回归模型:

ID  Input_Var   Actual_Value    Predictions
1   234         123             211
2   54          43              58
3   2           52              49
4   4           3               3.4
5   6           1               5

我想将预测添加为现有数据框中的新列。请参阅下表:

df_adhoc_1_final['predictions'] = pd.DataFrame(predictions)

为实现上述目的,我使用了以下代码:

{{1}}

以上是正确的代码吗?它是否将预测与正确的行对齐?

0 个答案:

没有答案