Conversation
…ocessing trees do
Move validation and ordinal encoding of categorical features to sklearn/tree/_preprocessing.py, used by trees, forests, GB and HGB. Encoded columns are written directly into a Fortran-ordered array in the original column order, without ColumnTransformer. HGB no longer moves categorical features first, which fixes interaction_cst, partial dependence with method="recursion" and the cardinality error message when a categorical feature is not first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Split the sample indices instead of X, bin X once with thresholds fitted on the training samples only (new sample_indices parameter of _BinMapper.fit), then split the binned data column by column. This avoids a float64 copy of X: peak memory is 2.3 to 4 times lower and fit is slightly faster. Fitted models are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Owner
Author
|
Possible follow-ups:
|
Owner
Author
|
@adam2392: heads-up about this PR. I'd like to open it on the official fork once yours (scikit-learn#34832) is merged. I think it's really good: removes a lot a duplication, adds features, fixes bugs, moves things towards unifying GB and HGB. |
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.
Reference Issues/PRs
Built on top of
feat/cat-forest(categorical support in forests, scikit-learn#34832). First step towards binning support for trees and towards unifying GB and HGB (scikit-learn#27873).What does this implement/fix? Explain your changes.
Trees, forests,
GradientBoosting*andHistGradientBoosting*now share the same X validation and categorical encoding, in the newsklearn/tree/_preprocessing.py._validate_and_preprocess_X, then_fit_validatedbuilds the tree. Forests call_fit_validateddirectly, so they don't need to know what kind of preprocessing trees do.ColumnTransformeranymore. This saves time, memory, and fixes bug in HGB (that was not re-ordering columns and had bugs because of that).categorical_featuresand for missing values. GB trees are single-outputsquared_errorregressors, so categorical splits work for all losses, including multi-class.predict_stagesnow uses the tree'sgoes_left: its manualX <= thresholdtraversal ignored categorical splits andmissing_go_to_left.interaction_cstapplied to the wrong features,partial_dependence(method="recursion")computed for the wrong feature, and the cardinality error reporting the wrong feature index.sample_indicesparameter of_BinMapper.fit) and then splits the binned data column by column. This avoids a float64 copy of X. Fitted models are bit-identical.Excluding tests and changelogs: +500 / −610 lines.
AI usage disclosure
I used AI assistance for:
Benchmarks
HGB fit, early stopping with the internal validation split, 20 iterations:
Categorical encoding (500k rows, 16 numerical + 4 categorical pandas columns): trees 124 → 97 ms (the scatter copy after
ColumnTransformeris gone), HGB 85 → 88 ms (theOrdinalEncoderdominates).Any other comments?
warm_startis not supported with categorical features in GB, same as forests.method="recursion": that's fixed separately in FIX raise NotImplementedError for recursion PDP with categorical target features scikit-learn/scikit-learn#34554.XXXXXfor GB,YYYYYfor HGB).🤖 Generated with Claude Code