From 05203c28e606e3d9d42686b713477084ec1e0ad8 Mon Sep 17 00:00:00 2001 From: ChrisW09 <50968720+ChrisW09@users.noreply.github.com> Date: Fri, 16 Oct 2026 14:18:17 +0200 Subject: [PATCH] fix(placement): label-encode classification targets for LightGBM LightGBMLocationSelector always trained LightGBM's binary objective on the raw labels. Labels other than {0, 1} looked single-class, so no split was found and placement silently fell back to quantiles; multiclass targets only found the class-0 boundary; and string labels raised in fit. For task="classification" the target is now label-encoded and trained with the binary objective for two classes and the multiclass objective (num_class) for more, matching what the CART selector supports. A single-class target has no split to learn and takes the unsupervised fallback instead of failing. Fixes #61 --- pretab/core/selectors.py | 22 ++++++++++++++++++-- tests/core/test_location_selectors.py | 30 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/pretab/core/selectors.py b/pretab/core/selectors.py index f4cef30..41c40b3 100644 --- a/pretab/core/selectors.py +++ b/pretab/core/selectors.py @@ -287,6 +287,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]``. @@ -338,9 +344,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": "regression" if task == "regression" else "binary", - "metric": "rmse" if task == "regression" else "binary_logloss", + **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 e1e6e7f..6c33559 100644 --- a/tests/core/test_location_selectors.py +++ b/tests/core/test_location_selectors.py @@ -160,3 +160,33 @@ def test_supplement_preserves_high_end_split_end_to_end(): locations = CARTLocationSelector().select(xs.reshape(-1, 1), ys, task="regression", min_count=6, max_count=6) 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])