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

Code before:
    # The above is a special broken case, since distinct values
    # are less than 3, mid-points are automatically calculated
    # and return different values as compared to when using weights
    # since the extra 1 breakes the tie. For now the test is slightly
    # modified, in future we need to decide what to do when
    # distinct_values<=max_bins but sample weights are supplied

    X = [[1, 0], [1, 0], [1, 0], [0, 1], [1, 1], [0, 0]]
    y = [0, 0, 1, 0, 2, -1]
    # ignore the first 2 training samples by setting their weight to 0
    sample_weight = [0, 0, 1, 1, 1, 1]
    gb = HistGradientBoostingClassifier(loss="log_loss", min_samples_leaf=1)
    gb.fit(X, y, sample_weight=sample_weight)
    assert_array_equal(gb.predict([[1, 0]]), [1])


@pytest.mark.parametrize(
    "problem", ("regression", "binary_classification", "multiclass_classification")
)
def test_sample_weight_effect(problem, global_random_seed):
    # High level test to make sure that duplicating a sample is equivalent to
    # giving it weight of 2.

    # Uses subsampling for > int(2e5) without weighting
    # keep n_samples > 255 to make sure bin sampling is used.
    # sure only unique values are used so SW have no effect on binning.
    n_samples = 500
    n_features = 2
    rng = np.random.RandomState(global_random_seed)
    if problem == "regression":
        X, y = make_regression(
            n_samples=n_samples,
            n_features=n_features,
            n_informative=n_features,
            random_state=0,
        )
        Klass = HistGradientBoostingRegressor
    else:
        n_classes = 2 if problem == "binary_classification" else 3
        X, y = make_classification(
            n_samples=n_samples,
            n_features=n_features,
            n_informative=n_features,
            n_redundant=0,
            n_clusters_per_class=1,
            n_classes=n_classes,
            random_state=0,
        )
        Klass = HistGradientBoostingClassifier

    # This test can't pass if min_samples_leaf > 1 because that would force 2

Review comment (suggestion):
This comment seems off. Let me try a rephrasing from my current understanding of the code:


```suggestion
    # This test assumes that subsampling in `_BinMapper` is disabled
    # (when `n_samples < 2e5`) and therefore the binning results should be
    # deterministic.
    # Otherwise, this test would require being rewritten as a statistical test.
    # We also set `n_samples` large enough to ensure that columns have more than
    # 255 distinct values and that we test the impact of weight-aware binning.
```

Code after:
    X = [[1, 0], [1, 0], [1, 0], [0, 1], [1, 1]]
    y = [0, 0, 1, 0, 2]

    # ignore the first 2 training samples by setting their weight to 0
    sample_weight = [0, 0, 1, 1, 1]
    gb = HistGradientBoostingClassifier(loss="log_loss", min_samples_leaf=1)
    gb.fit(X, y, sample_weight=sample_weight)
    assert_array_equal(gb.predict([[1, 0]]), [1])


@pytest.mark.parametrize(
    "problem", ("regression", "binary_classification", "multiclass_classification")
)
def test_sample_weight_effect(problem, global_random_seed):
    # High level test to make sure that duplicating a sample is equivalent to
    # giving it weight of 2.

    # This test assumes that subsampling in `_BinMapper` is disabled
    # (when `n_samples < 2e5`) and therefore the binning results should be
    # deterministic.
    # Otherwise, this test would require being rewritten as a statistical test.
    # We also set `n_samples` large enough to ensure that columns have more than
    # 255 distinct values and that we test the impact of weight-aware binning.
    n_samples = 300
    n_features = 2
    rng = np.random.RandomState(global_random_seed)
    if problem == "regression":
        X, y = make_regression(
            n_samples=n_samples,
            n_features=n_features,
            n_informative=n_features,
            random_state=0,
        )
        Klass = HistGradientBoostingRegressor
    else:
        n_classes = 2 if problem == "binary_classification" else 3
        X, y = make_classification(
            n_samples=n_samples,
            n_features=n_features,
            n_informative=n_features,
            n_redundant=0,
            n_clusters_per_class=1,
            n_classes=n_classes,
            random_state=0,
        )
        Klass = HistGradientBoostingClassifier

    # This test can't pass if min_samples_leaf > 1 because that would force 2
    # samples to be in the same node in est_sw, while these samples would be
    # free to be separate in est_dup: est_dup would just group together the
    # duplicated samples.