【问题标题】:How to find the corresponding class in clf.predict_proba()如何在 clf.predict_proba() 中找到对应的类
【发布时间】:2013-05-27 08:32:13
【问题描述】:

我有许多类和对应的特征向量,当我运行 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]])

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

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

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

【问题讨论】:

    标签: python machine-learning scikit-learn


    【解决方案1】:

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

    一般在Python中,你可以使用dir函数来找出一个对象有哪些属性。

    【讨论】:

      【解决方案2】:

      只需使用分类器的.classes_ 属性即可恢复映射。在您的示例中:

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

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

      【讨论】:

      • 对于单个样本,zip(clf.classes_, clf.predict_proba(x)[0]) 提供可读的输出。
      • 有没有办法将预定义的顺序传递给分类器?
      • 如果您愿意,可以直接将您的课程命名为0, 1, 2...。否则你必须使用clf.classes_ 属性来做反向映射。
      【解决方案3】:
      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
      

      【讨论】:

        猜你喜欢
        • 2018-06-05
        • 2011-11-29
        • 2013-10-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-07
        • 2020-12-22
        • 2016-06-18
        • 1970-01-01
        相关资源
        最近更新 更多