pandas 用于随机生成器的 Python 熊猫种子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44068913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:38:21  来源:igfitidea点击:

Python pandas seed for random generator

pythonpandasrandomseed

提问by Mr T.

I have a small question re: np.random.seed(seed=x)
I have a df SEED = 1column of seed numbers
I would like to generate random number after choosing a seed from my df SEED
For example in the first simulation I'll use seed no 100 of the df SEEDand in the next simulation, I want to use seed no 200 of the SEEDdf.
I've been trying out things , but to no avail.
Any hints for me?
Cheers

我有一个小问题:np.random.seed(seed=x)
我有一个 dfSEED = 1种子数列
我想在从我的 df 中选择一个种子后生成随机数SEED
例如,在第一次模拟中,我将使用 df 的 100 号种子SEED,在下一次模拟中,我想用SEEDdf的200号种子。
我一直在尝试一些东西,但无济于事。
对我有什么提示吗?
干杯

回答by jezrael

It seems you need loop by values of column SEEDand set np.random.seed(x):

看来您需要按列的值循环SEED并设置np.random.seed(x)

df = pd.DataFrame({'SEED':[100,200,500]})
print (df)
   SEED
0   100
1   200
2   500

for i, x in df['SEED'].items():
    print (x)
    np.random.seed(x)
    #some random function
    a = np.random.randint(10, size=5)
    print (a)

100
[8 8 3 7 7]
200
[9 0 4 7 9]
500
[7 1 1 8 7]


If need generate random value from list:

如果需要从列表中生成随机值:

L = [100,200,500]
a = np.random.choice(L, size=1)[0]
np.random.seed(a)
print (a)
500