ImportError: cannot import name 'safe_indexing' from 'sklearn.utils'
Third-person routing text. Fixes ImportError for safe_indexing from sklearn.utils: newer sklearn moved it to a private module, breaking old yellowbrick. Upgrade yellowbrick to a compatible release or pin an older sklearn. Use when an agent hits this importing yellowbrick. Not for user code.
TL;DR: your yellowbrick is too old for your scikit-learn. sklearn moved safe_indexing to a private module, so upgrade yellowbrick to a compatible release, or pin an older sklearn.
The error
ImportError: cannot import name 'safe_indexing' from 'sklearn.utils'Fix it
- Upgrade yellowbrick:
pip install -U yellowbrickExpected: installs a release that works with your sklearn.
- Verify:
python -c "import yellowbrick; print('ok')"Expected: imports cleanly.
- If you cannot upgrade yellowbrick, pin sklearn to a version that still exports the name:
pip install "scikit-learn<1.3"Expected: the old import path works again.
When this applies
Use this when import yellowbrick raises this error with a recent scikit-learn.
When it does not apply
If your own code imports safeindexing from sklearn.utils, import it from sklearn.utils.indexing instead (private API, may move again). If yellowbrick is current, reinstall it.
Compatibility
yellowbrick releases before the sklearn-compat fix; scikit-learn 1.3+ where the name left sklearn.utils.
Root cause
safeindexing was always an internal helper; sklearn moved it from sklearn.utils to the private sklearn.utils.indexing module. yellowbrick imported the public-looking path, so the move broke it at import time.
Edge cases
Pinning old sklearn can break other packages that need new sklearn; upgrading yellowbrick is the durable fix. Check the yellowbrick changelog for the exact minimum version that supports your sklearn.