【问题标题】:The features importance from scikit -learn pipeline (SVC)scikit -learn 管道 (SVC) 的特征重要性
【发布时间】:2019-02-14 16:49:20
【问题描述】:

我有以下管道,我想获得每个类的功能。我有三个课程(“小说”、“非小说”、“无”)。我使用的分类器是SVC

Book_contents= Pipeline([('selector', ItemSelector(key='Book')),
                         ('tfidf',CountVectorizer(analyzer='word',
                                                  binary=True,
                                                  ngram_range=(1,1))),
                        ])

Author_description= Pipeline([('selector', ItemSelector(key='Description')),
                              ('tfidf', CountVectorizer(analyzer='word',
                                                        binary=True,
                                                        ngram_range=(1,1))),
                             ])

ppl = Pipeline([('feats', FeatureUnion([('Contents',Book_contents),
                                        ('Desc',Author_description)])),
                ('clf', SVC(kernel='linear',class_weight='balanced'))
               ])

model = ppl.fit(training_data, Y_train)   

我尝试过 eli5,但出现特征名称和分类器不匹配的错误。

f1=model.named_steps['feats'].transformer_list[0][1].named_steps['tfidf'].get_feature_names()
f2=model.named_steps['feats'].transformer_list[1][1].named_steps['tfidf'].get_feature_names()
    list_features=f1
list_features.append(f2)
explain_weights.explain_linear_classifier_weights(model.named_steps['clf'], 
                                              vec=None, top=20, 
                                              target_names=ppl.classes_, 
                                              feature_names=list_features)

我收到了这个错误:

feature_names 的长度错误:expected=47783, got=10528

如何获得每个类别的特征权重的排名?他们有没有 eli5 的方式来做到这一点?

【问题讨论】:

  • 请解释一下你在这段代码中做了什么:feature_names=model.named_steps['feats'].transformer_list[0][1].named_steps['tfidf'].get_feature_names()?
  • 嗨@VivekKumar,此代码用于访问管道中的步骤以获取功能,但我不确定这是否是正确的方法
  • 这就是我问的原因。您仅从 FeatureUnion 的第一部分访问了功能,而不是从第二部分访问了
  • 感谢您指出@VivekKumar,但即使添加了每个功能,我仍然无法访问 wights。除了eli5还有其他方法吗?可能有 coef_ 的东西?
  • 你做得怎么样

标签: python scikit-learn svm


【解决方案1】:

除了这一行之外,您所做的一切都是正确的:

list_features.append(f2)

在这里,您将整个f2 列表作为一个元素附加到f1 列表中。这不是你想要的。

您想将 f2 的所有元素添加到 f1。为此,您需要使用extend。只需这样做:

list_features.extend(f2)

查看这个问题了解更多详情:

除此之外,我认为你调用explain_weights.explain_linear_classifier_weights 的方式是错误的。您只需要调用explain_weights(...),它会自动在内部调用explain_linear_classifier_weights

【讨论】:

  • 他也可以list_features += f2
猜你喜欢
  • 2017-11-04
  • 2018-09-23
  • 2016-03-18
  • 2018-08-16
  • 2021-02-02
  • 2020-09-18
  • 2016-11-30
  • 1970-01-01
  • 2016-06-23
相关资源
最近更新 更多