首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sklearn passthrough特征选择器

sklearn passthrough特征选择器
EN

Stack Overflow用户
提问于 2021-02-11 16:28:44
回答 1查看 60关注 0票数 0

我正在使用sklearn管道,并希望有一个功能选择步骤,可以设置为无功能选择。有没有什么也不做的sklearn.feature_selection.SelectorMixin对象?

编辑:或者至少有一个模板可以开发一个模板,比如可以有一个估计器?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-12 04:31:15

一天结束后,我选择了这样的东西,它似乎符合我现在的目的。但不确定它是否会被验证为正确的sklearn.feature_selection选择器:

代码语言:javascript
复制
import numpy as np
import pandas as pd
from sklearn.utils.validation import check_is_fitted


class PassThroughSelector():
    """
    Simply selects all columns of the dataframe, allowing
    to have the equivalent of no selector without changing
    too much of the structure.

    Args:
    """

    def __init__(self):
        pass
    
    def fit(self, x, y=None):  # pylint:disable=unused-argument, arguments-differ
        """
        Stores a list of selected columns.
        Args:
            x: training data
            y: training y (no effect)
        Returns:
            self
        """
        self.check_x(x)
        mask = np.where(np.ones(len(x.columns)), True, False)
        self.support_ = mask
        self.selected_features_ = x.columns[self.support_].tolist()
        return self

    def get_support(self, indices = False) -> np.ndarray:
        """Provides a boolean mask of the selected features."""
        check_is_fitted(self)
        if indices == True:
            return np.array([i for i in range(len(self.support_)) if self.support_[i]==True])
        else:
            return self.support_

    def transform(self, x: pd.DataFrame):
        """Selects the features selected in `fit` from a provided dataframe."""
        check_is_fitted(self)
        self.check_x(x)
        return x.loc[:, self.support_]

    def get_params(self, deep = True):
        return {}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66151158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档