如何在使用Python的numpy随机选择时指定随机种子? [英] How to specify a random seed while using Python's numpy random choice?

查看:1139
本文介绍了如何在使用Python的numpy随机选择时指定随机种子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个字符串的列表.然后在Pandas数据框中,我想创建一个变量,从该列表中随机选择一个值,并将其分配给每一行.我正在使用numpy的随机选择,但是阅读其文档时,没有任何种子选项.如何为随机分配指定随机种子,以便每次随机分配都相同?

I have a list of four strings. Then in a Pandas dataframe I want to create a variable randomly selecting a value from this list and assign into each row. I am using numpy's random choice, but reading their documentation, there is no seed option. How can I specify the random seed to the random assignment so every time the random assignment will be the same?

service_code_options = ['899.59O', '12.42R', '13.59P', '204.68L']
df['SERVICE_CODE'] = [np.random.choice(service_code_options ) for i in df.index]

推荐答案

您需要先通过 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆