使用模型对象

使用 Python 中的 statsmodels 进行回归入门

Maarten Van den Broeck

Content Developer at DataCamp

.params 属性

from statsmodels.formula.api import ols
mdl_mass_vs_length = ols("mass_g ~ length_cm", data = bream).fit()
print(mdl_mass_vs_length.params)
Intercept   -1035.347565
length_cm      54.549981
dtype: float64
使用 Python 中的 statsmodels 进行回归入门

.fittedvalues 属性

拟合值:在原始数据集上的预测

print(mdl_mass_vs_length.fittedvalues)

或等价地

explanatory_data = bream["length_cm"]

print(mdl_mass_vs_length.predict(explanatory_data))
0      230.211993
1      273.851977
2      268.396979
3      399.316934
4      410.226930
    ...
30     873.901768
31     873.901768
32     939.361745
33    1004.821722
34    1037.551710
Length: 35, dtype: float64
使用 Python 中的 statsmodels 进行回归入门

.resid 属性

残差:实际响应值减去预测响应值

print(mdl_mass_vs_length.resid)

或等价地

print(bream["mass_g"] - mdl_mass_vs_length.fittedvalues)
0    11.788007
1    16.148023
2    71.603021
3   -36.316934
4    19.773070
   ...

残差(红色)是实际值(蓝点)与预测值(蓝线)之差

使用 Python 中的 statsmodels 进行回归入门

.summary()

mdl_mass_vs_length.summary()
                            OLS回归结果                            
==============================================================================
因变量:                   mass_g   R-squared:                       0.878
模型:                         OLS   Adj. R-squared:                  0.874
方法:                 最小二乘法   F-statistic:                     237.6
日期:                Thu, 29 Oct 2020   Prob (F-statistic):           1.22e-16
时间:                        13:23:21   Log-Likelihood:                -199.35
观测数:                        35   AIC:                             402.7
残差自由度:                    33   BIC:                             405.8
模型自由度:                     1                                         
协方差类型:               nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
<-----------------------------------------------------------------------------
Intercept  -1035.3476    107.973     -9.589      0.000   -1255.020    -815.676
length_cm     54.5500      3.539     15.415      0.000      47.350      61.750
==============================================================================
Omnibus:                        7.314   Durbin-Watson:                   1.478
Prob(Omnibus):                  0.026   Jarque-Bera (JB):               10.857
Skew:                          -0.252   Prob(JB):                      0.00439
Kurtosis:                       5.682   Cond. No.                         263.
使用 Python 中的 statsmodels 进行回归入门
                            OLS回归结果                            
==============================================================================
因变量:                   mass_g   R-squared:                       0.878
模型:                         OLS   Adj. R-squared:                  0.874
方法:                 最小二乘法   F-statistic:                     237.6
日期:                Thu, 29 Oct 2020   Prob (F-statistic):           1.22e-16
时间:                        13:23:21   Log-Likelihood:                -199.35
观测数:                        35   AIC:                             402.7
残差自由度:                    33   BIC:                             405.8
模型自由度:                     1                                         
协方差类型:               nonrobust                                         
使用 Python 中的 statsmodels 进行回归入门
                 coef    std err          t      P>|t|      [0.025      0.975]
<-----------------------------------------------------------------------------
Intercept  -1035.3476    107.973     -9.589      0.000   -1255.020    -815.676
length_cm     54.5500      3.539     15.415      0.000      47.350      61.750
==============================================================================
Omnibus:                        7.314   Durbin-Watson:                   1.478
Prob(Omnibus):                  0.026   Jarque-Bera (JB):               10.857
Skew:                          -0.252   Prob(JB):                      0.00439
Kurtosis:                       5.682   Cond. No.                         263.
使用 Python 中的 statsmodels 进行回归入门

开始练习吧!

使用 Python 中的 statsmodels 进行回归入门

Preparing Video For Download...