Repository navigation
[python-package] Accept numpy generators as random_state - #6174
Conversation
jameslamb
left a comment
There was a problem hiding this comment.
Thanks so much for this! I definitely support it, but think some changes are needed in this PR.
Lots of other software such as SciPy have moved towards Generator
In the future, we'd appreciate if claims like this were supported with some evidence. I do see scipy/scipy#11680 from 3 years ago, looks like scipy has been supporting this for a while.
Although I think it'd be more accurate to say they "added support for" Generator than "moved to" it, as scipy still supports RandomState:
RandomStateis deprecated
That may be true, but I don't support raising lightgbm's oldest support numpy version just to support it.
As of this writing that's numpy==1.16.6:
It looks to me that np.random.Generator didn't make it into numpy until 1.17.0:
That's why the CI job where we test lightgbm's compatibility with the oldest versions it supports is failing on this PR:
Traceback (most recent call last):
File "./examples/python-guide/advanced_example.py", line 11, in <module>
import lightgbm as lgb
File "/usr/local/lib/python3.6/site-packages/lightgbm/__init__.py", line 13, in <module>
from .sklearn import LGBMClassifier, LGBMModel, LGBMRanker, LGBMRegressor
File "/usr/local/lib/python3.6/site-packages/lightgbm/sklearn.py", line 391, in <module>
class LGBMModel(_LGBMModelBase):
File "/usr/local/lib/python3.6/site-packages/lightgbm/sklearn.py", line 414, in LGBMModel
importance_type: str = 'split',
AttributeError: module 'numpy.random' has no attribute 'Generator'
Can you please introduce this in a way that's backwards-compatible with versions of numpy that didn't yet have np.random.Generator? That'd mean the following changes:
- add a block in
compat.pywhich defines anp_random_Generatorwithin a try-catch block, like this:
- use that
compat.np_random_Generatorin code here, similar to howcompat.pd_DataFrameis used:
- put all of the type hints touched in this PR in double quotes, so their evaluation will be deferred to type-checking time and not cause import errors in environments with older versions of
numpy
like this:
as described in https://mypy.readthedocs.io/en/stable/runtime_troubles.html
|
|
||
|
|
||
| def test_random_state_object(): | ||
| @pytest.mark.parametrize("rng_constructor", [np.random.RandomState, np.random.default_rng]) |
There was a problem hiding this comment.
forgot to add in my review... thanks very much for adding this to this test!
|
Added a compat entry and quoted type hints for running with older numpy. |
|
I'm not sure why the linter is complaining about seemingly unrelated files. This is what I found in the logs: None of those files are being modified here. |
|
Those That job is failing on this PR for reasons unrelated to this PR, caused by the latest release of |
jameslamb
left a comment
There was a problem hiding this comment.
Thanks for the changes! This looks good to me. I'll take care of updating it to latest master and merging it once the unrelated CI issues are fixed.
|
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 PR adds support for numpy generators as possible inputs for
random_state. Generators are the recommended way of drawing random numbers from numpy, whileRandomStateis deprecated. Lots of other software such as SciPy have moved towardsGeneratorand/or allow both (example:scipy.sparse.random).