简体   繁体   English

在 sklearn.feature_selection 之后过滤 DataFrame

[英]Filter DataFrame after sklearn.feature_selection

I reduce dimensionality of a dataset (pandas DataFrame).我降低了数据集(pandas DataFrame)的维数。

X = df.as_matrix()
sel = VarianceThreshold(threshold=0.1)
X_r = sel.fit_transform(X) 

then I wanto to get back the reduced DataFrame (ie keep only ok columns)然后我想取回减少的 DataFrame(即只保留 ok 列)

I found only this ugly way to do so, which is very inefficient, do you have any cleaner idea?我发现只有这种丑陋的方法,这是非常低效的,你有什么更清晰的想法吗?

    cols_OK = sel.get_support()  # which columns are OK?
    c = list()
    for i, col in enumerate(cols_OK):
        if col:
            c.append(df.columns[i])
    return df[c]

I think you need if return mask :我认为你需要 if return mask

cols_OK = sel.get_support()
df = df.loc[:, cols_OK]

and if return indices:如果返回索引:

cols_OK = sel.get_support()
df = df.iloc[:, cols_OK]

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

相关问题 自动功能选择-Sklearn.feature_selection - Automatic feature selection - Sklearn.feature_selection F_sklearn.feature_selection的回归 - F_Regression from sklearn.feature_selection sklearn.feature_selection chi2 为不同的标签识别相同的一元和二元 - sklearn.feature_selection chi2 identifies same unigrams and bigrams for different labels sklearn.feature_selection中chi2的“ ValueError:长度必须匹配才能进行比较” - “ValueError: Lengths must match to compare” for chi2 from sklearn.feature_selection 如何解释sklearn.feature_selection中多类的_coeffs输出的特征重要性? - How to interpret importance of features from _coeffs outputs for multi-class in sklearn.feature_selection? sklearn单变量特征选择 - sklearn univariate feature selection sklearn数据集中的特征选择问题 - The problem of feature selection in sklearn dataset Sklearn Chi2用于特征选择 - Sklearn Chi2 For Feature Selection 传递给 sklearn.model_selection.cross_validate 时,来自 DataFrame 的选定特征具有不同的长度? - selected feature from DataFrame has a different length when passing to sklearn.model_selection.cross_validate? 如何在 python 的 sklearn 中使用 gridsearchcv 执行特征选择 - How to perform feature selection with gridsearchcv in sklearn in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM