0

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

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

4

1 回答 1

0

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

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 {}
于 2021-02-11T20:31:14.940 回答