In [1]:
from sklearn.metrics import confusion_matrix
In [2]:
y_true = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
y_pred = [0, 1, 1, 1, 1, 0, 0, 0, 1, 1]
In [3]:
cm = confusion_matrix(y_true, y_pred)
In [4]:
print(cm)
In [5]:
print(type(cm))
In [6]:
print(cm.flatten())
In [7]:
tn, fp, fn, tp = cm.flatten()
In [8]:
print(tn)
In [9]:
print(fp)
In [10]:
print(fn)
In [11]:
print(tp)
In [12]:
y_true_ab = ['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B']
y_pred_ab = ['A', 'B', 'B', 'B', 'B', 'A', 'A', 'A', 'B', 'B']
In [13]:
print(confusion_matrix(y_true_ab, y_pred_ab))
In [14]:
print(confusion_matrix(y_true_ab, y_pred_ab, labels=['B', 'A']))
In [15]:
y_true_multi = [0, 0, 0, 1, 1, 1, 2, 2, 2]
y_pred_multi = [0, 1, 1, 1, 1, 2, 2, 2, 2]
In [16]:
print(confusion_matrix(y_true_multi, y_pred_multi))
In [17]:
print(confusion_matrix(y_true_multi, y_pred_multi, labels=[2, 1]))