Thanks to visit codestin.com
Credit goes to github.com

Skip to content

perf: defer scipy.stats, pandas and scipy.special imports - #34265

Merged
thomasjpfan merged 2 commits into
scikit-learn:mainfrom
rth:perf/defer-heavy-imports
Jun 12, 2026
Merged

perf: defer scipy.stats, pandas and scipy.special imports #34265
thomasjpfan merged 2 commits into
scikit-learn:mainfrom
rth:perf/defer-heavy-imports

Conversation

@rth

@rth rth commented Jun 12, 2026

Copy link
Copy Markdown
Member

It's small micro-optimisation to reduce the import time both for import sklearn and 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 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).

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)

scenario before after Δ speedup
hist_gradient_boosting 1067 ms 879 ms 182 ms 1.21x
kmeans 1066 ms 891 ms 167 ms 1.20x
random_forest 1058 ms 880 ms 185 ms 1.20x
knn_classifier 977 ms 785 ms 191 ms 1.25x
pca 951 ms 777 ms 171 ms 1.22x
svc 941 ms 740 ms 199 ms 1.27x
pipeline_scaler_logreg 936 ms 739 ms 195 ms 1.27x
linear_regression 931 ms 735 ms 193 ms 1.27x
logistic_regression 928 ms 733 ms 191 ms 1.27x
standard_scaler 771 ms 587 ms 180 ms 1.31x
bare_import 731 ms 264 ms 467 ms 2.77x

cc @ogrisel @jeremiedbb

…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

@jeremiedbb jeremiedbb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks

@cakedev0 cakedev0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, might benefit from commenting the imports to explain why lazy is helpful

Comment thread sklearn/utils/fixes.py Outdated

# TODO: Remove when SciPy 1.11 is the minimum supported version
def _mode(a, axis=0):
import scipy.stats

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 thomasjpfan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@thomasjpfan
thomasjpfan enabled auto-merge (squash) June 12, 2026 13:06
@thomasjpfan
thomasjpfan merged commit 7ba6d38 into scikit-learn:main Jun 12, 2026
36 checks passed
@rth
rth deleted the perf/defer-heavy-imports branch June 12, 2026 14:04
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
@jeremiedbb jeremiedbb mentioned this pull request Sep 8, 2026
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants