get true labels from keras generator
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: RPG Blues Looping
--
Chapters
00:00 Get True Labels From Keras Generator
00:29 Accepted Answer Score 4
00:57 Answer 2 Score 1
01:39 Thank you
--
Full question
https://stackoverflow.com/questions/5394...
--
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
