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)
1from sklearn.metrics import confusion_matrix
2cm = confusion_matrix(y_test, y_predicted)
3cm
4# after creating the confusion matrix, for better understaning plot the cm.
5import seaborn as sn
6plt.figure(figsize = (10,7))
7sn.heatmap(cm, annot=True)
8plt.xlabel('Predicted')
9plt.ylabel('Truth')
1df_confusion = pd.crosstab(y_actu, y_pred, rownames=['Actual'], colnames=['Predicted'], margins=True)
2
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