Python 如何在 clf.predict_proba() 中找到对应的类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16858652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:50:18  来源:igfitidea点击:

How to find the corresponding class in clf.predict_proba()

pythonmachine-learningscikit-learn

提问by user1506145

I have a number of classes and corresponding feature vectors, and when I run predict_proba() I will get this:

我有许多类和相应的特征向量,当我运行 predict_proba() 时,我会得到这个:

classes = ['one','two','three','one','three']

feature = [[0,1,1,0],[0,1,0,1],[1,1,0,0],[0,0,0,0],[0,1,1,1]]

from sklearn.naive_bayes import BernoulliNB

clf = BernoulliNB()
clf.fit(feature,classes)
clf.predict_proba([0,1,1,0])
>> array([[ 0.48247836,  0.40709111,  0.11043053]])

I would like to get what probability that corresponds to what class. On this page it says that they are ordered by arithmetical order, i'm not 100% sure of what that means: http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html#sklearn.svm.SVC.predict_proba

我想得到对应于什么类别的概率。在这个页面上它说它们是按算术顺序排序的,我不是 100% 确定这意味着什么:http: //scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html#sklearn .svm.SVC.predict_proba

Does it mean that I have go trough my training examples assign the corresponding index to the first encounter of a class, or is there a command like

这是否意味着我已经通过我的训练示例为类的第一次遇到分配了相应的索引,或者是否有类似的命令

clf.getClasses() = ['one','two','three']?

clf.getClasses() = ['one','two','three']?

采纳答案by ogrisel

Just use the .classes_attribute of the classifier to recover the mapping. In your example that gives:

只需使用.classes_分类器的属性来恢复映射。在你的例子中,给出:

>>> clf.classes_
array(['one', 'three', 'two'], 
      dtype='|S5')

And thanks for putting a minimalistic reproduction script in your question, it makes answering really easy by just copy and pasting in a IPython shell :)

感谢您在您的问题中放置了一个简约的复制脚本,只需在 IPython shell 中复制和粘贴,就可以轻松回答:)

回答by lazy1

As a rule, any attribute in a learner that ends with _ is a learned one. In your case you're looking for clf.classes_.

通常,学习器中以 _ 结尾的任何属性都是学习的。在您的情况下,您正在寻找clf.classes_.

Generally in Python, you can use the dirfunction to find out which attributes an object has.

通常在 Python 中,您可以使用该dir函数找出对象具有哪些属性。

回答by pomber

import pandas as pd
test = [[0,1,1,0],[1,1,1,0]]
pd.DataFrame(clf.predict_proba(test), columns=clf.classes_)

Out[2]:
         one       three         two
0   0.542815    0.361876    0.095309
1   0.306431    0.612863    0.080706