■ Key words
ㆍConfusion Matrix
ㆍ정확도(Accuracy), 정밀도(Precision), 재현율(Recall)
ㆍROC(Receiver Operating Characteristic) Curve, AUC(Area Under the Curve) Score
■ 주요내용
ㆍConfusion Matrix : 모델의 성능을 확인하기 위해 예측 × 실제를 2 × 2차원으로 나눈 matrix. TP/FP/FN/TN로 되어있다.
ㆍ정확도(Accuracy) : 모두 맞춘 경우(True Positive + True Negative)를 전체 표본 수(Total)로 나눈 값
ㆍ정밀도(Precision) : Positive로 예측한 경우 중 맞춘 비율(True positive ÷ (True positive + False Positive)
ㆍ재현율(민감도, Recall) : 실제 Positive 중에 맞춘 비율((True positive ÷ (True positive + False Negative)
ㆍF Beta Score : 정밀도(Precision)와 재현율(Recall)의 조화평균(Harmonic Mean). 0(최악) ~ 1(이상)의 값을 가진다.
β 값에 따라 정밀도와 재현율의 비중을 바꿀 수 있다.
- F0. 5-Measure (beta=0.5): More weight on precision, less weight on recall.
- F1-Measure (beta=1.0): Balance the weight on precision and recall.
- F2-Measure (beta=2.0): Less weight on precision, more weight on recall.
ㆍ임계값(Threshold) : Positive로 분류하는 기준값. 임계값이 높아지면 Positive로 분류하는 수가 적어지므로 FP가 줄어
정밀도는 증가하지만, 대신 FN이 증가하여 재현율은 떨어진다.
ㆍPR curve(Precision Recall curve, 정밀도-재현율 곡선) : 0 ~ 1사이의 모든 임계값에 따라 x축을 재현율, y축을 정밀도로 그린 그림
ㆍROC(Receiver Operating Characteristic) Curve : 모든 임계값에 따라 x 축을 (1-특이도)로 y 축을 민감도로 그린 그림.
이진분류문제에 사용 가능 / 다중분류문제에서는 이진분류문제로 변환(One Vs All)하여 적용 가능
- 1-특이도(위양성률 / FPR, False Positive Rate) : 실제 음성인 것 중 양성으로 잘못 예측한 것의 비율(1 - TNR)
- 민감도(Recall) : 실제 양성인 것 중 양성으로 잘 예측한 것의 비율
ㆍAUC(Area Under the Curve) : ROC Curve의 적분값
* PR curve AUC를 사용하는 경우
- 데이터가 불균형 할때
- 양성 클래스를 탐지하는 중요도가 음성 클래스를 탐지하는 중요도보다 높을때 (ex. 암 환자 진단)
* ROC curve AUC를 사용하는 경우
- 데이터가 균형일때
- 양성 클래스 탐지와 음성 클래스 탐지의 중요도가 비슷할 때 (ex. 개와 고양이 분류)
■ Session note
ㆍTFR을 잘 안 보는 이유는 1. 1에서 TPR을 빼면 나오고, 2. 우리는 분류에서 Positive를 더 궁금해 하기 때문이다.
■ 주요함수
ㆍConfusion Matrix
from sklearn.metrics import plot_confusion_matrix
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
pcm = plot_confusion_matrix(pipe, X_val, y_val,
cmap=plt.cm.Blues,
ax=ax);
plt.title(f'Confusion matrix, n = {len(y_val)}', fontsize=15)
plt.show()
ㆍSKlearn.metrics.classification_report : Confusion Matrix를 토대로 Accuracy/Recall/Precision/F1 score 계산
from sklearn.metrics import classification_report
print(classification_report(y_val, y_pred))
ㆍpredict_proba : 각 클래스별 확률
ㆍTarget의 항목별 확률분포 확인 히스토그램
pipe.classes_ # Target의 class 확인
pipe.predict(X_val) # Feature의 예측값 확인
pipe.predict_proba(X_val) # Feature 예측값의 각 class별 확률 확인
threshold = 0.5 # Threshold의 기본값은 0.5임
y_pred_proba = pipe.predict_proba(X_val)[:, 1] # Feature의 예측값의 확률별 분포 확인
import seaborn as sns
sns.displot(y_pred_proba);
ㆍIpywidgets을 이용하여 Threshold 변화에 따른 각 값의 변화를 확인할 수 있다.
from ipywidgets import interact, fixed
def explore_threshold(y_true, y_pred_proba, threshold=0.5):
y_pred = y_pred_proba >= threshold
vc = pd.Series(y_pred).value_counts()
ax = sns.histplot(y_pred_proba, kde=True)
ax.axvline(threshold, color='red')
ax.set_title(f'# of target, 1={vc[1]}, 0={vc[0]}')
plt.show()
print(classification_report(y_true, y_pred))
interact(explore_threshold,
y_true=fixed(y_val),
y_pred_proba=fixed(y_pred_proba),
threshold=(0, 1, 0.01));
ㆍTarget과 예측확률을 Data Frame으로 만든 후 해당하지 않을(0) 확률 순으로 정렬
pred_proba = pd.DataFrame({
'y_val': y_val,
'pred_proba': y_pred_proba})
top = pred_proba.sort_values(by='pred_proba', ascending=True)[:4500]
display(top)
ㆍTarget 확인을 통해 이들 중 False Negative 확인하기
vc = top['y_val'].value_counts()
display(vc)
ㆍSKlearn ROC Curve
from sklearn.metrics import roc_curve
# roc_curve(타겟값, prob of 1)
fpr, tpr, thresholds = roc_curve(y_val, y_pred_proba)
roc = pd.DataFrame({
'FPR(Fall-out)': fpr,
'TPRate(Recall)': tpr,
'Threshold': thresholds
})
display(roc)
plt.scatter(fpr, tpr)
plt.title('ROC curve')
plt.xlabel('FPR(Fall-out)')
plt.ylabel('TPR(Recall)');
ㆍThreshold 최대값의 인덱스, np.argmax() : 재현률을 최대화 하면서 위양성률은 최소화 하는 최적의 임계값
optimal_idx = np.argmax(tpr - fpr)
optimal_threshold = thresholds[optimal_idx]
print('idx:', optimal_idx, ', threshold:', optimal_threshold)
plt.plot(tpr-fpr);
ㆍ해당 최적의 임계값을 모델의 예측치에 적용 - classification report 생성
y_pred_optimal = y_pred_proba >= optimal_threshold
print(classification_report(y_val, y_pred_optimal))
ㆍAUC Score(ROC Curve의 적분값) 계산
from sklearn.metrics import roc_auc_score
auc_score = roc_auc_score(y_val, y_pred_proba)
auc_score
■ Reference
ㆍConfusion Matrix : https://www.youtube.com/watch?v=wpp3VfzgNcI
ㆍPrecisio, Recall & F-Measure : https://www.youtube.com/watch?v=j-EB6RqqjGI
ㆍReceiver Operating Characteristic : https://en.wikipedia.org/wiki/Receiver_operating_characteristic / http://www.navan.name/roc/
ㆍ산술/기하/조화평균 : https://wikidocs.net/23088
ㆍ모델 평가하기 :
ㆍFbeta-Measurement : https://machinelearningmastery.com/fbeta-measure-for-machine-learning/
ㆍTarget Encoding : https://www.kaggle.com/ryanholbrook/target-encoding
ㆍConfusion Matrix : https://yamalab.tistory.com/50
'Data Science > 2. 기계학습' 카테고리의 다른 글
[n232]Data Wrangling (0) | 2021.06.23 |
---|---|
[n231]Choose Your ML Problem (0) | 2021.06.22 |
[n222]Random Forests (0) | 2021.06.21 |
[n221]결정트리(Decision Trees) (0) | 2021.06.21 |
[n214]Logistic Regression (0) | 2021.06.21 |