【问题标题】:How to get the selected features with their scores?如何获得所选特征及其分数?
【发布时间】:2017-09-13 02:09:22
【问题描述】:

参考Map object is not subscriptable error 我使用 Moses Xu 的答案来获取特征名称。它会产生错误“地图对象不可下标”。代码如下。我正在使用 python 3.x

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).

错误在第二行代码。

输出

('00 8b 4d fc', 3.4028916591534005e-61)
('51 00 22 05', 3.4028916591534005e-61)
('00 74 00 61', 8.3973527363656966e-61)

【问题讨论】:

    标签: scikit-learn feature-selection


    【解决方案1】:

    在 python 3.x 中,map 返回一个迭代器,而不是一个列表。因此,返回的对象是不可下标的。 您可以将其更改为list(map(list,zip(*a)))[0] 以解决问题。

    已编辑:

    您也可以使用next(map(list,zip(*a))) 不将分数加载到内存中,并让代码更加pythonic。

    【讨论】:

    • @banu 在同一篇文章中添加一个额外的问题只会造成混乱。看起来好像您收到错误和正确的输出。请删除第二个问题。您可以在新帖子中提出新问题,也可以搜索现有解决方案。
    • 好的。感谢您的指导。
    猜你喜欢
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 2022-07-06
    • 2014-02-11
    • 2014-09-21
    • 1970-01-01
    • 2019-08-08
    • 2021-05-14
    相关资源
    最近更新 更多