고객 이탈
Coddy Machine Learning 여정의 지도 학습 섹션에 포함된 레슨. 58개 중 58번째.
밀키트 서비스는 매주 상자를 배송합니다. subscribers.csv에는 고객 300명의 정보와 두 달 이내에 구독을 취소했는지 여부가 기록되어 있습니다. 배송을 건너뛴 횟수는 경고 신호 중 하나입니다.
팀은 고객이 떠나기 전에 이탈 위험이 있는 고객에게 전화하려고 합니다. 한 가지 징후만으로는 충분하지 않으므로, 랜덤 포레스트가 모든 징후를 종합해 판단합니다.
깊이를 조정해야 하며, ROC 곡선은 위험 점수가 취소하는 사람을 얼마나 잘 순위화하는지 보여 줍니다.
챌린지
어려움prep에는 전처리 단계가 들어 있고, 프로그램은 트리 깊이를 depths에 읽어 들입니다.
- 층화 추출을 적용해 테스트 데이터 1/4을 분리합니다.
random_state=0. - 훈련 데이터에서
prep와 트리 100개로 구성된 랜덤 포레스트(random_state=0)를 결합해depths를 대상으로 5겹 그리드 검색을 수행합니다. 가장 좋은 깊이와 교차 검증 정확도를 출력합니다. - 테스트 데이터에서 취소할 확률을 바탕으로 AUC와 전화 대상 목록, 즉 확률이 0.5 이상인 경우를 출력합니다.
- 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그러면 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")
# 아래에 코드를 작성하세요
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
지도 학습의 모든 레슨
직접 연습해 보세요: 온라인 Python 컴파일러