【发布时间】: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