【问题标题】:K Value not changing outcome for SelectKBestK 值不会改变 SelectKBest 的结果
【发布时间】:2019-03-22 09:57:28
【问题描述】:

我目前正在开展一个项目,我正在运行数据并试图找到数据中的最佳特征。我正在使用 sklearn 和 SelectKBest 模块。当我运行代码时,我会得到结果,但无论我使用什么 K 值,它都会给我相同的结果。想知道是否有人可以查看我的代码并告诉我出了什么问题。我正在使用 Jupyter Notebook 构建它,因此我将更改值然后重新运行该块。

features_list = ['poi', 'salary','to_messages','deferral_payments',
                 'total_payments','exercised_stock_options','bonus',
                 'restricted_stock','shared_receipt_with_poi','restricted_stock_deferred',
                 'total_stock_value','expenses','loan_advances','from_messages','other',
                 'from_this_person_to_poi','director_fees','deferred_income','long_term_incentive','from_poi_to_this_person'] 

        data = featureFormat(data_dict, features_list, sort_keys=True)
            labels, features = targetFeatureSplit(data)

            from sklearn.feature_selection import SelectKBest

            clf = SelectKBest()
            new_features = clf.fit_transform(features,labels)
            params = clf.get_params()

            i=0
            featureImportance = []
            for item in clf.scores_:
                featureImportance.append((item,features_list[i+1]))
                i=i+1

            featureImportance=sorted(featureImportance, reverse=True)
            for item in featureImportance:
                 print "{0} , {1:4.2f}%".format(item[1],item[0])

输出:

exercised_stock_options , 25.10%
total_stock_value , 24.47%
bonus , 21.06%
salary , 18.58%
deferred_income , 11.60%
long_term_incentive , 10.07%
restricted_stock , 9.35%
total_payments , 8.87%
shared_receipt_with_poi , 8.75%
loan_advances , 7.24%
expenses , 6.23%
from_poi_to_this_person , 5.34%
other , 4.20%
from_this_person_to_poi , 2.43%
director_fees , 2.11%
to_messages , 1.70%
deferral_payments , 0.22%
from_messages , 0.16%
restricted_stock_deferred , 0.06%

% 不变。

【问题讨论】:

    标签: python machine-learning scikit-learn jupyter-notebook


    【解决方案1】:

    scores_ 是根据SelectKBest 中提供的score_func 参数在完整功能上生成的:

    score_func:采用两个数组 X 和 y 的可调用函数,以及 返回一对数组(分数,pvalues)或单个数组 分数。默认为 f_classif(参见下文“另见”)。默认 函数仅适用于分类任务。

    默认情况下,SelectKBest 将使用f_classif

    由于提供给它的功能不会改变,所以分数不会改变。将改变的是根据这些分数选择了多少这些特征。根据您选择的 k 值,将选择最热门的功能。

    您可以使用get_support() 方法查看选择了哪些功能。

    【讨论】:

      猜你喜欢
      • 2021-06-04
      • 2021-03-27
      • 2017-06-30
      • 2019-08-03
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      • 2017-12-13
      • 2017-03-21
      相关资源
      最近更新 更多