손실 함수 II

Python으로 설계하는 Machine Learning 워크플로

Dr. Chris Anagnostopoulos

Honorary Associate Professor

확률 점수

clf = GaussianNB().fit(X_train, y_train)
scores = clf.predict_proba(X_test)
array([[3.74717371e-07, 9.99999625e-01],
       [9.99943716e-01, 5.62841678e-05],
       ...,
       [9.99937502e-01, 6.24977552e-05]])
[s[1] > 0.5 for s in scores] == clf.predict(X_test)
Python으로 설계하는 Machine Learning 워크플로

확률 점수

임계값 거짓 양성 거짓 음성
0.0 178 0
0.25 66 17
0.5 35 37
0.75 13 57
1.0 0 72
Python으로 설계하는 Machine Learning 워크플로

ROC 곡선

FPR 대비 TPR 그래프에 왼쪽 위와 상단 경계에 가까워지는 곡선과 대각선이 표시됩니다.

fpr, tpr, thres = roc_curve(
    ground_truth, 
    [s[1] for s in scores])
plt.plot(fpr, tpr)
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
Python으로 설계하는 Machine Learning 워크플로

앞과 같이, FPR 대비 TPR 그래프에 왼쪽 위와 상단 경계에 가까워지는 곡선과 대각선이 표시됩니다.

Python으로 설계하는 Machine Learning 워크플로

여기에 GaussianNB의 추가 곡선이 표시됩니다. 전체 FPR 범위에서 AdaBoost보다 낮습니다.

Python으로 설계하는 Machine Learning 워크플로

무작위 숲(Random Forest)의 곡선이 추가로 표시됩니다. 작은 FPR에서는 GaussianNB보다 낮고, 큰 FPR에서는 더 높습니다.

Python으로 설계하는 Machine Learning 워크플로

AUC

clf = AdaBoostClassifier().fit(X_train, y_train)
scores_ab = clf.predict_proba(X_test)
roc_auc_score(ground_truth, [s[1] for s in scores_ab])
0.9999
Python으로 설계하는 Machine Learning 워크플로

비용 최소화

def my_scorer(y_test, y_est, cost_fp=10.0, cost_fn=1.0):
    tn, fp, fn, tp = confusion_matrix(y_test, y_est).ravel()
    return cost_fp*fp + cost_fn*fn
t_range = [0.0, 0.25, 0.5, 0.75, 1.0]
costs = [
   my_scorer(y_test, [s[1] > thres for s in scores]) for thres in t_range
]
[94740.0, 626.0, 587.0, 507.0, 2855.0]
Python으로 설계하는 Machine Learning 워크플로

사용 사례마다 다릅니다!

Python으로 설계하는 Machine Learning 워크플로

Preparing Video For Download...