📅  最后修改于: 2022-03-11 14:46:50.392000             🧑  作者: Mango
import numpy as np
currentDataClass = [1, 3, 3, 2, 5, 5, 3, 2, 1, 4, 3, 2, 1, 1, 2]
predictedClass = [1, 2, 3, 4, 2, 3, 3, 2, 1, 2, 3, 1, 5, 1, 1]
def comp_confmat(actual, predicted):
classes = np.unique(actual) # extract the different classes
matrix = np.zeros((len(classes), len(classes))) # initialize the confusion matrix with zeros
for i in range(len(classes)):
for j in range(len(classes)):
matrix[i, j] = np.sum((actual == classes[i]) & (predicted == classes[j]))
return matrix
comp_confmat(currentDataClass, predictedClass)
array([[3., 0., 0., 0., 1.],
[2., 1., 0., 1., 0.],
[0., 1., 3., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 1., 1., 0., 0.]])