[英]How do I randomize a pandas column in the same order every time I run the code in Python?
This is my code: 这是我的代码:
random_idx = np.random.permutation(len(cIds))
train_Ids = cIds[random_idx[:train_size]]
Now, I want the list to be randomized in the same order every time I run this line of code. 现在,我希望每次运行这行代码时,列表都以相同的顺序随机化。
Note: I do not want to save random_idx
variable in a text file, and fetch the same list. 注意:我不想在文本文件中保存
random_idx
变量,并获取相同的列表。
You can use seed
in order to tell numpy to generate the same random numbers: 您可以使用
seed
来告诉numpy生成相同的随机数:
np.random.seed(seed=1234)
random_idx = np.random.permutation(len(cIds))
same as: 如同:
np.random.seed(1234)
random_idx = np.random.permutation(len(cIds))
Or 要么
random_idx = np.random.RandomState(seed=1234).permutation(len(cIds)
seed: Must be convertible to 32 bit unsigned integers`
种子:必须可以转换为32位无符号整数
Or you can do in in pandas style: 或者你可以用熊猫风格做:
cIds.sample(n=train_size,
replace=False,
random_state=your_favorite_number_here)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.