【问题标题】:Identifying a sklearn-model's classes识别 sklearn-model 的类
【发布时间】:2015-07-23 03:58:16
【问题描述】:

SVMs 上的文档暗示存在一个名为 classes_ 的属性,据称它揭示了模型如何在内部表示类。

我想获取这些信息,以便解释像 predict_proba 这样的函数的输出,它会为多个样本生成类概率。希望知道给定一些说明性值:

model.classes_ 
>>> [1, 2, 4]

意味着我可以假设这成立:

model.predict_proba([[1.2312, 0.23512, 6.01234], [3.7655, 8.2353, 0.86323]]) 
>>> [[0.032, 0.143, 0.825], [0.325, 0.143, 0.532]]

概率应该转换为与类相同的顺序,即对于我可以假设的第一组特征:

probability of class 1: 0.032
probability of class 2: 0.143
probability of class 4: 0.825

但是在 SVM 上调用 classes_ 会导致错误。有没有获取这些信息的好方法?无法想象模型训练完成后就再也无法访问了。


编辑: 我构建模型的方式或多或少是这样的:

from sklearn.svm import SVC
from sklearn.grid_search import GridSearchCV
from sklearn.pipeline import Pipeline, FeatureUnion


pipeline = Pipeline([
   ('features', FeatureUnion(transformer_list[ ... ])),
   ('svm', SVC(probability=True))
])
parameters = { ... }
grid_search = GridSearchCV(
    pipeline,
    parameters
)

grid_search.fit(get_data(), get_labels())
clf = [elem for elem in grid_search.estimator.steps if elem[0] == 'svm'][0][1]

print(clf)
>> SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
  kernel='rbf', max_iter=-1, probability=True, random_state=None,
  shrinking=True, tol=0.001, verbose=False)
print(clf.classes_)
>> Traceback (most recent call last):
  File "path/to/script.py", line 284, in <module>
  File "path/to/script.py", line 181, in re_train
    print(clf.classes_)
AttributeError: 'SVC' object has no attribute 'classes_'

【问题讨论】:

    标签: python scikit-learn svm


    【解决方案1】:

    sklearn 中有一个 classes 字段,这可能意味着你调用了错误的模型,请参见下面的示例,我们可以看到 classes_ 字段中有 classes:

    >>> import numpy as np
    >>> from sklearn.svm import SVC
    >>> X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
    >>> y = np.array([1, 1, 2, 2])
    >>> clf = SVC(probability=True)
    >>> clf.fit(X, y)
    SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
      kernel='rbf', max_iter=-1, probability=True, random_state=None,
      shrinking=True, tol=0.001, verbose=False)
    >>> print clf.classes_
    [1 2]
    >>> print clf.predict([[-0.8, -1]])
    [1]
    >>> print clf.predict_proba([[-0.8, -1]])
    [[ 0.92419129  0.07580871]]
    

    【讨论】:

    • 我明白了。也许我的问题是,我的模型“隐藏”在 GridSearchCV 中。我用更多信息和更具体的错误消息更新了最初的帖子。
    【解决方案2】:

    您正在查看的grid_search.estimator 是未安装的管道。 classes_ 属性只有在拟合后才存在,因为分类器需要看到y

    您想要的是使用最佳参数设置训练的估计器,即grid_search.best_estimator_

    以下将起作用:

    clf = grid_search.best_estimator_.named_steps['svm']
    print(clf.classes_)
    

    [和 classes_ 完全按照你的想法做]。

    【讨论】:

      【解决方案3】:

      我相信这应该可以解决问题

      arr = model.predict_proba(X)
      
      list1 = arr.tolist()
      
      cls = model.classes_
      
      list2 = cls.tolist()
      
      d = {''Category'':list2,''Probability'':list1[0]}
      
      df = pd.DataFrame(d)
      
      print(df)
      

      【讨论】:

        猜你喜欢
        • 2013-11-19
        • 2021-04-25
        • 1970-01-01
        • 2015-09-12
        • 2015-10-28
        • 1970-01-01
        • 2022-11-17
        • 1970-01-01
        相关资源
        最近更新 更多