在numpy中置换每一列内容的最佳方法 [英] Best way to permute contents of each column in numpy

查看:149
本文介绍了在numpy中置换每一列内容的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有效地置换numpy数组中每一列的内容的最佳方法是什么?

What's the best way to efficiently permute the contents of each column in a numpy array?

我所拥有的是这样的:

>>> arr = np.arange(16).reshape((4, 4))
>>> arr
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

>> # Shuffle each column independently to obtain something like
array([[  8,  5, 10,  7],
       [ 12,  1,  6,  3],
       [  4,  9, 14, 11],
       [  0, 13,  2, 15]])

推荐答案

如果您的数组是多维数组,请

If your array is multi-dimensional, np.random.permutation permutes along the first axis (columns) by default:

>>> np.random.permutation(arr)
array([[ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [ 0,  1,  2,  3],
       [12, 13, 14, 15]])

但是,这会乱排行索引,因此每一列都具有相同的(随机)顺序.

However, this shuffles the row indices and so each column has the same (random) ordering.

独立重排各列的最简单方法是遍历各列并使用

The simplest way of shuffling each column independently could be to loop over the columns and use np.random.shuffle to shuffle each one in place:

for i in range(arr.shape[1]):
    np.random.shuffle(arr[:,i])

例如,哪个给出

array([[12,  1, 14, 11],
       [ 4,  9, 10,  7],
       [ 8,  5,  6, 15],
       [ 0, 13,  2,  3]])

如果您有一个非常大的数组而您不想复制,因为每个列的排列都就位了,则此方法很有用.另一方面,即使是简单的Python循环也可能非常慢,并且还有更快的NumPy方法,例如@jme提供的方法.

This method can be useful if you have a very large array which you don't want to copy because the permutation of each column is done in place. On the other hand, even simple Python loops can be very slow and there are quicker NumPy methods such as the one provided by @jme.

这篇关于在numpy中置换每一列内容的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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