use*_*717 16 machine-learning scikit-learn
我需要为文本构建一个分类器,现在我使用TfidfVectorizer和SelectKBest来选择这些功能,如下所示:
vectorizer = TfidfVectorizer(sublinear_tf = True, max_df = 0.5, stop_words = 'english',charset_error='strict')
X_train_features = vectorizer.fit_transform(data_train.data)
y_train_labels = data_train.target;
ch2 = SelectKBest(chi2, k = 1000)
X_train_features = ch2.fit_transform(X_train_features, y_train_labels)
Run Code Online (Sandbox Code Playgroud)
我想在选择k最佳功能后打印出所选功能名称(文本),有什么办法可以做到吗?我只需要打印出选定的功能名称,也许我应该使用CountVectorizer?
ogr*_*sel 17
以下应该有效:
np.asarray(vectorizer.get_feature_names())[ch2.get_support()]
Run Code Online (Sandbox Code Playgroud)
为了扩展@ ogrisel的答案,返回的特征列表在被矢量化时的顺序相同.下面的代码将为您提供排名最高的功能列表,这些功能按照其Chi-2分数降序排列(以及相应的p值):
top_ranked_features = sorted(enumerate(ch2.scores_),key=lambda x:x[1], reverse=True)[:1000]
top_ranked_features_indices = map(list,zip(*top_ranked_features))[0]
for feature_pvalue in zip(np.asarray(train_vectorizer.get_feature_names())[top_ranked_features_indices],ch2.pvalues_[top_ranked_features_indices]):
print feature_pvalue
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7276 次 |
| 最近记录: |