How can I retrieve the top 5 predictions from model.predict()
in Keras? It only gives only 1 prediction. Is there any way to do so? I don't want it as an evaluation metric. I just need the top 5 predictions.
if you are trying to get top prediction from a image classification problem, you will receive a one hot code prediction.
class_prob = [0.98,0.50,0.60,0.90,0.87,0.79,0.87]
top_values_index = sorted(range(len(class_prob)), key=lambda i: class_prob[i])[-the_top_values_you_want_to_extract:]
you now have the index for all the five top values.you can now just loop through the index and get the class name.
to extract just the top_values_without_index
top_values= [class_prob[i] for i in np.argsort(class_prob)[-5:]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With