使用Numpy随机排列数组的列 [英] Shuffle columns of an array with Numpy

查看:171
本文介绍了使用Numpy随机排列数组的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个尺寸为(n, m)的数组r.我想改组该数组的列.

Let's say I have an array r of dimension (n, m). I would like to shuffle the columns of that array.

如果我使用numpy.random.shuffle(r),它将使行混乱.我怎样才能只对列进行洗牌?这样第一列就随机地成为第二列,而第三列则是第一列,依此类推.

If I use numpy.random.shuffle(r) it shuffles the lines. How can I only shuffle the columns? So that the first column become the second one and the third the first, etc, randomly.

示例:

输入:

array([[  1,  20, 100],
       [  2,  31, 401],
       [  8,  11, 108]])

输出:

array([[  20, 1, 100],
       [  31, 2, 401],
       [  11,  8, 108]])

推荐答案

在问我可能要改组转置数组时,

While asking I thought about maybe I could shuffle the transposed array:

 np.random.shuffle(np.transpose(r))

看起来确实可以完成.我希望通过评论了解这是否是实现此目标的好方法.

It looks like it does the job. I'd appreciate comments to know if it's a good way of achieving this.

这篇关于使用Numpy随机排列数组的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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