Skip to content

RFC design of random_state #14042

Open
Open

Description

@amueller

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.

Activity

  1. thomasjpfan commented on Jun 9, 2019

    @thomasjpfan
    Member

    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.

  2. jnothman commented on Jun 11, 2019

    @jnothman
    Member
  3. rth commented on Jun 11, 2019

    @rth
    Member

    I do not think passing RandomState is common, thus deprecating it should not be too painful.

    Are you sure about that? A quick search yields that,

    • 8 examples in scikit-learn use random_state=rng,
          examples/cluster/plot_dict_face_patches.py
       41:kmeans = MiniBatchKMeans(n_clusters=81, random_state=rng, verbose=True)
       52:                                  random_state=rng)
       
       examples/decomposition/plot_ica_vs_pca.py
       54:ica = FastICA(random_state=rng)
       
       examples/decomposition/plot_faces_decomposition.py
       37:dataset = fetch_olivetti_faces(shuffle=True, random_state=rng)
       84:                                      random_state=rng,
       91:                                                  random_state=rng),
       96:                        max_iter=50, random_state=rng),
       148:                                                  random_state=rng),
       153:                                                  random_state=rng,
       159:                                                  random_state=rng,
       165:                                                  random_state=rng,
       
       examples/ensemble/plot_adaboost_regression.py
       36:                          n_estimators=300, random_state=rng)
       
       examples/ensemble/plot_isolation_forest.py
       43:clf = IsolationForest(max_samples=100, random_state=rng)
       
       examples/linear_model/plot_poisson_spline_regression.py
       62:                           n_estimators=10, random_state=rng)
       
       examples/linear_model/plot_sgd_comparison.py
       48:                train_test_split(X, y, test_size=i, random_state=rng)
       
       examples/preprocessing/plot_map_data_to_normal.py
       56:qt = QuantileTransformer(output_distribution='normal', random_state=rng)
      
      as well as 30 test files in scikit-learn.
    • so does this imbalanced-learn example or this blog.

    If we can fix this without deprecating random_state=rng as suggested above that might be preferrable?

  4. rth commented on Sep 11, 2019

    @rth
    Member

    +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.

  5. ogrisel commented on Sep 13, 2019

    @ogrisel
    Member

    About the random_state=None case.

    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.

  6. rth commented on Sep 13, 2019

    @rth
    Member

    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.

    Which should satisfy both conditions I think?

  7. ogrisel commented on Sep 13, 2019

    @ogrisel
    Member

    But then it would mean than fit does not forget previous calls to fit (even when warm_start=False) which breaks the contract for sklearn.base.clone.

  8. rth commented on Sep 13, 2019

    @rth
    Member

    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?

  9. ogrisel commented on Sep 13, 2019

    @ogrisel
    Member

    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.

  10. thomasjpfan commented on Sep 13, 2019

    @thomasjpfan
    Member

    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.

  11. NicolasHug commented on Oct 7, 2019

    @NicolasHug
    Member

    Another subtle issue caused by random_state=None is #15149

  12. NicolasHug commented on Nov 27, 2019

    @NicolasHug
    Member

    I have opened a SLEP for fixing this: scikit-learn/enhancement_proposals#24

  13. added this to the 1.0 milestone on Feb 4, 2020
  14. adrinjalali commented on Aug 22, 2021

    @adrinjalali
    Member

    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.

  15. removed this from the 1.0 milestone on Aug 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions