Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras Top 5 predictions

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.

like image 838
Aprameyo Roy Avatar asked Apr 14 '18 04:04

Aprameyo Roy


1 Answers

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:]]
like image 141
thefifthjack005 Avatar answered Nov 11 '22 23:11

thefifthjack005