question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API: RandomState deprecation

See original GitHub issue

np.random.RandomState is not a good choice for random number generation going forward:

RandomState is effectively frozen and will only receive updates that are required by changes in the the internals of Numpy. More substantial changes, including algorithmic improvements, are reserved for Generator.

I don’t understand all the details, but the TL;DR I’ve gathered from monitoring some SciPy discussions is that it’s not as good as the new default_rng / Generator-based way of making random numbers and it should be replaced in people’s code.

In the long term I think we want to switch random_state=0 to use default_rng(random_state) instead of RandomState(random_state) internally. I propose:

  1. Have a mne.set_rng_backend(backend) that can be 'RandomState' or 'Generator', maybe with a use_rng_backend context manager. The context manager is maybe overkill, but I do think it might help people transition code from old to new, and it’s only a few lines for us.
  2. If this has not been called by the user yet, if they do random_state=<int> they will see a DeprecationWarning stating that the default is 'RandomState' in 0.23 but will change to 'Generator' in 0.24 (or maybe this is a good case for making it 0.25 to give people more time to change over), and we will call set_rng_backend('RandomState').
  3. In 0.24 (or 0.25) we no longer emit this warning and just use 'Generator'.

I think this is a workable solution for modernizing our random_state params.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
rkerncommented, Apr 1, 2021

Hi everyone! I’m the cause of everyone’s problems here. As you work through these issues, I do recommend reading NEP 19 which goes into more detail about why we had the old backwards-compatibility policy and why we moved away from it. The concerns you have brought up here were definitely in our thoughts. I’m happy to answer any questions that the NEP doesn’t answer.

I can’t speak definitively as to when/how/if scipy really deprecates RandomState per se, but I have my guess as to where we’re going on the aforementioned PR. Some time after we drop support for numpy 1.16, I expect that we will transition the code to use Generator instead of RandomState. I expect that we’ll still accept RandomState instances, but we will wrap its core uniform PRNG in Generator and call its methods instead. So if you call scipy.stats.norm.rvs(random_state=RandomState(1234)), you will get the results from Generator.standard_normal()'s improved algorithm rather than the older, slower RandomState.standard_normal(). The core Mersenne Twister PRNG underneath the passed RandomState will still be used, however. Again, we haven’t made any decisions, yet, but I think that’s where we are going.

0reactions
rkerncommented, Apr 2, 2021

Sounds like a good plan.

Read more comments on GitHub >

github_iconTop Results From Across the Web

random.RandomState with different versions of numpy has ...
We should replace RandomState if it's deprecated. What should it be replaced with, is the change 100% backward compatible?
Read more >
numpy.random.RandomState.random_integers
The np.int_ type translates to the C long integer type and its precision is platform dependent. This function has been deprecated. Use randint...
Read more >
XGBRegressor: change random_state no effect - Stack Overflow
... there is an issue in the seed / random_state handling by the current sklearn API in xgboost. seed is indeed claimed to...
Read more >
NXEP 4 — Adopting numpy.random.Generator as default ...
Generator API is very similar to the numpy.random.RandomState API, so users can benefit from these improvements without any additional ...
Read more >
Python API Reference — xgboost 1.7.2 documentation
Save the model to a in memory buffer representation instead of file. Parameters. raw_format (str) – Format of output buffer. Can be json...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found