scipy.stats seed?

后端 未结 4 1014
滥情空心
滥情空心 2021-02-06 22:51

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 ther

4条回答
  •  猫巷女王i
    2021-02-06 23:41

    Adding to the answer of user5915738, which I think is the best answer in general, I'd like to point out the imho most convenient way to seed the random generator of a scipy.stats distribution.

    You can set the seed while generating the distribution with the rvs method, either by defining the seed as an integer, which is used to seed np.random.RandomState internally:

    uni_int_seed = scipy.stats.uniform(-.1, 1.).rvs(10, random_state=12)
    

    or by directly defining the np.random.RandomState:

    uni_state_seed = scipy.stats.uniform(-.1, 1.).rvs(
        10, random_state=np.random.RandomState(seed=12))
    

    Both methods are equivalent:

    np.all(uni_int_seed == uni_state_seed)
    # Out: True
    

    The advantage of this method over assigning it to the random_state of rv_continuous or rv_discrete is, that you always have explicit control over the random state of your rvs, whereas with my_dist.random_state = np.random.RandomState(seed=342423) the seed is lost after each call to rvs, possibly resulting in non-reproducible results when losing track of distributions.
    Also according to the The Zen of Python:

    1. Explicit is better than implicit.

    :)

提交回复
热议问题