You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is inspired by #14034 but we had several similar issues of the years.
The current design of random_state is often hard to understand and confusing for users.
I think we should rethink how random_state works. Maybe that's a 1.0 issue, I'm not sure.
What I find most confusing is the behavior of passing a RandomState object to random_state, because that makes the object be statefull across calls to fit, so it violates our contract of fit being idempotent. Because fit (or possibly even predict?) consumes the random state object it's mutated.
I don't see a real use-case for passing a random state object and think we might want to deprecate that. At least we should never store it imho.
There have been countless bugs because of this, and I think they are pretty easily avoidable.
Another question is the behavior of random_state=None which can also be confusing. Repeated calls to fit result in different models. Sometimes that's good, sometimes that's bad. There would be ways to change this, but I'm not sure if it's a good idea.
The bug in #14034 is also present when random_state=None, so just deprecating passing RandomState would not avoid these kinds of bugs in the future.
Reacted by lesshaste, Roman Yurchak and Cove Geary
I am +1 on deprecating RandomState object as input and keeping random_state=None for now. I do not think passing RandomState is common, thus deprecating it should not be too painful.
+1 to deprecating the support of RandomState instance. Numpy now has a new RNG API with numpy.random.Generator and there is a chance people start using that, so instead supporting neither is probably simpler.
I'm not sure how random_state=None would be handled in that case though.
We would still need to use the numpy singleton rng to get a new seed at each time, no? Otherwise it's not easy to have a python program that gives different outcome whenever you launch it.
So the questions boils down to two options:
A. do what we do now: instantiate an RNG instance at each call to fit
B. grab an int seed from the singleton at construction time and store the integer seed as self.random_state (no trailing _) in the __init__ of the estimator.
Option A violates fit-idem-potency but guarantees that we do not change the random_state provided in the constructor of the estimator.
Option B provides of fit-idem-potency but violates the non-mutation of __init__ params. But maybe this is fine.
B. grab an int seed from the singleton at construction time and store the integer seed as self.random_state (no trailing _) in the init of the estimator.
Why does it have to be at construction time? Can't we do it in .fit only the first time (i.e. when random_state=None), in following calls it will be an int, and we can then initialize an RNG as a local variable from it.
Hmm, I suppose it's about which rules we are happy to relax. So if I understand correctly with C) if you init an estimator then clone it, the .fit is not going to use the same RNG on the clone. So in that sense it's somewhat similar to A).
Maybe mutating __init__ for random state is OK then I don't know. Do you remember the initial motivation for not allowing it?
Maybe mutating __init__ for random state is OK then I don't know. Do you remember the initial motivation for not allowing it?
Because we want to have __init__ and set_params to behave consistently to ease the assumption we can make about parametrize model and integration with hyperparam selection tools. But maybe we can special case random_state=None.
Another option is to change how RandomState is handled, by copying the
state in fit rather than mutating it.
This option can also be used to deal with random_state=None or our current situation. Although this would mean storing RandomState.get_state, which contains 624 unsigned ints.
Activity
thomasjpfan commentedon Jun 9, 2019
I am +1 on deprecating
RandomStateobject as input and keepingrandom_state=Nonefor now. I do not think passingRandomStateis common, thus deprecating it should not be too painful.jnothman commentedon Jun 11, 2019
rth commentedon Jun 11, 2019
Are you sure about that? A quick search yields that,
random_state=rng,If we can fix this without deprecating
random_state=rngas suggested above that might be preferrable?rth commentedon Sep 11, 2019
+1 to deprecating the support of
RandomStateinstance. Numpy now has a new RNG API withnumpy.random.Generatorand there is a chance people start using that, so instead supporting neither is probably simpler.I'm not sure how
random_state=Nonewould be handled in that case though.ogrisel commentedon Sep 13, 2019
About the
random_state=Nonecase.We would still need to use the numpy singleton rng to get a new seed at each time, no? Otherwise it's not easy to have a python program that gives different outcome whenever you launch it.
So the questions boils down to two options:
A. do what we do now: instantiate an RNG instance at each call to fit
B. grab an int seed from the singleton at construction time and store the integer seed as
self.random_state(no trailing_) in the__init__of the estimator.Option A violates
fit-idem-potency but guarantees that we do not change therandom_stateprovided in the constructor of the estimator.Option B provides of
fit-idem-potency but violates the non-mutation of__init__params. But maybe this is fine.rth commentedon Sep 13, 2019
Why does it have to be at construction time? Can't we do it in
.fitonly the first time (i.e. whenrandom_state=None), in following calls it will be an int, and we can then initialize an RNG as a local variable from it.Which should satisfy both conditions I think?
ogrisel commentedon Sep 13, 2019
But then it would mean than
fitdoes not forget previous calls tofit(even whenwarm_start=False) which breaks the contract forsklearn.base.clone.rth commentedon Sep 13, 2019
Hmm, I suppose it's about which rules we are happy to relax. So if I understand correctly with C) if you init an estimator then clone it, the .fit is not going to use the same RNG on the clone. So in that sense it's somewhat similar to A).
Maybe mutating
__init__for random state is OK then I don't know. Do you remember the initial motivation for not allowing it?ogrisel commentedon Sep 13, 2019
Because we want to have
__init__andset_paramsto behave consistently to ease the assumption we can make about parametrize model and integration with hyperparam selection tools. But maybe we can special caserandom_state=None.thomasjpfan commentedon Sep 13, 2019
This option can also be used to deal with
random_state=Noneor our current situation. Although this would mean storingRandomState.get_state, which contains 624 unsigned ints.NicolasHug commentedon Oct 7, 2019
Another subtle issue caused by
random_state=Noneis #15149NicolasHug commentedon Nov 27, 2019
I have opened a SLEP for fixing this: scikit-learn/enhancement_proposals#24
adrinjalali commentedon Aug 22, 2021
We seem kind of stuck on this one. I'd be happy to see support for the new numpy generators, but removing the milestone for now.