繁体   English   中英

每次在Python中运行代码时,如何以相同的顺序随机化pandas列?

[英]How do I randomize a pandas column in the same order every time I run the code in Python?

这是我的代码:

random_idx = np.random.permutation(len(cIds))
train_Ids = cIds[random_idx[:train_size]]

现在,我希望每次运行这行代码时,列表都以相同的顺序随机化。

注意:我不想在文本文件中保存random_idx变量,并获取相同的列表。

您可以使用seed来告诉numpy生成相同的随机数:

np.random.seed(seed=1234)
random_idx = np.random.permutation(len(cIds))

如同:

np.random.seed(1234)
random_idx = np.random.permutation(len(cIds))

要么

random_idx = np.random.RandomState(seed=1234).permutation(len(cIds)

种子:必须可以转换为32位无符号整数

或者你可以用熊猫风格做:

cIds.sample(n=train_size, 
            replace=False, 
            random_state=your_favorite_number_here)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM