Creating a confusion matrix without using sklearn for mnist data

Viewed 37

Need to make a confusion matrix for data without using sklearn. Trained the neural network and tested it. I think I need to compute the sum of outcomes for each possible outcomes (0-9) and find the average but I'm not sure how to extract the relevant trials for each outcome from the list.

Here is my code:

# go through all the records in the test data set
for record in test_data_list:
# split the record by the ',' commas
all_values = record.split(',')
# correct answer is first value
correct_label = int(all_values[0])
# scale and shift the inputs
inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
# query the network
outputs = n.query(inputs)

  
# Note that this array, outputs, is the 10 output nodes of the NW for each trial
# This is wehre you need to chnage the code below to build the confusion matrix

label = numpy.argmax(outputs)
# append correct or incorrect to list 
if (label == correct_label):
    # network's answer matches correct answer, add 1 to scorecard
    scorecard.append(1)
else:
    # network's answer doesn't match correct answer, add 0 to scorecard
    scorecard.append(0)
    pass
pass
1 Answers

Create a (n_classes, n_classes) matrix and increment value for each conf_mat[gt_label, predicted_label] += 1

Code:

import numpy as np


n_classes = 10
ground_truth_labels = np.random.randint(0, n_classes, size=1000)
predicted_labels = np.random.randint(0, n_classes, size=1000)

# Self-made confusion matrix
confusion_matrix = np.zeros((n_classes, n_classes))
for ground_truth_label, predicted_label in zip(ground_truth_labels, predicted_labels):
    confusion_matrix[ground_truth_label, predicted_label] += 1

# check the correctness of our matrix
from sklearn.metrics import confusion_matrix as sklearn_confusion_matrix
conf_matrix_sklearn = sklearn_confusion_matrix(ground_truth_labels, predicted_labels, labels=range(10))

assert np.allclose(conf_matrix_sklearn, confusion_matrix)
Related