拟合线性回归

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

Maarten Van den Broeck

Content Developer at DataCamp

直线由两点决定

截距

当 $x$ 为 0 时的 $y$ 值。

斜率

$x$ 每增加 1,$y$ 增加的量。

方程

$y = \text{intercept} + \text{slope} * x$

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

估计截距

总支付额与理赔次数的散点图及线性趋势线。理赔次数增加时,支付额线性上升。

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

估计截距

总支付额与理赔次数的散点图,标注趋势线与 y 轴的交点。

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

估计截距

总支付额与理赔次数的散点图,标注理赔次数为 0 时的取值。

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

估计斜率

总支付额与理赔次数的散点图,趋势线上两点:1500克朗、40次理赔;3500克朗、100次理赔。

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

估计斜率

总支付额与理赔次数的散点图,标注两点的支付差:3500克朗减去1500克朗等于2000克朗。

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

估计斜率

总支付额与理赔次数的散点图,标注两点的理赔次数差:100次减去40次等于60次。

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

估计斜率

总支付额与理赔次数的散点图,标注差值之比:2000除以60约等于33。

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

运行模型

from statsmodels.formula.api import ols

mdl_payment_vs_claims = ols("total_payment_sek ~ n_claims", data=swedish_motor_insurance)
mdl_payment_vs_claims = mdl_payment_vs_claims.fit()
print(mdl_payment_vs_claims.params)
Intercept    19.994486
n_claims      3.413824
dtype: float64
使用 Python 中的 statsmodels 进行回归入门

解释模型系数

Intercept    19.994486
n_claims      3.413824
dtype: float64

方程

$\text{total\_payment\_sek} = 19.99 + 3.41 * \text{n\_claims}$

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

Ayo berlatih!

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

Preparing Video For Download...