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

Code before:
            KFold,
            StratifiedGroupKFold,
            StratifiedKFold,
        )
        from sklearn.model_selection._split import check_cv

        _raise_for_params(params, self, "fit_transform")

        X_ordinal, X_known_mask, y_encoded, n_categories = self._fit_encodings_all(X, y)

        if self.target_type_ == "continuous":
            cv = check_cv(
                self.cv,
                y,
                classifier=False,
                shuffle=self.shuffle,
                random_state=self.random_state,
            )
        else:
            cv = check_cv(
                self.cv,
                y,
                classifier=True,
                shuffle=self.shuffle,
                random_state=self.random_state,
            )

        if _routing_enabled():
            if params["groups"] is not None:
                X, y, params["groups"] = indexable(X, y, params["groups"])
            routed_params = process_routing(self, "fit_transform", **params)
        else:
            routed_params = Bunch(splitter=Bunch(split={}))

        # The internal cross-fitting is only well-defined when each sample index
        # appears in exactly one validation fold. Skip the validation check for
        # known non-overlapping splitters in scikit-learn:
        if not isinstance(
            cv, (GroupKFold, KFold, StratifiedKFold, StratifiedGroupKFold)
        ):
            seen_count = np.zeros(X.shape[0])
            for _, test_idx in cv.split(X, y, **routed_params.splitter.split):
                seen_count[test_idx] += 1
            if not np.all(seen_count == 1):
                raise ValueError(
                    "Validation indices from `cv` must cover each sample index exactly "
                    "once with no overlap. Pass a splitter with non-overlapping "
                    "validation folds as `cv` or refer to the docs for other options."
                )

        self.cv_ = cv.__class__.__name__

Review comment (suggestion):
all of this can become:

```py
          cv = check_cv(
              self.cv,
              y,
              classifier=self.target_type_ != "continuous",
              shuffle=self.shuffle,
              random_state=self.random_state,
          )
```

Code after:
        cv = check_cv(
            self.cv,
            y,
            classifier=self.target_type_ != "continuous",
            shuffle=self.shuffle,
            random_state=self.random_state,
        )

        if _routing_enabled():
            if params["groups"] is not None:
                X, y, params["groups"] = indexable(X, y, params["groups"])
            routed_params = process_routing(self, "fit_transform", **params)
        else:
            routed_params = Bunch(splitter=Bunch(split={}))

        # The internal cross-fitting is only well-defined when each sample index
        # appears in exactly one validation fold. Skip the validation check for
        # known non-overlapping splitters in scikit-learn:
        if not isinstance(
            cv, (GroupKFold, KFold, StratifiedKFold, StratifiedGroupKFold)
        ):
            seen_count = np.zeros(X.shape[0])
            for _, test_idx in cv.split(X, y, **routed_params.splitter.split):
                seen_count[test_idx] += 1
            if not np.all(seen_count == 1):
                raise ValueError(
                    "Validation indices from `cv` must cover each sample index exactly "
                    "once with no overlap. Pass a splitter with non-overlapping "
                    "validation folds as `cv` or refer to the docs for other options."
                )

        # If 'multiclass' multiply axis=1 by num classes else keep shape the same
        if self.target_type_ == "multiclass":
            X_out = np.empty(
                (X_ordinal.shape[0], X_ordinal.shape[1] * len(self.classes_)),
                dtype=np.float64,
            )
        else:
            X_out = np.empty_like(X_ordinal, dtype=np.float64)

        for train_idx, test_idx in cv.split(X, y, **routed_params.splitter.split):
            X_train, y_train = X_ordinal[train_idx, :], y_encoded[train_idx]
            y_train_mean = np.mean(y_train, axis=0)

            if self.target_type_ == "multiclass":
                encodings = self._fit_encoding_multiclass(
                    X_train,
                    y_train,
                    n_categories,
                    y_train_mean,
                )