相关疑难解决方法(0)

在Scikit Learn中运行SelectKBest后获取功能名称的最简单方法

我想进行有监督的学习.

到现在为止,我知道要对所有功能进行监督学习.

但是,我还想进行K最佳功能的实验.

我阅读了文档,发现在Scikit中学习了SelectKBest方法.

不幸的是,我不确定在找到这些最佳功能后如何创建新的数据帧:

我们假设我想进行5个最佳功能的实验:

from sklearn.feature_selection import SelectKBest, f_classif
select_k_best_classifier = SelectKBest(score_func=f_classif, k=5).fit_transform(features_dataframe, targeted_class)
Run Code Online (Sandbox Code Playgroud)

现在,如果我要添加下一行:

dataframe = pd.DataFrame(select_k_best_classifier)
Run Code Online (Sandbox Code Playgroud)

我将收到一个没有功能名称的新数据帧(只有索引从0到4开始).

我应该把它替换为:

dataframe = pd.DataFrame(fit_transofrmed_features, columns=features_names)
Run Code Online (Sandbox Code Playgroud)

我的问题是如何创建features_names列表?

我知道我应该使用:select_k_best_classifier.get_support()

返回布尔值数组.

数组中的真值表示右列中的索引.

我应该如何使用这个布尔数组与我可以通过该方法获得的所有功能名称的数组:

feature_names = list(features_dataframe.columns.values)
Run Code Online (Sandbox Code Playgroud)

python feature-selection pandas scikit-learn

38
推荐指数
4
解决办法
3万
查看次数

标签 统计

feature-selection ×1

pandas ×1

python ×1

scikit-learn ×1