python数据分析——OLS的summary保存为pandas下的dataframe

思路先保存成html,再用pandas读取html(今天刚知道pandas可以读取html),下面的results_summary就是OLS的summary()得到的对象

results_as_html = results_summary.tables[1].as_html()
pd.read_html(results_as_html, header=0, index_col=0)[0]
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我们先来看使用Python-PandasOLS工具。 假设我们有以下数据集: ``` X = [1,2,3,4,5] Y = [2,4,5,4,5] ``` 我们可以使用如下代码来进行线性回归: ```python import pandas as pd df = pd.DataFrame({'x': X, 'y': Y}) X = df[['x']] Y = df['y'] import statsmodels.api as sm X = sm.add_constant(X) model = sm.OLS(Y,X).fit() predictions = model.predict(X) print(model.summary()) ``` 运行结果如下: ``` OLS Regression Results ============================================================================== Dep. Variable: y R-squared: 0.300 Model: OLS Adj. R-squared: 0.100 Method: Least Squares F-statistic: 1.500 Date: Mon, 09 Aug 2021 Prob (F-statistic): 0.311 Time: 15:35:22 Log-Likelihood: -5.2181 No. Observations: 5 AIC: 14.44 Df Residuals: 3 BIC: 13.78 Df Model: 1 Covariance Type: nonrobust ============================================================================== coef std err t P>|t| [0.025 0.975] ------------------------------------------------------------------------------ const 2.2000 0.788 2.791 0.061 -0.148 4.548 x 0.6000 0.490 1.225 0.311 -1.207 2.407 ============================================================================== Omnibus: nan Durbin-Watson: 2.200 Prob(Omnibus): nan Jarque-Bera (JB): 0.357 Skew: 0.000 Prob(JB): 0.836 Kurtosis: 1.673 Cond. No. 6.00 ============================================================================== Notes: [1] Standard Errors assume that the covariance matrix of the errors is correctly specified. [2] The condition number is large, 6. This might indicate that there are strong multicollinearity or other numerical problems. ``` 可以看到,我们得到了回归结果的摘要统计信息。 接下来我们看一下如何使用Scikit-Learn工具。 ```python from sklearn.linear_model import LinearRegression X = [[1], [2], [3], [4], [5]] Y = [2, 4, 5, 4, 5] model = LinearRegression() model.fit(X, Y) print('Coefficients: ', model.coef_) print('Intercept: ', model.intercept_) ``` 运行结果如下: ``` Coefficients: [0.6] Intercept: 2.2 ``` 同样得到了回归系数和截距的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

量化橙同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值