假设我有一个函数,它使用一个random_state
参数来确保可复制性
def replicable_function(random_seed):
choice = np.random.choice(X, 10)
#do more stuff here with choice
return f(choice)
这是我的两个要求:
random_seed
(可以是整数或np.random.RandomState
对象)意味着replicable_function
总是输出相同的东西理想情况下,我想将此random_state
传递给np.random.choice
函数,但似乎没有这样的论点(see the source code here!)
答案 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)