scipy.stats seed?

2019-04-04 03:23发布

问题:

I am trying to generate scipy.stats.pareto.rvs(b, loc=0, scale=1, size=1) with different seed.

In numpy we can seed using numpy.random.seed(seed=233423).

Is there any way to seed the random number generated by scipy stats.

Note: I am not using numpy pareto because I want to give different values for scale.

回答1:

scipy.stats just uses numpy.random to generate its random numbers, so numpy.random.seed() will work here as well. E.g.,

import numpy as np
from scipy.stats import pareto
b = 0.9
np.random.seed(seed=233423)
print pareto.rvs(b, loc=0, scale=1, size=5)
np.random.seed(seed=233423)
print pareto.rvs(b, loc=0, scale=1, size=5)

will print [ 9.7758784 10.78405752 4.19704602 1.19256849 1.02750628] twice.



回答2:

For those who happen upon this post four years later, Scipy DOES provide a way to pass a np.random.RandomState object to its random variable classes, see rv_continuous and rv_discrete for more details. The scipy documentation says this:

seed : None or int or numpy.random.RandomState instance, optional

This parameter defines the RandomState object to use for drawing random variates. If None (or np.random), the global np.random state is used. If integer, it is used to seed the local RandomState instance. Default is None.

Unfortunately, it appears this argument is not available after the continuous/discrete rvs subclass rv_continuous or rv_discrete. However, the random_state property does belong to the sublass, meaning we can set the seed using an instance of np.random.RandomState after instantiation like so:

import numpy as np
import scipy.stats as stats

alpha_rv = stats.alpha(3.57)
alpha_rv.random_state = np.random.RandomState(seed=342423)