我有一个模型,它根据一些条件对场地进行分类,它有10个类别,我想知道模型预测每个类别的置信度得分是多少?
我的代码:结果是一个数组,模型在其中进行预测
predictions=model.predict(result)
confidence_score= model.predict_proba(list(result))
model.prpredict只返回一个值,置信度分数有每个类的分数列表,如下所示:
[[0. 0.14 0. 0. 0. 0.56 0. 0.17
0.1 0.01 0. 0.20 0. 0. 0.002 0.01]]
它还应该为每个类返回类标签,例如:A类有0.2%的概率发生,等等。
labelencoder.inverse_transform(predictions)
输出应该看起来像:
{Class Label A : Probability score , Class Label B: Probability score ....}
输出使用以下代码:
dictionary=[dict(zip(labelencoder.classes_, cs)) for cs in confidence_score]
Output
{Covent Garden': 0.0, 'London Cocktail Club - Liverpool Street': 0.0,
'Lost Society Battersea': 0.0, 'Lost Society Putney': 0.94.....}
在这种情况下,你可以看到Lost Society有更高的置信度分数,但当我做建模时,它会给我返回一些其他标签,而不是这个标签,我在代码中写了预测得分最高的类的代码。
My code :
predictions=model.predict(result) //returns the single number
confidence_score= model.predict_proba(list(result))
dictionary=[dict(zip(labelencoder.classes_, cs)) for cs in confidence_score]
print(dictionary)
print("Recommended Venue for this type of category is",labelencoder.inverse_transform(predictions))
print("Confidence Score : ", np.max(confidence_score))
return labelencoder.inverse_transform(predictions),np.max(confidence_score)
我们可以zip
、labelencoder.classes_
和confidence_score
,并将zip
对象传递给dict
,以创建字典
dict(zip(labelencoder.classes_, confidence_score.squeeze()))
如果你想在一次中预测多个样本
[dict(zip(labelencoder.classes_, cs)) for cs in confidence_score]