【发布时间】:2020-07-02 07:59:52
【问题描述】:
我有一个 cmets(文本)列表,我必须使用一些分类器(输入)对其进行分类。
我正在使用pipeline 来执行此操作,我使用KFold 因为数据集非常小。
我想知道带有SelectKBest 的分类器的最佳特征名称,但由于它在pipeline 中,我不知道如何获得最佳特征名称。
comments 是一个字符串列表。
def classify(classifiers, folder="tfidf-classifiers"):
comments = get_comments()
labels = get_labels()
tfidf_vector = TfidfVectorizer(tokenizer=tokenizer, lowercase=False)
stats = {}
for i in classifiers:
classifier = i()
pipe = Pipeline(
[('vectorizer', tfidf_vector), ('feature_selection', SelectKBest(chi2)), ('classifier', classifier)])
result = cross_val_predict(pipe, comments, labels, cv=KFold(n_splits=10, shuffle=True))
cm = confusion_matrix(result, labels, [information, non_information])
saveHeatmap(cm, i.__name__, folder)
report = classification_report(labels, result, digits=3, target_names=['no', 'yes'], output_dict=True)
stats[i.__name__] = report
return stats
我在网上搜索了一下,发现了这个:
pipe.named_steps['feature_selection'].get_support()
但我不能这样做,因为我没有在管道上调用fit。我在这里使用管道:
result = cross_val_predict(pipe, comments, labels, cv=KFold(n_splits=10, shuffle=True))
如何获得最佳的 K 特征名称?
我想要的是一个简单的列表,列出对分类器的工作“帮助最大”的单词...
【问题讨论】:
标签: python scikit-learn tf-idf feature-selection tfidfvectorizer