【问题标题】:Efficiently apply different permutations for each row of a 2D NumPy array [duplicate]为 2D NumPy 数组的每一行有效地应用不同的排列 [重复]
【发布时间】:2019-10-25 07:43:04
【问题描述】:

给定一个矩阵 A,我想对 A 的不同行应用不同的随机洗牌;例如,

array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

变成

array([[1, 3, 2],
       [6, 5, 4],
       [7, 9, 8]])

当然我们可以循环遍历矩阵,让每一行随机打乱;但是迭代很慢,我想问是否有更有效的方法来做到这一点。

【问题讨论】:

标签: python arrays numpy random


【解决方案1】:

从 Divakar 那里学到了这个巧妙的技巧,其中涉及 randnargsort

np.random.seed(0)

s = np.arange(16).reshape(4, 4)
np.take_along_axis(s, np.random.randn(*s.shape).argsort(axis=1), axis=1)

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

对于二维数组,可以简化为

s[np.arange(len(s))[:,None], np.random.randn(*s.shape).argsort(axis=1)]

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

您还可以将np.random.permutation 单独应用于每一行以返回一个新数组。

np.apply_along_axis(np.random.permutation, axis=1, arr=s)

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

性能-

s = np.arange(10000 * 100).reshape(10000, 100) 

%timeit s[np.arange(len(s))[:,None], np.random.randn(*s.shape).argsort(axis=1)] 
%timeit np.apply_along_axis(np.random.permutation, 1, s)   

84.6 ms ± 857 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
842 ms ± 8.06 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

我注意到这取决于数据的维度,请务必先进行测试。

【讨论】:

  • 谢谢!所以如果我有一个 3D 数组,如果我想置换最后一个维度,那么我可以做np.take_along_axis(s, np.random.randn(*s.shape).argsort(axis=2), axis=2),对吧?
  • @Tony 是的,我认为应该可以。
【解决方案2】:

在代码方面你可以使用 numpy 的 apply_along_axis 作为

np.apply_along_axis(np.random.shuffle, 1, matrix)

但它似乎并不比迭代至少对 3x3 矩阵更有效,对于我得到的那种方法

> %%timeit 
> np.apply_along_axis(np.random.shuffle, 1, test)
67 µs ± 1.8 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

当迭代给出时

> %%timeit
> for i in range(test.shape[0]):
>     np.random.shuffle(test[i])
20.3 µs ± 284 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

【讨论】:

  • apply_along_axis 本质上只是迭代“其他”轴。没有速度承诺。它使 3d 和更大的迭代更漂亮;对 2d 没有任何作用。
猜你喜欢
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-14
  • 2021-11-21
  • 1970-01-01
  • 2021-02-07
相关资源
最近更新 更多