RF 모델의 성능 지표
이전 연습 문제에서는 랜덤 포레스트 모델의 정확도를 계산했어요. 하지만 사기 탐지의 경우 accuracy는 오해를 부를 수 있습니다. 사기 데이터처럼 클래스 불균형이 큰 상황에서는 AUROC 곡선이 서로 다른 분류기를 비교할 때 더 신뢰할 수 있는 성능 지표예요. 또한 classification report는 모델의 정밀도(precision)와 재현율(recall)을 알려주고, confusion matrix는 실제로 몇 건의 사기 사례를 올바르게 예측했는지를 보여줍니다. 이제 이 성능 지표들을 구해 보겠습니다.
이전 연습 문제의 같은 랜덤 포레스트 모델을 계속 사용합니다. model = RandomForestClassifier(random_state=5)로 정의된 모델은 이미 학습 데이터에 적합(fit)되어 있으며, X_train, y_train, X_test, y_test가 준비되어 있어요.
이 연습은 강의의 일부입니다
Python으로 배우는 사기 탐지
연습 안내
sklearn.metrics에서 classification report, confusion matrix, ROC score를 임포트하세요.- 학습된 랜덤 포레스트
model에서 이진 예측값을 얻으세요. predict_proba()함수를 실행해 예측 확률을 얻으세요.y_test와predicted를 비교해 classification report와 confusion matrix를 구하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Import the packages to get the different performance metrics
from sklearn.metrics import ____, ____, ____
# Obtain the predictions from our random forest model
predicted = model.____(X_test)
# Predict probabilities
probs = ____.____(X_test)
# Print the ROC curve, classification report and confusion matrix
print(____(y_test, probs[:,1]))
print(____(____, predicted))
print(____(____, ____))