【问题标题】:How do I get feature names when I select only the 20% most important features, using a chi2 test?当我使用 chi2 测试仅选择 20% 最重要的特征时,如何获取特征名称?
【发布时间】:2014-02-10 04:36:37
【问题描述】:

这是我在“Learning scikit-learn:”一书中找到的代码片段:

from sklearn import feature_selection
fs = feature_selection.SelectPercentile(feature_selection.chi2, percentile=20)
X_train_fs = fs.fit_transform(X_train, y_train)

我想要的是特征名称和相对重要性的数组。

我在 Stack (show feature names after feature selection) 中找到了这段代码,但并不完全理解它,也无法让它工作。

对于如何获取、存储和打印功能的建议,我将不胜感激。感谢您的帮助。

【问题讨论】:

  • 你试过fs.scores_属性吗?根据docs,这应该会给你特征和他们的分数
  • 这么多属性;这么少的时间......谢谢Ed!克里斯

标签: numpy pandas scikit-learn


【解决方案1】:

这是感谢 EdChum 的代码

.....将数据读入numpy数组

.... 获取特征名称并放入 feature_names_

from sklearn import feature_selection
fs = feature_selection.SelectPercentile(feature_selection.chi2, percentile=20)
X_train_fs = fs.fit_transform(plants_X, plants_y)

print feature_names, '\n', fs.scores_

【讨论】:

    猜你喜欢
    • 2018-09-25
    • 2019-01-12
    • 2019-08-13
    • 2017-06-03
    • 2012-12-17
    • 2014-09-20
    • 2017-11-14
    • 2022-12-19
    相关资源
    最近更新 更多