perf: defer scipy.stats, pandas and scipy.special imports - #34265
Merged
Conversation
…t sklearn` These modules sit on the default `import sklearn` path (via `sklearn.base` -> `sklearn.utils`) but none of them are needed at import time: - `sklearn/utils/fixes.py` imported `scipy.stats` (used only by `_mode`) and `pandas` (imported but never used in the module nor re-exported). - `sklearn/utils/_array_api.py` imported `scipy.special` (used only by `_expit` / `_logit`). Deferring these into the functions that use them removes `pandas`, `scipy.stats` and `scipy.special` from the default import path. A bare `import sklearn` becomes ~2.7x faster on a warm cache, and `pandas` no longer loads on any inference-only workload. Relates to scikit-learn#25590 Relates to scikit-learn#29799
cakedev0
approved these changes
Jun 12, 2026
cakedev0
left a comment
Contributor
There was a problem hiding this comment.
LGTM, might benefit from commenting the imports to explain why lazy is helpful
|
|
||
| # TODO: Remove when SciPy 1.11 is the minimum supported version | ||
| def _mode(a, axis=0): | ||
| import scipy.stats |
Contributor
There was a problem hiding this comment.
Maybe a small comment so it's not undone in 2 months? :p
Suggested change
| import scipy.stats | |
| import scipy.stats # lazy import to reduce sklearn import overhead |
thomasjpfan
enabled auto-merge (squash)
June 12, 2026 13:06
prady0t
pushed a commit
to prady0t/scikit-learn
that referenced
this pull request
Sep 2, 2026
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.
It's small micro-optimisation to reduce the import time both for
import sklearnand also when running inference of some of the most propular models. The use case is typically training a model somewhere and then runnning the inference in some resource constrained environement where cold start matters (lambda like, wasm etc)Relates to #25590
This defers scipy.stats, pandas and scipy.special imports .
These modules sit on the default
import sklearnpath (viasklearn.base->sklearn.utils) but none of them are needed at import time:sklearn/utils/fixes.pyimportedscipy.stats(used only by_mode) andpandas(imported but never used in the module nor re-exported).sklearn/utils/_array_api.pyimportedscipy.special(used only by_expit/_logit).I did quick benchmarks here for typical inference scenarios import_time.zip and the results below are import times for these cases (on M4 CPU, it will be more noticable on a slower system)
cc @ogrisel @jeremiedbb