본문 바로가기
python/MLDL

GPT통해서 Metric 컨닝페이퍼 만들기

by 포잉띠 2024. 4. 26.

ROC Curve, Confusion Matrix 계산, 출력 예시코드

from sklearn.metrics import roc_curve, auc, confusion_matrix, ConfusionMatrixDisplay

# ROC Curve
fpr, tpr, thresholds = roc_curve(y_test, y_proba[:, 1], pos_label=1)
roc_auc = auc(fpr, tpr)

# Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=iris.target_names)

# ROC Curve vis
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (area = {roc_auc:.2f})')
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc="lower right")

# Confusion Matrix vis
plt.subplot(1, 2, 2)
disp.plot(cmap=plt.cm.Blues)
plt.title('Confusion Matrix')

 

precision, recall, F1-score 계산과 확인 예시코드

from sklearn.metrics import precision_score, recall_score, f1_score

# 성능 지표 계산
precision = precision_score(y_test, y_pred, average='macro')  # 모든 클래스에 대한 평균
recall = recall_score(y_test, y_pred, average='macro')
f1 = f1_score(y_test, y_pred, average='macro')

print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")
print(f"F1 Score: {f1:.2f}")

 

R 스코어 계산과 확인 예시코드

from sklearn.metrics import r2_score

# R² 스코어 계산
r2 = r2_score(y_test, y_pred)
print(f"R² Score: {r2:.2f}")