개발공부

sklearn 라이브러리의 분류 결과표 Confusion Matrix (분류 문제의 인공지능 성능 평가 방법) 본문

Python/Machine Learning

sklearn 라이브러리의 분류 결과표 Confusion Matrix (분류 문제의 인공지능 성능 평가 방법)

mscha 2022. 5. 9. 20:06

분류 결과표 (Confusion Matrix)

타겟의 원래 클래스와 모형이 예측한 클래스가 일치하는지를 갯수로 센 결과를 표로 나타낸 것

정답 클래스는 행(row)으로 예측한 클래스는 열(column)로 나타낸다.

 

두개의 클래스로 분류할 때

이진 분류 결과표(Binary Confusion Matrix)

클래스가 0과 1 두종류 밖에 없는 이진 분류의 경우

일반적으로 클래스 이름을 양성(Positive)과 음성(Negative)로 표시한다.

 

sklearn 라이브러리를 이용한 confusion metrix

>>> from sklearn.metrics import confusion_matrix, accuracy_score, classification_report

#정답 라벨인 y_test와 학습한 결과로 얻은 예측 값 y_pred 이용
>>> cm = confusion_matrix(y_test, y_pred)
>>> cm
array([[51,  4],
       [ 8, 17]], dtype=int64)
       
# 정확도 직접계산
>>> (51 + 17) / cm.sum() 
0.85

# 정확도 함수이용
>>> accuracy_score(y_test, y_pred)
0.85

>>> print(classification_report(y_test, y_pred))
              precision    recall  f1-score   support

           0       0.86      0.93      0.89        55
           1       0.81      0.68      0.74        25

    accuracy                           0.85        80
   macro avg       0.84      0.80      0.82        80
weighted avg       0.85      0.85      0.85        80