Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate accuracy from Keras model.predict_generator

I have Keras model and I want to evaluate it using my test data. When I use keras model.evaluate_generator I get loss and acc returned by it and I can print the percentage accuracy like:

loss, acc = model.evaluate_generator(test_gen, steps=evaluation_steps)
print("\n%s: %.2f%%" % (model.metrics_names[1], acc * 100))

This results in something like 92%.
As I wanted to create a confusion matrix (too see how much false positives and false negatives I have) I changed my code to:

predictions = model.predict_generator(test_gen, steps=evaluation_steps)
y_pred = np.argmax(predictions, axis=1)
y_true = np.argmax(labels, axis=1)
confusion_matrix(y_true, y_pred)

I get correct confusion matrix with this. However, I still want that 92% displayed, can I get it from predictions?

like image 663
John Avatar asked Dec 20 '25 21:12

John


1 Answers

Accuracy can be calculated in a straightforward way from your y_pred and y_true; here is an example with dummy data for 3-class classification:

import numpy as np

y_true = np.array([2, 0, 2, 2, 0, 1])
y_pred = np.array([0, 0, 2, 2, 1, 2])

Simple visual inspection here tells us that our accuracy should be 0.5 (50%); so:

l = len(y_true)
acc = sum([y_pred[i]==y_true[i] for i in range(l)])/l
acc
# 0.5
like image 183
desertnaut Avatar answered Dec 23 '25 11:12

desertnaut