재현율이 더 중요한 경우
- positive를 negative로 잘못 판단
- FN ▼
- 암 판정 / 환자를 정상이라고 함 >> FN 낮추는데 집중 >> 재현율 중요
정밀도가 더 중요한 경우
- negative를 positive로 잘못 판단
- FP ▼
- 스팸메일 판정 / 업무메일을 스팸이라고 함 >> FP 낮추는데 집중 >> 정밀도 중요
predict_proba : 분류에서만 사용가능 / class별 확률 반환
np.where(조건, 1, 0) : 조건에 임계값 조정해서 사용
tree.predict_proba(X_train[:3])
>>결과값
array([[0.99173554, 0.00826446],
[0.96610169, 0.03389831],
[0.98695652, 0.01304348]])
#positive(1)의 확률
rfc.predict_proba(X_train[:3])[:,1]
>>결과값
array([0.06722102, 0.42116418, 0.05364828])
#임계값 조정
np.where(pp> 0.5,1,0)
# 모델이 추론한 positive의 확률 조회
#DecisionTree
pos_test_tree = tree.predict_proba(X_test)[:, 1]
#from sklearn.metrics import precision_recall_curve
result = precision_recall_curve(y_test, pos_test_tree) #(정답, pos 확률)
= precision_list, recall_list, thresh_list
# 임계점을 변경
pred_test_06 = np.where(pos_test_tree >0.6, 1,0)
np.unique(pred_test_06, return_counts=True)
#from sklearn.metrics import precision_recall_curve, PrecisionRecallDisplay, average_precision_score
#test set 검증
# 각 모델이 추정한 positive 확률 조회
pos_test_tree = tree.predict_proba(X_test)[:,1]
pos_test_rfc = rfc.predict_proba(X_test)[:,1]
#ap_score #average_precision_score(y정답, positive확률)
ap_score = average_precision_score(y_test, pos_test_tree)
ap_score_rfc = average_precision_score(y_test, pos_test_rfc)
#한 그래프에 그리기
ax = plt.gca()
disp_tree = PrecisionRecallDisplay(precision_list1,
recall_list1,
average_precision=ap_score, #ap_score을 보여준다.
estimator_name="DecisionTree")
disp_rfc = PrecisionRecallDisplay(precision_list2,
recall_list2,
average_precision=ap_score_rfc, #ap_score을 보여준다.
estimator_name="RandomForest")
disp_tree.plot(ax=ax)
disp_rfc.plot(ax=ax)
plt.title("Precision Recall Curve")
plt.show()
cf) Positive 임계값 변경 > FPR & TPR(recall)비례해서 변화
#from sklearn.metrics import roc_curve, RocCurveDisplay, roc_auc_score
fpr_list1, tpr_list1, thresh_list1 = roc_curve(y_test, pos_test_tree) #(y정답, pos확률)
auc = roc_auc_score(y_test, pos_test_tree)
disp_tree = RocCurveDisplay(fpr=fpr_list1, tpr=tpr_list1,
roc_auc = auc,
estimator_name = 'DecisionTree')
cf) 안쓸때 _ 사용(임계값 사용 X)
fpr1, tpr1, _ = roc_curve(y_test, pos_test_tree)