預測與勝算

使用 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 進行回歸入門

尺度比較

Scale Are values easy to interpret? Are changes easy to interpret? Is precise?
Probability
Most likely outcome ✔✔
Odds
Log odds
使用 Python 中的 statsmodels 進行回歸入門

一起來練習吧!

使用 Python 中的 statsmodels 進行回歸入門

Preparing Video For Download...