import statsmodels.api as sm
train_x['const'] = 1
lr = sm.OLS(train_y,train_x)
result = lr.fit()
result.summary2()
test_x['const'] = 1
result.predict(test_x)
a1 = pd.DataFrame(result.summary().tables[0])
a2 = pd.DataFrame(result.summary().tables[1])
df_result = a1.append(a2)
df_result
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/abd2dc2bee2c436b9d2aef790d283d52.png)
模型结果导入excel
a1 = pd.DataFrame([i.split(',') for i in result.summary().tables[0].as_csv().replace(' ', '').split('\n')])
a2 = pd.DataFrame([i.split(',') for i in result.summary().tables[1].as_csv().replace(' ', '').split('\n')])
df_result = a1.append(a2)
train_res = train_data.误差.describe(percentiles=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.95,0.97]).rename('训练集误差').reset_index()
test_res = test_data.误差.describe(percentiles=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.95,0.97]).rename('测试集误差').reset_index()
df_res = pd.merge(train_res, test_res, on=['index'])
df_result = df_res.append(df_result)