Thanks to visit codestin.com
Credit goes to github.com

Skip to content

AttributeError: This 'LabelEncoder' has no attribute 'set_output' #26711

Open
@Clarit7

Description

@Clarit7

Describe the bug

I tried to call 'set_output' from LabelEncoder object and got the AttributeError.

The document says sklearn.preprocessing.LabelEncoder has 'set_output' method, but it was not working.

Soon I found most of other 'set_output' available estimators inherits both of sklearn.base.OneToOneFeatureMixin and sklearn.base.TransformerMinxin

Howerver, LabelEncoder only inherits the TransformerMinxin.

class LabelEncoder(TransformerMixin, BaseEstimator):

Function 'set_output' seems available when '_auto_wrap_is_configured' is True. (utils._set_output.py)

    @available_if(_auto_wrap_is_configured)
    def set_output(self, *, transform=None):
        """Set output container.

        See :ref:`sphx_glr_auto_examples_miscellaneous_plot_set_output.py`
        for an example on how to use the API.

        Parameters
        ----------
        transform : {"default", "pandas"}, default=None
            Configure output of `transform` and `fit_transform`.

            - `"default"`: Default output format of a transformer
            - `"pandas"`: DataFrame output
            - `None`: Transform configuration is unchanged

        Returns
        -------
        self : estimator instance
            Estimator instance.
        """
        if transform is None:
            return self

        if not hasattr(self, "_sklearn_output_config"):
            self._sklearn_output_config = {}

        self._sklearn_output_config["transform"] = transform
        return self

Then estimator should have 'get_feature_names_out' to make '_auto_wrap_is_configured' returns True. (utils._set_output.py)

def _auto_wrap_is_configured(estimator):
    """Return True if estimator is configured for auto-wrapping the transform method.

    `_SetOutputMixin` sets `_sklearn_auto_wrap_output_keys` to `set()` if auto wrapping
    is manually disabled.
    """
    auto_wrap_output_keys = getattr(estimator, "_sklearn_auto_wrap_output_keys", set())
    return (
        hasattr(estimator, "get_feature_names_out")
        and "transform" in auto_wrap_output_keys
    )

To have 'get_feature_names_out' attr, estimator should inherit OneToOneFeatureMixin as I think. (base.py)

class OneToOneFeatureMixin:
    """Provides `get_feature_names_out` for simple transformers.

    This mixin assumes there's a 1-to-1 correspondence between input features
    and output features, such as :class:`~preprocessing.StandardScaler`.
    """

    def get_feature_names_out(self, input_features=None):
        """Get output feature names for transformation.

        Parameters
        ----------
        input_features : array-like of str or None, default=None
            Input features.

            - If `input_features` is `None`, then `feature_names_in_` is
              used as feature names in. If `feature_names_in_` is not defined,
              then the following input feature names are generated:
              `["x0", "x1", ..., "x(n_features_in_ - 1)"]`.
            - If `input_features` is an array-like, then `input_features` must
              match `feature_names_in_` if `feature_names_in_` is defined.

        Returns
        -------
        feature_names_out : ndarray of str objects
            Same as input features.
        """
        check_is_fitted(self, "n_features_in_")
        return _check_feature_names_in(self, input_features)

I want to know that it is kind of a bug, or the document says wrong information.

Steps/Code to Reproduce

from sklearn.preprocessing import LabelEncoder

LabelEncoder().set_output()

Expected Results

No error is thrown

Actual Results

--------------------------------------------------------------------------------
AttributeError                                 Traceback (most recent call last)                        
/tmp/ipykernel_16765/3091610596.py in <module>
---> 1 LabelEncoder().set_output()

~/.local/lib/python3.8/site-packages/sklearn/utils/_available_if.py in __get__(self, obj, owner)
      31        # this is to allow access to the docstrings.
      32        if not self.check(obj):
--->  33            raise attr_err
      34        out = MethodType(self.fn, obj)
      35
AttributeError: This 'LabelEncoder' has no attribute 'set_output'

Versions

1.2.2

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Moderate

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions