如何通过保留除一个属性之外的所有属性来随机地随机化一个种群? [英] How to randomly shuffle a populaiton by preserving all properites except one?

查看:71
本文介绍了如何通过保留除一个属性之外的所有属性来随机地随机化一个种群?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个球形的空间区域充满了特定分布的较小的,不同大小的球体.每个球体都与一些物理属性相关联:位置,半径,质量,速度和ID均表示为1d或3d numpy数组.我想以完全 random 的方式对这群球进行混洗,以使任何单个球都保留其3d位置阵列以外的所有属性.我在这里遇到过类似的问题(除了第一列以外,随机地对列进行了随机排序)但是,有没有一种简便而快速的pythonic方式而不使用DataFrame来做到这一点?

A spherical region of space is filled with a specific distribution of smaller, different size spheres. Each sphere is associated with some physical properties: position, radius, mass, velocity, and ID all represented as 1d or 3d numpy arrays. I would like to shuffle this population of spheres in a totally random manner such that any single sphere preserves all of its properties except its 3d position array. I have encountered this similar question in here (Randomly shuffle columns except first column) but, is there an easy and fast pythonic way to do this without using DataFrame?

感谢您的帮助.

推荐答案

如果您使用的是 pandas ,则可以随机排列一列:

If you're using pandas, you could just shuffle one column:

df['col'] = df['col'].sample(frac=1).values

这在任何列子集上同样有效,例如

This works equally well on any subset of columns, e.g.

cols = ['col1', 'col2']
df[cols] = df[cols].sample(frac=1).values

这两列混在一起,即它们各自的值保持对齐.

The two columns are shuffled together, i.e. their respective values remain aligned.

另请参见此答案.

这篇关于如何通过保留除一个属性之外的所有属性来随机地随机化一个种群?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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