如何获取每个类别的概率和标签?

我有一个基于某些条件对场所进行分类的模型,它有10个类别,我想知道模型对每个类别的预测置信度得分是多少?

我的代码:结果是一个模型进行预测的数组

        predictions=model.predict(result)        confidence_score= model.predict_proba(list(result))

model.predict 只返回一个单一的值,而confidence score则包含每个类别的得分列表,如下所示:

[[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) 

输出应该看起来像这样:

{类别标签A : 概率得分 , 类别标签B: 概率得分 ....}

使用以下代码可以得到输出:

      dictionary=[dict(zip(labelencoder.classes_, cs)) for cs in confidence_score]输出   {Covent Garden': 0.0, 'London Cocktail Club - Liverpool Street': 0.0,'Lost Society Battersea': 0.0, 'Lost Society Putney': 0.94.....}

在这种情况下,你可以看到Lost Society的置信度得分更高,但当我使用model.predict时,它返回的是其他标签而不是这个,我在代码中已经写明要预测得分最高的类别。

我的代码:        predictions=model.predict(result) //返回单一数字         confidence_score= model.predict_proba(list(result))        dictionary=[dict(zip(labelencoder.classes_, cs)) for cs in confidence_score]        print(dictionary)                    print("推荐的此类别场所是",labelencoder.inverse_transform(predictions))        print("置信度得分 : ", 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]

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注