在numpy数组中有效排列每列内容的最佳方法是什么?
我有这样的东西:
>>> 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]])
3条答案
按热度按时间c8ib6hqw1#
如果你的数组是多维的,
np.random.permutation
默认沿着第一个轴(列)排列:然而,这会打乱行索引,因此每列具有相同的(随机)顺序。
独立地混洗每一列的最简单的方法可以是在列上循环,并使用
np.random.shuffle
将每一列混洗到位:其给出例如:
如果你有一个非常大的数组,你不想复制它,因为每一列的排列都是在原地完成的,这个方法很有用。另一方面,即使是简单的Python循环也可能非常慢,有更快的NumPy方法,比如@jme提供的方法。
zf9nrax12#
这里有另一种方法:
快速测试:
其思想是生成一堆随机数,然后在每列中独立地
argsort
它们。这产生了每列索引的随机排列。请注意,这具有次优的渐近时间复杂度,因为对于大小为
m x n
的数组,排序需要时间O(n m log m)
。但是由于Python的for
循环非常慢,因此除了非常高的矩阵之外,您实际上可以获得更好的性能。xmq68pz93#
要沿着数组的行轴执行排列,可以使用以下代码:
或者:
但是,如果您有一个多维数组,则可以使用以下代码沿着特定轴执行置换:
例如,假设您要沿着第二个轴排列以下数组:
在轴1上排列
采样器定义如下:
然后,您可以使用take()方法应用置换:
如果你想把所有元素沿着所有的轴排列,你可以这样做