fix(classifier): encode class labels to 0..K-1 for training - #434
Open
ChrisW09 wants to merge 1 commit into
Open
fix(classifier): encode class labels to 0..K-1 for training#434ChrisW09 wants to merge 1 commit into
ChrisW09 wants to merge 1 commit into
Conversation
Raw label values were passed straight into the loss. classes_ is set to
np.unique(y) and predict() maps model output indices back through it,
but nothing ever produced those indices: no LabelEncoder, no
return_inverse, no searchsorted anywhere in the package.
Three failure modes, all reproduced:
- String labels crash in the preprocessor (PLE's decision-tree binning
calls float(y)), so sklearn-style string labels were unusable.
- Non-contiguous integer labels (e.g. {10,20,30}) send out-of-range
targets to CrossEntropy: IndexError on CPU, and on MPS -- which has no
bounds check -- training silently proceeds on garbage.
- Binary labels other than {0,1} silently train BCEWithLogitsLoss
against targets like 5.0/7.0; a perfectly separable dataset with
labels {5,7} reached 47.75% accuracy versus 100% with {0,1}.
fit() and build_model() now encode y (and y_val, raising a clear error
for validation labels unseen during fit) through the sorted classes_
array, completing the round trip predict() already assumed. Class-weight
resolution still sees the raw labels, so {label: weight} mappings keep
working and the resulting weight vector order matches the encoded
indices.
Fixes #409
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #409
Problem
SklearnBaseClassifier.fitsetsself.classes_ = np.unique(y)andpredict()maps argmax indices back through it — but nothing ever convertsyinto those indices. The raw values flow through_build_model→TabularDataModule.preprocess_data→torch.tensor(y_train, dtype=torch.long)and are used directly as CrossEntropy indices / BCE targets. There is no LabelEncoder,return_inverse, orsearchsortedanywhere in the package.Three reproduced failure modes:
{"yes","no"}ValueError: could not convert string to floatin the preprocessor{10,20,30}IndexError: Target 10 is out of boundson CPU; silently trains on garbage on MPS (no bounds check){5,7}{0,1}Only
yalready coded as0..K-1worked, which is why the suite never caught it.Fix
A small
_encode_labelshelper maps labels to contiguous indices via the sortedclasses_array (np.searchsorted), applied in bothfit()andbuild_model().y_valis encoded with the same mapping and raises a clearValueErrorlisting any validation labels unseen during fit.Class-weight resolution deliberately still runs on the raw labels, so
class_weight={label: weight}mappings keep working —compute_class_weightsorders its output by the same sortedclasses_, so the weight vector already matches the encoded indices.Tests
New
tests/test_label_encoding.py: binary and multiclass string labels round-trip throughpredict/predict_proba, non-contiguous integers work,{5,7}on separable data now exceeds 90% accuracy, unseeny_vallabels raise, and plain0..K-1labels are unaffected.tests/test_class_imbalance.pyandtests/test_models.pypass unchanged (219 passed).🤖 Generated with Claude Code