Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

Passion, Grace & Fire.

sklearn confusion matrix 예제 본문

EDA

sklearn confusion matrix 예제

vincenthanna 2020. 8. 18. 22:47

binary(0 or 1) category의 경우 다음과 같이 confusion matrix를 표현할 수 있다.

 

  predicted
0 1
expected 0 TN FP
1 FN TP

 

sklearn.metrics.confusion_matrix로 위와 같은 표를 출력하기 위한 예제 코드:

from sklearn.metrics import confusion_matrix
from matplotlib import pyplot as plt

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

    row direction    : expected
    column direction : predicted

Confusion matrix:
           0      1 (predicted)
       --------------
    0 |  114709   0
    1 |  4334     0
(expected)

result shows all test data predicted as class '0'. (imbalanced)
"""


conf_mat = confusion_matrix(y_true=y_test, y_pred=y_pred)
print(y_test.shape, y_pred.shape)
print(conf_mat.shape)
print("Confusion matrix:\n", conf_mat)

labels = ['class 0', 'class 1']
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(conf_mat, cmap=plt.cm.Blues)
fig.colorbar(cax)
ax.set_xticklabels([''] + labels)
ax.set_yticklabels([''] + labels)
plt.xlabel('Predicted')
plt.ylabel('Expected')
plt.show()

Comments