[python-package] remove hard dependency on 'scikit-learn', fix minimal runtime dependencies#5942
Merged
[python-package] remove hard dependency on 'scikit-learn', fix minimal runtime dependencies#5942
Conversation
…l runtime dependencies
jmoralez
approved these changes
Jun 23, 2023
Contributor
|
This pull request has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this. |
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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 lightgbmabsolutely 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.sklearninterface don't have to pay the download time, disk usage, and incompatibility-risk cost of having to installscikit-learn.scikit-learnis 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 lightgbmwouldn't automatically installnumpyandscipyif they weren't present. This PR fixes that as well.How I tested this
Built the wheel (just using
--nompto 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 --nompConfirmed that
pip install lightgbmpulls in justnumpyandscipydocker run \ --rm \ -v $(pwd):/opt/lgb-build \ -w /opt/lgb-build \ python:3.11 \ /bin/bash -c "pip install --find-links=./dist 'lightgbm' && pip freeze"Confirmed that
pip install lightgbm[scikit-learn]works, and pulls innumpy,scikit-learn,scipy, and all ofscikit-learn's other dependencies.docker run \ --rm \ -v $(pwd):/opt/lgb-build \ -w /opt/lgb-build \ -it python:3.11 \ /bin/bash -c "pip install --find-links=./dist 'lightgbm[scikit-learn]' && pip freeze"Notes for Reviewers
I called this
breakingbecause if anyone is currently relying onpip install lightgbmpulling inscikit-learn(e.g. they aren't specifying thescikit-learndependency separately), their setup will break when upgrading tolightgbm4.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
xgboostdoes 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