将随机种子传递给numpy random.choice函数

时间:2019-11-16 14:41:40

标签: numpy random-seed

假设我有一个函数,它使用一个random_state参数来确保可复制性

def replicable_function(random_seed):
    choice = np.random.choice(X, 10)
    #do more stuff here with choice
    return f(choice)

这是我的两个要求:

  1. 传递相同的random_seed(可以是整数或np.random.RandomState对象)意味着replicable_function总是输出相同的东西
  2. 我不会更改全局numpy随机种子状态(尝试对用户友好,不要更改她不期望的内容)

理想情况下,我想将此random_state传递给np.random.choice函数,但似乎没有这样的论点(see the source code here!)

1 个答案:

答案 0 :(得分:0)

我在这里问了之后就想出了这个答案。 我不确定这是否是最好的解决方案,所以很高兴收到其他建议。

我最终使用了sklearn中的实用函数,该函数在需要时将整数输入转换为RandomState实例。

def check_random_state(seed):
    """Turn seed into a np.random.RandomState instance

    Parameters
    ----------
    seed : None | int | instance of RandomState
        If seed is None, return the RandomState singleton used by np.random.
        If seed is an int, return a new RandomState instance seeded with seed.
        If seed is already a RandomState instance, return it.
        Otherwise raise ValueError.
    """
    if seed is None or seed is np.random:
        return np.random.mtrand._rand
    if isinstance(seed, (numbers.Integral, np.integer)):
        return np.random.RandomState(seed)
    if isinstance(seed, np.random.RandomState):
        return seed
    raise ValueError('%r cannot be used to seed a numpy.random.RandomState'
                     ' instance' % seed)

因此,我可以写:

from sklearn.utils import check_random_state

def replicable_function(random_seed):
    random_seed = check_random_state(random_seed)
    choice = random_seed.choice(X, 10) #instead of np.random.choice(X, 10)
    #do more stuff here with choice
    return f(choice)