The Python Oracle

get true labels from keras generator

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5 Looping

--

Chapters
00:00 Question
00:38 Accepted answer (Score 4)
01:12 Answer 2 (Score 1)
02:11 Thank you

--

Full question
https://stackoverflow.com/questions/5394...

Question links:
[sklearn.metrics.confusion_matrix(y_true, y_pred)]: https://scikit-learn.org/stable/modules/...
[predict_generator(generator)]: https://keras.io/models/model/#predict_g...

Answer 1 links:
[ImageDataGenerator]: https://keras.io/preprocessing/image/#im...
https://scikit-learn.org/stable/auto_exa...
[image]: https://i.stack.imgur.com/I1h9t.png

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #scikitlearn #keras

#avk47



ACCEPTED ANSWER

Score 4


generator.classes will give you observed values in sparse format. You probably need it in dense (i.e., one-hot encoded format). You could get that with:

import pandas as pd
pd.get_dummies(pd.Series(generator.classes)).to_dense()

NOTE though: you must set the generator's shuffle attribute to False before generating the predictions and fetching the observed classes, otherwise your predictions and observations will not line up!




ANSWER 2

Score 1


After creating a data generator, either your own or the built in ImageDataGenerator, use your trained model to make predictions:

true_labels = data_generator.classes
predictions = model.predict_generator(data_generator)

sklearn's confusion matrix expects a 1-d array of labels, so you have to convert your predictions using np.argmax()

y_true = true_labels
y_pred = np.array([np.argmax(x) for x in predictions])

Then you can use those variables directly in the confusion_matrix function

cm = sklearn.metrics.confusion_matrix(y_true, y_pred)

And you can plot it using the example plot_confusion_matrix() function found here:

https://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html

enter image description here