'create confusion matrix (average) in python

How to make a confusion matrix with the average of the results obtained from a binary classifier for, for example, 20 training/testing iterations? These sets can have different sizes in each iteration.



Solution 1:[1]

You can put the training/test protocol with a confusion matrix generator inside a loop and sum the matrices with numpy and then divide for the average at the end. You can calculate the confusion matrix with sklearn but I like to use spectrapepper:

import spectrapepper as spep
import numpy as spep

iterations = 20
classes = 4 # number of possible classifications
master = np.array([[0 for _ in range(classes)] for _ in range(classes)])
for i in range(iterations):
    # training and test protocol
    # ...
    # ...
    # ...
    c = spep.confusionmatrix(real, predicted, gn=[i for i in range(classes)], plot=False) # if plot = True will auto plot the matrix.
    master = master+np.array(c)

print(master/iterations)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Enric Grau-Luque