Repository: scikit-learn/scikit-learn
File: sklearn/ensemble/_hist_gradient_boosting/binning.py (Python)

Code before:
        y: None
            Ignored.

        Returns
        -------
        self : object
        """
        if not (3 <= self.n_bins <= 256):
            # min is 3: at least 2 distinct bins and a missing values bin
            raise ValueError(
                "n_bins={} should be no smaller than 3 and no larger than 256.".format(
                    self.n_bins
                )
            )

        X = check_array(X, dtype=[X_DTYPE], ensure_all_finite=False)
        max_bins = self.n_bins - 1

        subsampling_probabilities = None
        if sample_weight is not None:
            subsampling_probabilities = sample_weight / np.sum(sample_weight)

        rng = check_random_state(self.random_state)
        if self.subsample is not None and X.shape[0] > self.subsample:
            subset = rng.choice(
                X.shape[0], self.subsample, p=subsampling_probabilities, replace=False
            )
            X = X.take(subset, axis=0)
            sample_weight = None

        if self.is_categorical is None:
            self.is_categorical_ = np.zeros(X.shape[1], dtype=np.uint8)
        else:
            self.is_categorical_ = np.asarray(self.is_categorical, dtype=np.uint8)

        n_features = X.shape[1]
        known_categories = self.known_categories
        if known_categories is None:
            known_categories = [None] * n_features

        # validate is_categorical and known_categories parameters
        for f_idx in range(n_features):
            is_categorical = self.is_categorical_[f_idx]
            known_cats = known_categories[f_idx]
            if is_categorical and known_cats is None:
                raise ValueError(
                    f"Known categories for feature {f_idx} must be provided."
                )
            if not is_categorical and known_cats is not None:
                raise ValueError(
                    f"Feature {f_idx} isn't marked as a categorical feature, "

Review comment (suggestion):
I think we have to pass `replace=True` to get the statistical equivalence test to pass.

```suggestion
                X.shape[0], self.subsample, p=subsampling_probabilities, replace=True
```

Code after:
        is_categorical=None,
        known_categories=None,
        random_state=None,
        n_threads=None,
    ):
        self.n_bins = n_bins
        self.subsample = subsample
        self.is_categorical = is_categorical
        self.known_categories = known_categories
        self.random_state = random_state
        self.n_threads = n_threads

    def fit(self, X, y=None, sample_weight=None):
        """Fit data X by computing the binning thresholds.

        The last bin is reserved for missing values, whether missing values
        are present in the data or not.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            The data to bin.
        y: None
            Ignored.

        Returns
        -------
        self : object
        """
        if not (3 <= self.n_bins <= 256):
            # min is 3: at least 2 distinct bins and a missing values bin
            raise ValueError(
                "n_bins={} should be no smaller than 3 and no larger than 256.".format(
                    self.n_bins
                )
            )

        X = check_array(X, dtype=[X_DTYPE], ensure_all_finite=False)
        max_bins = self.n_bins - 1
        rng = check_random_state(self.random_state)
        if self.subsample is not None and X.shape[0] > self.subsample:
            subsampling_probabilities = None
            if sample_weight is not None:
                subsampling_probabilities = sample_weight / np.sum(sample_weight)
            # Sampling with replacement to implement frequency semantics
            # for sample weights. Note that we need `replace=True` even when
            # `sample_weight is None` to make sure that passing no weights is
            # statistically equivalent to passing unit weights.
            subset = rng.choice(
                X.shape[0], self.subsample, p=subsampling_probabilities, replace=True
            )