[python-package] remove hard dependency on 'scikit-learn', fix minimal runtime dependencies #5942
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.
Today,
pip install lightgbm
absolutely requires that you're able to also installnumpy
,scipy
, andscikit-learn
(and therefore all of their recursive dependencies).This PR proposes removing the hard requirement on
scikit-learn
, so that users who aren't using thelightgbm.sklearn
interface don't have to pay the download time, disk usage, and incompatibility-risk cost of having to installscikit-learn
.scikit-learn
is already used conditionally throughout the package...LightGBM/python-package/lightgbm/compat.py
Lines 71 to 73 in 2e603f8
LightGBM/python-package/lightgbm/sklearn.py
Lines 531 to 533 in 2e603f8
... so this is only a change to the Python package's dependency metadata and doesn't require any changes to the library code.
While doing this, I also discovered that the changes from #5759 broke the required dependencies, such that
pip install lightgbm
wouldn't automatically installnumpy
andscipy
if they weren't present. This PR fixes that as well.How I tested this
Built the wheel (just using
--nomp
to make compilation a little faster, it's irrelevant to this test):docker run \ --rm \ -v $(pwd):/opt/lgb-build \ -w /opt/lgb-build \ python:3.11 \ sh ./build-python.sh bdist_wheel --nomp
Confirmed that
pip install lightgbm
pulls in justnumpy
andscipy
Confirmed that
pip install lightgbm[scikit-learn]
works, and pulls innumpy
,scikit-learn
,scipy
, and all ofscikit-learn
's other dependencies.Notes for Reviewers
I called this
breaking
because if anyone is currently relying onpip install lightgbm
pulling inscikit-learn
(e.g. they aren't specifying thescikit-learn
dependency separately), their setup will break when upgrading tolightgbm
4.0. I think that's acceptable in exchange for making it possible to build a lighter-weight, less-risky-to-build, Python environment using justlightgbm
,numpy
, andscipy
.I got the idea for this by observing that
xgboost
does the same thinghttps://github.com/dmlc/xgboost/blob/54da4b31856625e9cca1848e1aa8ab8bf584e5fe/python-package/pyproject.toml#L30-L33
https://github.com/dmlc/xgboost/blob/54da4b31856625e9cca1848e1aa8ab8bf584e5fe/python-package/pyproject.toml#L41