Menu
CoddyTech

顧客離れ

CoddyのMachine Learningジャーニー「教師あり学習」セクションの一部。レッスン 58/58。

ミールキットの定期配送サービスでは、毎週ボックスを配送します。subscribers.csvには300人の顧客のデータと、2か月以内に解約したかどうかが記録されています。ボックスの受け取りをスキップすることは、解約の兆候の1つです。

過去3か月間にスキップしたボックス数別の解約率:スキップなしは15%、1回は36%、2回は60%、3回は61%、4回以上は86%

チームは、顧客が離れてしまう前に、離れるリスクのある顧客に電話をかけたいと考えています。兆候は1つだけでは不十分なので、フォレストがすべての兆候を考慮します。

その深さは調整が必要で、ROC曲線によって、リスクスコアが解約する人をどれだけ適切に順位付けできるかがわかります。

challenge icon

チャレンジ

難しい

prepには各処理の流れが格納されています。プログラムは木の深さをdepthsに読み込みます。

  1. 層化したテストデータを4分の1に分割します。random_state=0を指定します。
  2. 訓練データで、prepと100本の木からなるランダムフォレスト(random_state=0)を組み合わせ、depthsを対象に5分割交差検証でグリッドサーチします。最適な深さとCV精度を出力します。
  3. テストデータでの解約確率から、AUCと架電リスト(確率が0.5以上)を出力します。
  4. ROC曲線をforestとしてプロットし、破線のcoin toss対角線と凡例を追加して表示します。

別のサービスで、深さを3、5にした場合:

Best depth: 3 (CV accuracy 0.761)
Test AUC: 0.802
Call list: 21 customers, 15 of them really cancelled

「Who Will Cancel?」というタイトルのROC曲線。実線のforest曲線は破線のcoin toss対角線を大きく上回って急上昇し、凡例が付いている

その後、Coddyがあなたの曲線を説明します。

自分で試してみよう

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import make_pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_curve, roc_auc_score

subscribers = pd.read_csv("subscribers.csv")
numbers = ["months_subscribed", "boxes_skipped", "complaints", "monthly_price", "delivery_rating"]
text = ["plan", "channel"]
X = subscribers[numbers + text]
y = subscribers["churned"]

# Prepare the Data と同じ処理経路
number_steps = make_pipeline(SimpleImputer(strategy="median"), StandardScaler())
text_steps = make_pipeline(SimpleImputer(strategy="constant", fill_value="unknown"), OneHotEncoder())
prep = ColumnTransformer([("numbers", number_steps, numbers), ("words", text_steps, text)])

# 試す木の深さ
n = int(input())
depths = []
for i in range(n):
    depths.append(int(input()))

fig, ax = plt.subplots()
ax.set_title("Who Will Cancel?")
ax.set_xlabel("False positive rate")
ax.set_ylabel("Recall")

# 以下にコードを書いてください
quiz icon腕試し

このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。

教師あり学習のすべてのレッスン

自分で練習してみよう: Pythonオンラインコンパイラ