์ ํ๋, ์ ๋ฐ๋, ์ฌํ์จ... ์ซ์ ํ๋๋ก๋ง ๋ชจ๋ธ์ ํ๊ฐํ๊ธฐ์ ๋ถ์กฑํ ๋๊ฐ ์๋ค.
๋ชจ๋ธ์ด ์ผ๋ง๋ ์๊ฑฑํ๊ฒ(Threshold) ํ์ ํ๋๋์ ๋ฐ๋ผ ์ฑ๋ฅ์ด ๋ฌ๋ผ์ง๊ธฐ ๋๋ฌธ.
ROC ๊ณก์ ์ "๋ง์ถ ๊ฒ(์ด๋)"๊ณผ "ํ๋ฆฐ ๊ฒ(๋น์ฉ)"์ ํธ๋ ์ด๋์คํ(Trade-off)๋ฅผ ๋ณด์ฌ์ค๋ค
Y์ถ: TPR (True Positive Rate) ์ฌํ์จ(Recall)
์ค์ ์์ฑ(Positive)์ธ ์ฌ๋๋ค ์ค ๋ชจ๋ธ์ด "์์ฑ์
๋๋ค!"๋ผ๊ณ ๋ง๊ฒ ์ฐพ์ ๋น์จ.
Ex) ์ค์ ์ ํ์๋ฅผ ์์ด๋ผ๊ณ ์ง๋จํ ๋น์จ (๋์์๋ก ์ข์)
X์ถ: FPR (False Positive Rate)
์ค์ ์์ฑ(Negative)์ธ ์ฌ๋๋ค ์ค ๋ชจ๋ธ์ด "์์ฑ์
๋๋ค.."๋ผ๊ณ ํ๋ฆฌ๊ฒ ๊ฒฝ๊ณ ํ ๋น์จ.
Ex) ์ ์์ธ์ ์์ด๋ผ๊ณ ์ค์งํ ๋น์จ (๋ฎ์์๋ก ์ข์)
์ข์ ๋ชจ๋ธ์ ์ ํ์๋ ๋ค ์ฐพ์ผ๋ฉด์(TPR ), ์ ์์ธ์๊ฒ๋ ์ค์ง์ ์ ํ๋(FPR ) ๋ชจ๋ธ์ด๋ค. ๋ฐ๋ผ์ ๊ทธ๋ํ๊ฐ ์ข์๋จ(0, 1) ์ ๋ถ์์๋ก ์ข๋ค.

๋ชจ๋ธ์ ๋ด๋ถ์ ์ผ๋ก ํ๋ฅ (0.1 ~ 0.9)์ ๊ณ์ฐํ๊ณ , ํน์ฑ ๊ธฐ์ค(Threshold, ๋ณดํต 0.5)๋ฅผ ๋์ผ๋ฉด 1, ์๋๋ฉด 0์ผ๋ก ๋ถ๋ฅํ๋ค.
์๊ณ๊ฐ์ ๋ฎ์ถ๋ฉด (0.5 0.1): "์์ฌ๋๋ฉด ๋ฌด์กฐ๊ฑด ์์ด๋ผ๊ณ ํด!"
์ ํ์๋ฅผ ๊ฑฐ์ ๋ค ์ฐพ์๋. (TPR ์ฆ๊ฐ ๐)
ํ์ง๋ง ๋ฉ์ฉกํ ์ฌ๋๋ ์์ด๋ผ๊ณ ์ค์ง. (FPR ์ฆ๊ฐ ๐)
์๊ณ๊ฐ์ ๋์ด๋ฉด (0.5 0.9): "ํ์คํ ๋๋ง ์์ด๋ผ๊ณ ํด!"
์ค์ง์ด ๊ฑฐ์ ์๋ค. (FPR ๊ฐ์ ๐)
ํ์ง๋ง ์ด๊ธฐ ์ ํ์๋ฅผ ๋์น ์ ์์ต๋๋ค. (TPR ๊ฐ์ ๐)
ROC Curve๋ ์ด ๋ชจ๋ ์๊ณ๊ฐ์์์ ์ ๋ค์ ์ด์ ์ .
AUC = 1.0: ์๋ฒฝํ ๋ชจ๋ธ (FPR=0์ผ ๋ TPR=1)
AUC = 0.5: ์์ ๋๋ค ๋ชจ๋ธ (๋์ ๋์ง๊ธฐ ์์ค, ๊ทธ๋ํ์ ๋๊ฐ์ )
ํด์: ์ผ๋ฐ์ ์ผ๋ก 0.8 ์ด์์ด๋ฉด ํ๋ฅญํ ๋ชจ๋ธ๋ก ํ๊ฐ.
Scikit-learn์
roc_curve์aucํจ์๋ฅผ ์ฌ์ฉ.
์ฃผ์ํ ์ :roc_curveํจ์์๋ ์ต์ข ์์ธก๊ฐ(0, 1)์ด ์๋๋ผ ์์ธก ํ๋ฅ (predict_proba)์ ๋ฃ์ด์ผ ๋งค๋๋ฌ์ด ๊ณก์ ์ด ๊ทธ๋ ค์ง๋ค.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_curve, auc
# ๋ฐ์ดํฐ ๋ก๋ (Pima Indians Diabetes ์์ ๊ฐ์ )
df = pd.read_csv('data/pima-indians-diabetes.data.csv')
x_data = df.iloc[:, :-1].values
y_data = df.iloc[:, -1].values
# ๋ฐ์ดํฐ ๋ถํ
x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.3, stratify=y_data, random_state=42)
# ๋ชจ๋ธ ํ์ต
model = LogisticRegression(max_iter=500)
model.fit(x_train, y_train)
# ์ค์: predict()๊ฐ ์๋๋ผ predict_proba()๋ฅผ ์ฌ์ฉ!
# [:, 1]์ ๋ ์ด๋ธ์ด 1(์์ฑ)์ผ ํ๋ฅ ๋ง ๊ฐ์ ธ์จ๋ค๋ ๋ป
pred_proba = model.predict_proba(x_test)[:, 1]
# roc_curve(์ค์ ๊ฐ, ์์ธกํ๋ฅ ๊ฐ)
# ๋ฐํ๊ฐ: fpr, tpr, ์๊ณ๊ฐ๋ค
fpr, tpr, thresholds = roc_curve(y_test, pred_proba)
# AUC ๊ณ์ฐ
auc_val = auc(fpr, tpr)
print(f"AUC Score: {auc_val:.4f}")
# AUC Score: 0.8375
plt.figure(figsize=(6, 6))
# ROC Curve ๊ทธ๋ฆฌ๊ธฐ
plt.plot(fpr, tpr, color='blue', lw=2, label=f'ROC curve (AUC = {auc_val:.2f})')
# ๊ธฐ์ค์ ๊ทธ๋ฆฌ๊ธฐ (Random Classifier, AUC=0.5)
plt.plot([0, 1], [0, 1], color='gray', linestyle='--', label='Random')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate (FPR)')
plt.ylabel('True Positive Rate (TPR)')
plt.title('Receiver Operating Characteristic (ROC)')
plt.legend(loc="lower right")
plt.grid(True)
plt.show()
