[ML] confusion matrix (오차 행렬), precision, recall

Woong·2023년 8월 30일
0

Python / Machine Learning

목록 보기
16/20

confusion matrix (오차 행렬)

  • True Positive, True Negative, False Positive, False Negative 별 갯수를 적은 행렬

  • confusion_matrix 함수를 통한 예제
from sklearn.metrics import confusion_matrix

pipe_svc.fit(X_train, y_train)
y_pred = pipe_svc.predict(X_test)
confmat = confusion_matrix(y_true=y_test, y_pred=y_pred)
print(confmat)
[[71  1]
 [ 2 40]]

ERR, ACC

  • ECC (예측 오차) : 전체 중 잘못 분류된 비율

    • ERR=FP+FNFP+FN+TP+TNERR = \frac{FP+FN}{FP+FN+TP+TN}
  • ACC (정확도): 전체 중 올바르게 분류된 비율

    • ACC=TP+TNFP+FN+TP+TN=1ERRACC=\frac{TP+TN}{FP+FN+TP+TN}=1-ERR

TPR, FPR, PRE, REC

  • TPR(True Positive Rate) : 진짜 양성 비율

    • REC (재현율), recall 이라고도 표현
    • positive 중 올바르게 분류한 비율
    • TPR=REC=TPP=TPFN+TPTPR=REC=\frac{TP}{P}=\frac{TP}{FN+TP}
  • FPR (False Positive Rate) : 거짓 양성 비율

    • negative 중 positive 라고 잘못 분류한 비율
    • FPR=FPN=FPFP+TNFPR=\frac{FP}{N}=\frac{FP}{FP+TN}
  • PRE (정확도. 정밀도)

    • precision
    • positive 로 판별한 항목 중 진짜 positive 비율
    • PRE=TPTP+FPPRE=\frac{TP}{TP+FP}

F1-score

  • PRE 와 REC 를 조합한 점수
    • F1=2PRE×RECPRE+RECF1=2\frac{PRE \times REC}{PRE+REC}
  • scikit-learn 의 metrics 를 통해 사용
from sklearn.metrics import precision_score, recall_score, f1_score

print('재현율(TRP=REC): %.3f' % recall_score(y_true=y_test, y_pred=y_pred))
print('정밀도(PRE): %.3f' % precision_score(y_true=y_test, y_pred=y_pred))
print('F1: %.3f' % f1_score(y_true=y_test, y_pred=y_pred))

ROC (Receiver Operating Characteristic)

  • 분류기 임계값을 바꾸어가면 FPR, TPR 점수 계산
  • ROC 곡선의 아래 면적 ROC AUC(ROC Area Under the Curve) 를 계산해 분류 모델의 성능을 계산
    • roc_curve, roc_auc_score 함수로 ROC, ROC AUC 계산

reference

  • 서적 '머신러닝 교과서 with 파이썬, 사이킷런, 텐서플로 개정 3판'

0개의 댓글