1By definition, entry i,j in a confusion matrix is the number of
2observations actually in group i, but predicted to be in group j.
3Scikit-Learn provides a confusion_matrix function:
4
5from sklearn.metrics import confusion_matrix
6y_actu = [2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2]
7y_pred = [0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 2]
8confusion_matrix(y_actu, y_pred)
9# Output
10# array([[3, 0, 0],
11# [0, 1, 2],
12# [2, 1, 3]], dtype=int64)
1import pandas as pd
2y_true = pd.Series([2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2])
3y_pred = pd.Series([0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 2])
4
5pd.crosstab(y_true, y_pred, rownames=['True'], colnames=['Predicted'], margins=True)
6
1import numpy as np
2
3currentDataClass = [1, 3, 3, 2, 5, 5, 3, 2, 1, 4, 3, 2, 1, 1, 2]
4predictedClass = [1, 2, 3, 4, 2, 3, 3, 2, 1, 2, 3, 1, 5, 1, 1]
5
6def comp_confmat(actual, predicted):
7
8 classes = np.unique(actual) # extract the different classes
9 matrix = np.zeros((len(classes), len(classes))) # initialize the confusion matrix with zeros
10
11 for i in range(len(classes)):
12 for j in range(len(classes)):
13
14 matrix[i, j] = np.sum((actual == classes[i]) & (predicted == classes[j]))
15
16 return matrix
17
18comp_confmat(currentDataClass, predictedClass)
19
20array([[3., 0., 0., 0., 1.],
21 [2., 1., 0., 1., 0.],
22 [0., 1., 3., 0., 0.],
23 [0., 1., 0., 0., 0.],
24 [0., 1., 1., 0., 0.]])
25
26
1Predicted 0 1 2 All
2True
30 3 0 0 3
41 0 1 2 3
52 2 1 3 6
6All 5 2 5 12
7