预测与赔率

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

Maarten Van den Broeck

Content Developer at DataCamp

regplot() 的预测

sns.regplot(x="time_since_last_purchase",
            y="has_churned",
            data=churn,
            ci=None,
            logistic=True)

plt.show()

流失与上次购买间隔的散点图,带逻辑回归趋势线。

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

生成预测

mdl_recency = logit("has_churned ~ time_since_last_purchase",
                    data = churn).fit()


explanatory_data = pd.DataFrame( {"time_since_last_purchase": np.arange(-1, 6.25, 0.25)})
prediction_data = explanatory_data.assign( has_churned = mdl_recency.predict(explanatory_data))
使用 Python 中的 statsmodels 进行回归入门

添加点预测

sns.regplot(x="time_since_last_purchase",
            y="has_churned",
            data=churn,
            ci=None,
            logistic=True)

sns.scatterplot(x="time_since_last_purchase",
                y="has_churned",
                data=prediction_data,
                color="red")

plt.show()

流失与上次购买间隔的散点图,带逻辑回归趋势线。图中标注 predict() 的结果,完全贴合趋势线。

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

获取最可能结果

prediction_data = explanatory_data.assign(
    has_churned = mdl_recency.predict(explanatory_data))

prediction_data["most_likely_outcome"] = np.round(prediction_data["has_churned"])
使用 Python 中的 statsmodels 进行回归入门

可视化最可能结果

sns.regplot(x="time_since_last_purchase",
            y="has_churned",
            data=churn,
            ci=None,
            logistic=True)

sns.scatterplot(x="time_since_last_purchase",
                y="most_likely_outcome",
                data=prediction_data,
                color="red")

plt.show()

流失与上次购买间隔的散点图,带逻辑回归趋势线。图中标注最可能结果:间隔短时最可能为未流失;间隔长时最可能为流失。

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

赔率(Odds)

"赔率"是事件发生的概率除以其不发生的概率。

$$ \text{odds} = \frac{\text{probability}}{(1 - \text{probability)}} $$

$$ \text{odds} = \frac{0.25}{(1 - 0.25)} = \frac{1}{3} $$

赔率与概率的折线图。随着概率趋近于1,曲线渐近于无穷大。

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

计算赔率

prediction_data["odds"] = prediction_data["has_churned"] / 
                            (1 - prediction_data["has_churned"])

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

可视化赔率

sns.lineplot(x="time_since_last_purchase",
             y="odds",
             data=prediction_data)


plt.axhline(y=1, linestyle="dotted")
plt.show()

赔率与上次购买间隔的折线图,并在赔率=1处有一条线。间隔短时最可能为未流失;随着间隔增加,流失的赔率上升,最高约为未流失的5倍。

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

可视化对数赔率

sns.lineplot(x="time_since_last_purchase",
             y="odds",
             data=prediction_data)

plt.axhline(y=1,
            linestyle="dotted")
plt.yscale("log")

plt.show()

赔率与上次购买间隔的折线图,并在赔率=1处有一条线。y 轴为对数刻度,使赔率线呈线性。

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

计算对数赔率

prediction_data["log_odds"] = np.log(prediction_data["odds"])
使用 Python 中的 statsmodels 进行回归入门

汇总所有预测

time_since_last_prchs has_churned most_likely_rspns odds log_odds
0 0.491 0 0.966 -0.035
2 0.623 1 1.654 0.503
4 0.739 1 2.834 1.042
6 0.829 1 4.856 1.580
... ... ... ... ...
使用 Python 中的 statsmodels 进行回归入门

比较各尺度

尺度 数值易于解读? 变化易于解读? 精确吗?
概率
最可能结果 ✔✔
赔率
对数赔率
使用 Python 中的 statsmodels 进行回归入门

Passons à la pratique !

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

Preparing Video For Download...