繁体   English   中英

如何在scikit中获取与卡方特征选择分数相对应的特征名称

[英]How to get feature names corresponding to scores for chi square feature selection in scikit

我正在使用 Scikit 进行特征选择,但我想获取文本中所有 unigram 的得分值。 我得到了分数,但我如何将这些 map 转换为实际的功能名称。

from sklearn.feature_extraction.text  import CountVectorizer
from sklearn.feature_selection import  SelectKBest, chi2

Texts=["should schools have uniform","schools discipline","legalize marriage","marriage culture"]
labels=["3","3","7","7"]
vectorizer = CountVectorizer()
term_doc=vectorizer.fit_transform(Texts)
ch2 = SelectKBest(chi2, "all")
X_train = ch2.fit_transform(term_doc, labels)
print ch2.scores_

这给出了结果,但我怎么知道哪些特征名称映射到哪些分数?

它就在文档中:

get_feature_names()

要在初始 select 中打印特征名称,卡方中的所有特征然后将其与您的列匹配,并且根据 p 值,您可以删除该特征。

从 sklearn 导入数据集 从 sklearn.feature_selection 导入 SelectKBest 从 sklearn.feature_selection 导入 chi2

X = df.drop("结果",axis=1) y = df["结果"]

chi_scores = chi2(X,y)

chi_scores

p_values = pd.Series(chi_scores[1],index = X.columns) p_values.sort_values(ascending = False, inplace = True)

p_values.plot.bar(figsize=(20,10))

打印(p_values>=0.5)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM