
ROC는 이진 분류 문제에서 모델의 성능을 평가하기 위해 사용하는 그래프입니다.
이 그래프는 분류기의 민감도(Recall)와 특이도(Specificity)의 관계를 보여줍니다.
ROC 곡선은 모델이 분류 기준(Threshold)을 바꾸면서 FPR과 TPR이 어떻게 변하는지를 나타냅니다.
AUC는 ROC 곡선 아래의 면적을 의미합니다.
모델 성능 비교
Threshold 선택
불균형 데이터
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
# 데이터 생성 및 모델 학습
X, y = make_classification(n_samples=1000, n_features=20, random_state=42)
model = LogisticRegression()
model.fit(X, y)
y_score = model.predict_proba(X)[:, 1]
# ROC 곡선 및 AUC 계산
fpr, tpr, _ = roc_curve(y, y_score)
roc_auc = auc(fpr, tpr)
# 그래프 그리기
plt.figure(figsize=(8, 6))
plt.plot(fpr, tpr, label=f"ROC curve (AUC = {roc_auc:.2f})", color='darkorange', lw=2)
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")
plt.title("ROC Curve")
plt.legend(loc="lower right")
# 저장 및 출력
output_path = "/mnt/data/roc_curve_example.png"
plt.savefig(output_path)
plt.show()
output_path

니가 해