sklearn Selectkbest, how to create a dict of {feature1:score



我试图更清楚地了解选择kbest进程。我希望在字典上查看所有特征(选择与否)的分数,以便稍后像这样绘制它:

在此处输入图像描述

到目前为止我已经尝试过

print selector.scores_

我收到的地方

[ 18.57570327 9.34670079 10.07245453 24.46765405 6.23420114 4.20497086 8.86672154 0.21705893 11.59554766 25.09754153 7.2427304 21.06000171 5.31257143 0.1641645 1.69882435]

print sorted(selector.scores_, reverse=True)[:5]

selector = SelectKBest(f_classif, k=5)
selectedFeatures = selector.fit(features, labels)
selected_features_list = [features_list[i+1] for i in selectedFeatures.get_support(indices=True)]
features_list = features_list[:1]+selected_features_list
print 'New feature_list after SelectKbest isn',features_list,'n'
print sorted(selector.scores_, reverse=True)[:5]

我可以知道所选功能的地方,我可以知道 5 个最佳功能,但无法确定索引是否相同。

New feature_list after SelectKbest is
['poi', 'salary', 'total_stock_value', 'deferred_income', 'exercised_stock_options', 'bonus'] 
[25.097541528735491, 24.467654047526398, 21.060001707536571, 18.575703268041785, 11.595547659730601]

我正在寻找的是:

    [[best_feature,best_score],
[2nbest_feature,2nbest_score],
[3rdbest_feature,3rdbest_score],
and so on with all features]

知道吗?

警告一下,字典是一个无序对象,所以这样做没有意义,但无论如何我已经为你提供了最后一步

首先,您将分数和名称组合到一个对象中:

combined = zip(feature_names, scores)

然后,您需要根据分数对对象进行排序:

combined.sort(reverse=True, key= lambda x: x[1])

然后只需将数据放入字典中:

dict((x, y) for x, y in combined)

回答我自己的问题

对于字典创建:

all_scores_dict = {}
for i, score in enumerate(selector.scores_):
    all_scores_dict[features_list[support[i]+1]] = score

用于排序(表示形式现在是元组列表)

import operator
sorted_dict_scores = sorted(all_scores_dict.items(), key=operator.itemgetter(1),reverse = True)

这给你

[('exercised_stock_options', 25.097541528735491),
 ('total_stock_value', 24.467654047526398),
 ('bonus', 21.060001707536571),
 ('salary', 18.575703268041785),
 ('deferred_income', 11.595547659730601),
 ('long_term_incentive', 10.072454529369441),
 ('restricted_stock', 9.3467007910514877),
 ('total_payments', 8.8667215371077717),
 ('loan_advances', 7.2427303965360181),
 ('expenses', 6.2342011405067401),
 ('sum_of_unclassified', 5.31257142710212),
 ('other', 4.204970858301416),
 ('to_messages', 1.6988243485808501),
 ('deferral_payments', 0.2170589303395084),
 ('from_messages', 0.16416449823428736)]

相关内容

  • 没有找到相关文章