在numpy中置换每一列内容的最佳方法 [英] Best way to permute contents of each column in 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屋!