stack: add nns_stack_fit / nns_stack_predict (fit-once, reuse CV/RPM)#134
Merged
Conversation
Adds a real fit/predict split to NNS.stack so the cross-validated model
selection - the dominant cost - runs once and is reused across predictions,
instead of re-running the full out-of-fold CV on every call.
* nns_stack gains an opt-in "fitted" bypass: when supplied, the three CV
fold-loops (dimension count, n.best, ensemble weight) are skipped and the
stored hyperparameters are used; only the (cheap, test-dependent) final
production fit + projection runs. The default (fitted=None) leaves behavior
byte-identical.
* return_fit=True attaches the selected hyperparameters as the "_fit" state;
the default public result dict is unchanged (no new key).
* nns_stack_fit(x, y, ...) runs the selection once and returns a fit object;
nns_stack_predict(fit, newdata) scores new data reusing it.
Verified: nns_stack_predict(fit, newdata) is byte-identical to a full
nns_stack(x, y, newdata) call across method in {(1,2),(1,),(2,)} times
dim_red_method in {cor, equal, NNS.dep} (the selection is test-independent).
Fit-once + 5 predictions runs ~5.4x faster than 5 full stacks (2000x4,
order='max', folds=5). Full invariant suite (346) passes unchanged.
Co-Authored-By: Claude Opus 4.8 <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
Implements the RPM/CV caching item from the performance discussion (#3) as a real fit/predict split for
NNS.stack. The cross-validated model selection — the dominant cost — now runs once and is reused across predictions, instead of re-running the full out-of-fold CV on every call.API
How it works
nns_stackgains an opt-infittedbypass. When supplied, the three CV fold-loops (dimension count,n.best, ensemble weight) are skipped and the stored hyperparameters are used; only the cheap, test-dependent final production fit + X* projection runs. The defaultfitted=Noneleaves behavior byte-identical.return_fit=Trueattaches the selected hyperparameters as a private_fitstate. The default public result dict is unchanged (no new key — the existing key-set contract holds).nns_stack_fitruns the selection once and returns a lightweight fit object (training data + call args +_fit);nns_stack_predictscores new data reusing it.Since the CV selection is test-independent, fitting with no test set yields exactly the hyperparameters a full
nns_stack(x, y, new_x)would select — so predictions are identical, not approximate.Validation
nns_stack_predict(fit, new_x)["stack"]==nns_stack(x, y, new_x)["stack"]acrossmethod ∈ {(1,2),(1,),(2,)}×dim_red_method ∈ {cor, equal, NNS.dep}(atol=1e-9).order="max",folds=5).ruffclean.Notes
nns_stackresult or behavior.NNS.stack(OVVO-Financial/NNS) could take the same additivefitted/return_fitsplit; happy to follow up there if wanted.🤖 Generated with Claude Code
Generated by Claude Code