scipy.stats seed?

后端 未结 4 1016
滥情空心
滥情空心 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条回答
  •  轮回少年
    2021-02-06 23:37

    For those who are stumbling upon this question 7 years later, there has been a major change in numpy Random State generator function. As per the documentation here and here, the RandomState class is replaced with the Generator class. RandomState is guaranteed to be compatible with older versions/codes however it will not receive any substantial changes, including algorithmic improvements, which are reserved for Generator.

    For clarifications on how to pass an existing Numpy based random stream to Scipy functions in the same experiment, given below are some examples and reasonings for which cases are desirable and why.

    from numpy.random import Generator, PCG64
    from scipy.stats import binom
    
    n, p, size, seed = 10, 0.5, 10, 12345
    
    # Case 1 : Scipy uses some default Random Generator
    numpy_randomGen = Generator(PCG64(seed))
    scipy_randomGen = binom
    print(scipy_randomGen.rvs(n, p, size))
    print(numpy_randomGen.binomial(n, p, size))
    # prints
    # [6 6 5 4 6 6 8 6 6 4]
    # [4 4 6 6 5 4 5 4 6 7]
    # NOT DESIRABLE as we don't have control over the seed of Scipy random number generation
    
    
    # Case 2 : Scipy uses same seed and Random generator (new object though)
    scipy_randomGen.random_state=Generator(PCG64(seed))
    numpy_randomGen = Generator(PCG64(seed))
    print(scipy_randomGen.rvs(n, p, size))
    print(numpy_randomGen.binomial(n, p, size))
    # prints
    # [4 4 6 6 5 4 5 4 6 7]
    # [4 4 6 6 5 4 5 4 6 7]
        # This experiment is using same sequence of random numbers, one is being used by Scipy
    # and other by Numpy. NOT DESIRABLE as we don't want repetition of some random 
    # stream in same experiment.
    
    
    # Case 3 (IMP) : Scipy uses an existing Random Generator which can being passed to Scipy based 
    # random generator object
    numpy_randomGen = Generator(PCG64(seed))
    scipy_randomGen.random_state=numpy_randomGen
    print(scipy_randomGen.rvs(n, p, size))
    print(numpy_randomGen.binomial(n, p, size))
    # prints
    # [4 4 6 6 5 4 5 4 6 7]
    # [4 8 6 3 5 7 6 4 6 4]
    # This should be the case which we mostly want (DESIRABLE). If we are using both Numpy based and 
    #Scipy based random number generators/function, then not only do we have no repetition of 
    #random number sequences but also have reproducibility of results in this case.
    

提交回复
热议问题