diff --git a/pretab/core/selectors.py b/pretab/core/selectors.py index 85b43db..2cf6b6e 100644 --- a/pretab/core/selectors.py +++ b/pretab/core/selectors.py @@ -319,6 +319,12 @@ class LightGBMLocationSelector(BaseLocationSelector): split thresholds are ranked by their cumulative gain across all trees. This tends to find informative locations that a single tree can miss. + For ``task="classification"`` the target is label-encoded first, so any class + labels work (for example ``{1, 2}`` or strings, as with the CART selector). + Two classes are fitted with LightGBM's ``binary`` objective, more with its + ``multiclass`` objective; a target with a single class has no split to learn + and falls back to unsupervised locations. + Requires the optional ``lightgbm`` dependency, installable with ``pip install pretab[lightgbm]``. @@ -370,9 +376,21 @@ def _import_lightgbm(): def _ordered_candidates(self, x_valid: np.ndarray, y_valid: np.ndarray, task: Task) -> tuple[list[float], object]: lgb = self._import_lightgbm() + if task == "regression": + objective = {"objective": "regression", "metric": "rmse"} + else: + # LightGBM needs integer class codes: its binary objective treats every + # label > 0 as positive and it cannot read string labels. + classes, y_valid = np.unique(y_valid, return_inverse=True) + if len(classes) < 2: + return [], None + if len(classes) == 2: + objective = {"objective": "binary", "metric": "binary_logloss"} + else: + objective = {"objective": "multiclass", "metric": "multi_logloss", "num_class": len(classes)} + params = { - "objective": "binary" if task == "classification" else "regression", - "metric": "binary_logloss" if task == "classification" else "rmse", + **objective, "num_leaves": 2**self.max_depth, "max_depth": self.max_depth, "learning_rate": self.learning_rate, diff --git a/tests/core/test_location_selectors.py b/tests/core/test_location_selectors.py index 5ec5354..a910e60 100644 --- a/tests/core/test_location_selectors.py +++ b/tests/core/test_location_selectors.py @@ -180,6 +180,36 @@ def test_supplement_preserves_high_end_split_end_to_end(): assert locations.max() > 5.0, f"expected a high-end location to survive supplementing, got {locations}" +@pytest.mark.parametrize( + "make_target", + [ + pytest.param(lambda x: np.where(x < 0.9, 1, 2), id="labels-1-2"), + pytest.param(lambda x: np.digitize(x, [0.1, 0.9]), id="three-classes"), + pytest.param(lambda x: np.where(x < 0.9, "no", "yes"), id="string-labels"), + ], +) +def test_lightgbm_classification_places_a_location_at_the_class_boundary(make_target): + """Regression guard for issue #61: the binary objective on raw labels lost the + supervision for non-0/1, multiclass and string targets.""" + pytest.importorskip("lightgbm") + x = np.random.default_rng(0).uniform(0, 1, size=1000) + locations = LightGBMLocationSelector().select( + x.reshape(-1, 1), make_target(x), task="classification", min_count=2, max_count=2 + ) + assert np.abs(locations - 0.9).min() < 0.01, locations + + +def test_lightgbm_classification_with_a_single_class_falls_back(): + """A single-class target has no boundary to learn: the selector falls back to + unsupervised locations instead of failing on the (string) labels.""" + pytest.importorskip("lightgbm") + x = np.linspace(0, 1, 200) + locations = LightGBMLocationSelector().select( + x.reshape(-1, 1), np.full(200, "yes"), task="classification", min_count=2, max_count=2 + ) + np.testing.assert_allclose(locations, [1 / 3, 2 / 3]) + + # --- tied / discrete features (issue #57) ------------------------------------ @@ -195,6 +225,16 @@ def _assert_distinct_interior(locations, X, count): assert locations.min() > X.min() and locations.max() < X.max() +@pytest.mark.parametrize("levels", [2, 5]) +def test_lightgbm_single_class_tied_feature_keeps_fixed_width(levels): + pytest.importorskip("lightgbm") + X, _ = _discrete(levels) + locations = LightGBMLocationSelector().select( + X, np.full(len(X), "yes"), task="classification", min_count=8, max_count=8 + ) + _assert_distinct_interior(locations, X, 8) + + @pytest.mark.parametrize("selector_cls", [CARTLocationSelector, LightGBMLocationSelector]) @pytest.mark.parametrize("levels", [2, 3, 5]) def test_selector_tops_up_tied_features_with_distinct_interior_locations(selector_cls, levels): @@ -225,9 +265,10 @@ def test_supplement_fills_dense_requests_beyond_the_spacing(): @pytest.mark.parametrize("task", ["regression", "classification", None]) -def test_small_sample_fallback_on_tied_data_is_distinct_and_interior(task): +@pytest.mark.parametrize("selector_cls", [CARTLocationSelector, LightGBMLocationSelector]) +def test_small_sample_fallback_on_tied_data_is_distinct_and_interior(task, selector_cls): X = np.array([[0.0], [0.0], [0.0], [1.0], [1.0]]) - locations = CARTLocationSelector().select(X, np.arange(5.0), task=task, min_count=3, max_count=3) + locations = selector_cls().select(X, np.arange(5.0), task=task, min_count=3, max_count=3) _assert_distinct_interior(locations, X, 3)