简体   繁体   English

如何从 sklearn SelectKBest 获取实际选择的功能

[英]How to get the actual selected features from sklearn SelectKBest

Given the following data:给定以下数据:

import pandas as pd
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_regression
import io


df = pd.read_csv(
    io.StringIO(
        "noise_0,x0,x1,y\n1.0322600657764203,10.354468012163927,7.655143584899129,168.06121374114608\n4.478935261759052,8.786243147880384,6.244283164157256,156.570749155167\n9.085955030930956,10.450548129254543,8.084427493431185,152.10261405911672\n2.9361414837367947,10.869778308219216,9.165630427431644,129.72126680171317\n2.877753385863487,11.236593954599316,5.7987616455741575,55.294961794556315\n1.3002857211827767,9.111226379916955,10.289447419679227,308.7475968288771\n0.19366957870297075,9.753313270715008,9.803181441185592,163.337342478704\n6.788355329398909,9.752270042969856,9.004988677803736,271.9442757290742\n2.1162811600005904,8.67161845864426,9.801711898528824,158.09622149503954\n2.655466593722262,8.830913103331573,6.632544281651334,316.23912914041557\n"
    )
)

which looks as:看起来像:

    noise_0         x0         x1           y
0  1.032260  10.354468   7.655144  168.061214
1  4.478935   8.786243   6.244283  156.570749
2  9.085955  10.450548   8.084427  152.102614
3  2.936141  10.869778   9.165630  129.721267
4  2.877753  11.236594   5.798762   55.294962
5  1.300286   9.111226  10.289447  308.747597
6  0.193670   9.753313   9.803181  163.337342
7  6.788355   9.752270   9.004989  271.944276
8  2.116281   8.671618   9.801712  158.096221
9  2.655467   8.830913   6.632544  316.239129

and has correlation matrix并且有相关矩阵


|         |   noise_0 |        x0 |        x1 |         y |
|:--------|----------:|----------:|----------:|----------:|
| noise_0 |  1        |  0.159642 | -0.208966 | -0.02006  |
| x0      |  0.159642 |  1        | -0.197431 | -0.620964 |
| x1      | -0.208966 | -0.197431 |  1        |  0.304241 |
| y       | -0.02006  | -0.620964 |  0.304241 |  1        |

I'm interested how I can find the variable names x0,x1 from sklearns feature selection.我对如何从 sklearns 特征选择中找到变量名x0,x1很感兴趣。

When I try the following:当我尝试以下操作时:

X_new = SelectKBest(f_regression, k=2).fit(df.drop("y", axis=1), df["y"])

I'm expecting this to select x1, x2 , but am not sure how to determine which features were actually selected by it.我期待这是 select x1, x2 ,但我不确定如何确定它实际选择了哪些功能。

SelectKBest provides a get_support() method that can show you which features were selected. SelectKBest提供了一个get_support()方法,可以显示您选择了哪些功能。

Rearrange the code to save the SelectKBest instance:重新排列代码以保存SelectKBest实例:

selector = SelectKBest(f_regression, k=2)
X = df.drop("y", axis=1)
X_new = selector.fit(X, df["y"])

Now, running selector.get_support() will give us:现在,运行selector.get_support()会给我们:

[False,  True,  True]

We can then use selector.get_support() to mask the columns of X:然后我们可以使用selector.get_support()来屏蔽 X 的列:

X.columns.values[selector.get_support()]

for a final output of:对于最终的 output:

['x0', 'x1']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中的sklearn中获取GridSearchCV中的选定功能 - How to get the selected features in GridSearchCV in sklearn in python 如何从sklearn.feature_selection.SelectKBest获得每个功能的分数? - How to get the scores of each feature from sklearn.feature_selection.SelectKBest? 如果 ColumnTransformer 转换器没有 get_feature_names 属性,如何从 SelectKBest() 中获取选定的列? - How to get selected columns from SelectKBest() if ColumnTransformer transformer has not get_feature_names attribute? 如何将 SelectKBest 合并到 SKlearn 管道中 - How do I incorporate SelectKBest in an SKlearn pipeline 当sklearn管道中有多种特征选择方法时如何获取所选特征的名称? - How to get name of selected features when there are several feature selection methods in sklearn pipeline? sklearn:如何获得多项式特征的系数 - sklearn: how to get coefficients of polynomial features 如何使用sklearn获取无用功能列表? - How to get a list of useless features using sklearn? 从 selectKbest 中获取特征名称 - Getting the features names form selectKbest sklearn随机森林查找所选特征的分数 - sklearn random forest to find score of selected features 根据(估计的)功能量选择KBest - SelectKBest based on (estimated) amount of features
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM