Keras 前 5 名预测

Apr*_*Roy 6 deep-learning keras tensorflow

如何从model.predict()Keras 中检索前 5 个预测?它只给出 1 个预测。有什么办法吗?我不希望它作为评估指标。我只需要前 5 个预测。

the*_*005 3

如果您试图从图像分类问题中获得最佳预测,您将收到一个热门代码预测。

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:]
Run Code Online (Sandbox Code Playgroud)

您现在拥有所有五个最高值的索引。您现在可以循环遍历索引并获取类名称。

仅提取 top_values_without_index

top_values= [class_prob[i] for i in np.argsort(class_prob)[-5:]]
Run Code Online (Sandbox Code Playgroud)