본문 바로가기

ML & AI Theory

ROC, AUC Curve에 대해 : Receiver Operating Characteristic, Area Under the Curve

반응형

AUC-ROC Curve는 다양한 임계값에서 모델의 분류 성능에 대한 측정 그래프를 나타낼 수 있습니다. ROC곡선과 이에 기반한 AUC스코어는 이진 분류의 예측 성능 측정에서 중요하게 사용되는 지표입니다.  일반적으로 의학 분야에서 많이 사용되지만, ML의 이진 분류 모델의 예측 성능을 판단하는 중요한 평가 지표이기도 합니다.

 

ROC곡선과 이에 기반한 AUC스코어는 이진 분류의 예측 성능 측정에서 중요하게 사용되는 지표입니다.

 

1. ROC(Receiver Operation Characteristic): 모든 임계값에서 분류 모델의 성능을 보여주는 그래프가 됩니다. FPR을 0부터 1까지 변경하면서 TPR의 변화값을 구합니다. 

다시 설명하면, FPR(False ositive Rate)이 변할 때 TPR(True Positive Rate)이 어떻게 변하는지를 나타내는 곡선입니다. FPR을 X축으로, TPR을 Y축으로 잡으면 FPR의 변화에 따른 TPR의 변화가 곡선 형태로 나타납니다. ROC곡선이 가운데 직선에 가까울수록 성능이 떨어지는 것이며, 멀어질수록 성능이 뛰어난 것입니다.

2. AUC(Area Under the Curve): ROC Curve 아래 영역

 

AUC가 높다는 사실은 클래스를 구별하는 모델의 성능이 좋다는 것을 의미합니다.

ROC곡선은 TPR(True Positive Rate: 재현율=민감도)FPR(False Positive Rate)로 구성되어 있으며 TPR의 대응하는 지표로 TNR(True Negative Rate: 특이성)가 있습니다.

 

1.TPR은 실제값 Positive가 정확히 예측돼야 하는 수준을 의미 (질병이 있는 사람은 질병이 있는 것으로 양성 판정)

2.TNR은 실제값 Negative가 정확히 예측돼야 하는 수준을 의미 (질병이 없는 건강한 사람은 질병이 없는 것으로 음성 판정)

 

ML측면에서 우수한 분류 모델은 AUC가 1에 근접하고, 클래스를 분류하는 성능이 뛰어남을 의미하며 불량한 분류 모델은 AUC값이 0에 가깝고, 클래스를 분류하는 성능이 떨어짐을 의미합니다. 실제로는 AUC의 최소값은 0.5이므로, 이 경우는 모델의 클래스 분리 능력이 전혀 없음을 뜻합니다.

 

FPR은 FPR=1-특이도를 의미합니다. TPR과 FPR은 결국 반비례 관계에 있다고 할 수 있습니다.

 


AUC-ROC Summary

 

The worst AUROC is 0.5, and the best AUROC is 1.0. 

  • An AUROC of 0.5 (area under the red dashed line in the figure above) corresponds to a coin flip, i.e. a useless model.
  • An AUROC less than 0.7 is sub-optimal performance
  • An AUROC of 0.70 – 0.80 is good performance
  • An AUROC greater than 0.8 is excellent performance
  • An AUROC of 1.0 (area under the purple line in the figure above) corresponds to a perfect classifier

 

일반적으로 ROC곡선 자체는 FPR과 TPR의 변화 값을 보는 데 이용하며 분류의 성능 지표로 사용되는 것은 ROC곡선 면적에 기반한 AUC값으로 결정합니다. AUC값은 ROC곡선 밑의 면적을 구한 것으로서 일반적으로 1에 가까울수록 좋은 수치입니다. AUC수치가 커지혀면 FRP이 작은 상태에서 얼마나 큰 TPR을 얻을 수 있느냐가 관건입니다. 가운데 직선에서 멀어지고 왼쪽 상단 모서리 쪽으로 가파르게 곡선이 이동할수록 직사각형에 가까운 곡선이 되어 면적이 1에 가까워지는좋은 ROC AUC성능 수치를 얻게 됩니다.

train.csv
0.06MB

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_curve
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score, f1_score, roc_auc_score
import Common_Module.CMStat as CM
import Common_Module.CMPlot as CMPlot
import numpy as np

titanic_df = pd.read_csv('C:\\Users\\HANA\\PycharmProjects\\HANATOUR\\Pandas\\doit_pandas-master\\data\\train.csv')
# print(titanic_df.info())
y_titanic_df = titanic_df['Survived']
X_titanic_df = titanic_df.drop('Survived', axis=1)
X_titanic_df = CM.transform_features(X_titanic_df)
X_train, X_test, y_train, y_test = train_test_split(X_titanic_df, y_titanic_df, test_size=0.2, random_state=11)
# print(X_titanic_df)

lr_clf = LogisticRegression(max_iter=4000)
lr_clf.fit(X_train, y_train)
pred = lr_clf.predict(X_test)
pred_proba_class1 =lr_clf.predict_proba(X_test)[:,1]

fprs, tprs, threadholds = roc_curve(y_test, pred_proba_class1)
# print(fprs)
thr_index = np.arange(1, threadholds.shape[0],5)

print('Sample 추출을 위한 임계값 배열의 index 10개:', thr_index)
print('Sample 10개의 임계값: ', np.round(threadholds[thr_index], 2))
print('Sample 임계값 FPR: ', np.round(fprs[thr_index], 3))
print('Sample 임계값 TPR: ', np.round(tprs[thr_index], 3))

print(confusion_matrix(y_test, pred))
print("정확도: ", accuracy_score(y_test, pred))
print("정밀도: ", precision_score(y_test, pred))
print("재현율: ", recall_score(y_test, pred))
print(f1_score(y_test, pred))

CMPlot.roc_curve_plot(y_test, pred_proba_class1)

 

반응형