Repository: scikit-learn/scikit-learn
File: sklearn/preprocessing/_target_encoder.py (Python)

Code before:
        self.random_state = random_state

    @_fit_context(prefer_skip_nested_validation=True)
    def fit(self, X, y, **params):
        """Fit the :class:`TargetEncoder` to X and y.

        It is discouraged to use this method because it can introduce data leakage.
        Use `fit_transform` on training data instead.

        .. note::
            `fit(X, y).transform(X)` does not equal `fit_transform(X, y)` because a
            :term:`cross fitting` scheme is used in `fit_transform` for encoding.
            See the :ref:`User Guide <target_encoder>` for details.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            The data to determine the categories of each feature.

        y : array-like of shape (n_samples,)
            The target data used to encode the categories.

        **params : dict
            Always ignored, exists for API compatibility.

            .. versionadded:: 1.9

        Returns
        -------
        self : object
            Fitted encoder.
        """
        self._fit_encodings_all(X, y)
        return self

    @_fit_context(prefer_skip_nested_validation=True)
    def fit_transform(self, X, y, **params):
        """Fit :class:`TargetEncoder` and transform `X` with the target encoding.

        This method uses a :term:`cross fitting` scheme to prevent target leakage
        and overfitting in downstream predictors. It is the recommended method for
        encoding training data.

        .. note::
            `fit(X, y).transform(X)` does not equal `fit_transform(X, y)` because a
            :term:`cross fitting` scheme is used in `fit_transform` for encoding.
            See the :ref:`User Guide <target_encoder>` for details.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)

Review comment (suggestion):
we don't need this, unless, something breaks in a pipeline or a metaestimator calling `fit.transform` instead of `fit_transform`, but I think that already breaks since the two are not equivalent anyway.

Code after:

        It is discouraged to use this method because it can introduce data leakage.
        Use `fit_transform` on training data instead.

        .. note::
            `fit(X, y).transform(X)` does not equal `fit_transform(X, y)` because a
            :term:`cross fitting` scheme is used in `fit_transform` for encoding.
            See the :ref:`User Guide <target_encoder>` for details.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            The data to determine the categories of each feature.

        y : array-like of shape (n_samples,)
            The target data used to encode the categories.

        Returns
        -------
        self : object
            Fitted encoder.
        """
        self._fit_encodings_all(X, y)
        return self

    @_fit_context(prefer_skip_nested_validation=True)
    def fit_transform(self, X, y, **params):
        """Fit :class:`TargetEncoder` and transform `X` with the target encoding.

        This method uses a :term:`cross fitting` scheme to prevent target leakage
        and overfitting in downstream predictors. It is the recommended method for
        encoding training data.

        .. note::
            `fit(X, y).transform(X)` does not equal `fit_transform(X, y)` because a
            :term:`cross fitting` scheme is used in `fit_transform` for encoding.
            See the :ref:`User Guide <target_encoder>` for details.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            The data to determine the categories of each feature.

        y : array-like of shape (n_samples,)
            The target data used to encode the categories.

        **params : dict
            Parameters to route to the internal CV object.

            Can only be used in conjunction with a cross-validation generator as CV
            object.