본문 바로가기
인공지능/Machine Learning

[ML] Evaluation / Confusion Matrix

by 유일리 2022. 10. 17.

The method of evaluation will depend on whether it is a classification or regression model.

 - In the case of classification, the common evaluation methods are the confusion matrix, classification report, and accuracy score.

  • Confusion Matrix
  • Classification Report

Confusion Matrix (오차행렬)

Training 을 통한 Prediction 성능을 측정하기 위해 예측 value와 실제 value를 비교하기 위한 표이다. 즉, 지도 학습으로 훈련된 분류 알고리즘의 성능을 시각화 할 수 있는 표이다. 행렬의 각 행은 예측 된 클래스의 인스턴스를 나타내며 각 열은 실제 클래스의 인스턴스를 나타낸다 (또는 그 반대).

Actual은 실제값, Predicted는 예측값을 의미하며, T 는 TRUE, F는 FALSE, P는 POSITIVE, N은 NEGATIVE 를 의미한다. 즉, TP와 TN는 실제값을 맞게 예측한 부분이며, FP와 FN은 실제값과 다르게 예측한 부분을 의미한다.

  • Parameters

• y_true: array-like of shape (n_samples,)

              Ground truth (correct) target values.

• y_pred: array-like of shape (n_samples,)

               Estimated targets as returned by a classifier.

• Labels: array-like of shape (n_classes), default=None

               List of labels to index the matrix. This may be used to reorder or select a subset of labels.

               If None is given, those that appear at least once in y_true or y_pred are used in sorted order.

• sample_weight: array-like of shape (n_samples,), default=None

              Sample weights

 

  • Returns

• C : nd array of shape (n_classes, n_classes)

        Confusion matrix whose i-th row and j-th column entry indicates the number of samples with true label being i-th class and predicted label being j-th class.

 

Confusion Matrix Example #1
from sklearn.metrics import confusion_matrix
y_true = [2, 0, 2, 2, 0, 1]
y_pred = [0, 0, 2, 2, 0, 2]
confusion_matrix(y_true, y_pred)

우선 나올 수 있는 결과값이 3개이기 때문에 3x3 행렬이다. 첫번째 True 0 Predict 0 칸이 2인 이유는 실제로 0이여야 하는데 0이 나온 경우가 2개이기 때문이다. 다음으로 True 0 Predict 1 칸이 0인 이유는 실제로 0이여야 하는데 1이 나온 경우가 없기 때문이다. 이렇게 총 9칸을 채워준다. 

Confusion Matrix Example #2
y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
confusion_matrix(y_true, y_pred, labels=["ant", "bird", "cat"])

#1의 예제 0,1,2 숫자를 ant, bird, cat으로 바꾸어준 상황이다. 결과는 똑같다.

https://github.com/erica00j/machinelearning/blob/main/confusion_matrix.ipynb

 

GitHub - erica00j/machinelearning

Contribute to erica00j/machinelearning development by creating an account on GitHub.

github.com

 

댓글