Speed up preprocessing scalers and OneHotEncoder#1864
Open
MaxHalford wants to merge 1 commit into
Open
Conversation
- OneHotEncoder: maintain an incremental cache of the all-zeros dict so
transform_one copies it instead of rebuilding {f"{i}_{v}": 0 ...}
every call. ~8x faster transform_one, ~5.5x faster learn+transform on
100k rows x 5 features (cardinality 20).
- StandardScaler: hoist self.counts/self.means/self.vars out of the
inner loop, split the with_std branch, inline safe_div in
transform_one. ~15% faster learn_one. Welford formula unchanged.
- MinMaxScaler / MaxAbsScaler: cache each feature's .get() result in
transform_one (self.min[i].get() was called twice per feature),
inline safe_div, hoist self.min/self.max out of the loop. ~1.3x
faster transform_one. stats.Min/Max/AbsMax are still updated and
read only via their .update()/.get() methods.
Outputs are bit-identical to baseline across a 500-row parity test for
all four classes. All 32 preprocessing tests and 312 check_estimator
framework checks pass.
Co-Authored-By: Claude Opus 4.7 (1M context) <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.
Summary
Profiled and optimized the three preprocessing estimators in scope:
OneHotEncoder.transform_one— ~8.0× faster. The previous implementation rebuilt the all-zeros dict via{f"{i}_{v}": 0 ...}on every call. The encoder now maintains an incremental cache of that zero-dict inlearn_one/learn_many, andtransform_onejust.copy()s it before setting the 1s.StandardScaler— ~15% fasterlearn_one. Hoistedself.counts/self.means/self.varsreferences into locals, split thewith_std=True/with_std=Falsebranches out of the loop, and inlined thesafe_divcall intransform_one(which was a 1M-times-called function for 100k × 10 features). Welford's update formula is unchanged.MinMaxScaler/MaxAbsScaler— ~1.3× fastertransform_one. Cache each feature's.get()result in a local (self.min[i].get()was called twice per feature) and inlinesafe_div.stats.Min/stats.Max/stats.AbsMaxare still updated and read only via their.update()/.get()methods — no internal abstractions were bypassed.Benchmarks (best of 5, 100k samples)
StandardScaler.learn_one(10 feat)StandardScaler.transform_oneStandardScaler.learn+transformMinMaxScaler.transform_one(10 feat)MinMaxScaler.learn+transformOneHotEncoder.transform_one(5 feat, k=20)OneHotEncoder.learn+transformCorrectness
Outputs are bit-identical to baseline across a 500-row parity test for
StandardScaler,MinMaxScaler,MaxAbsScaler, andOneHotEncoder(includingdrop_zeros=True, drop_first=True).Test plan
uv run pytest river/preprocessing— 32 passeduv run pytest river/test_estimators.py -k 'StandardScaler or MinMaxScaler or MaxAbsScaler or OneHotEncoder'— 312check_estimatorframework checks passeduv run pytest river/compose— pipeline integration still passescategories=.../learn_manypaths forOneHotEncoderexercised manually🤖 Generated with Claude Code