在numpy数组的每一行中随机调整项目

Jac*_*tad 12 python arrays numpy

我有一个像下面这样的numpy数组:

Xtrain = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [1, 7, 3]])
Run Code Online (Sandbox Code Playgroud)

我想分别对每行的项进行随机播放,但不希望每行的shuffle相同(如在几个示例中只是随机播放列顺序).

例如,我想要一个如下输出:

output = np.array([[3, 2, 1],
                   [4, 6, 5],
                   [7, 3, 1]])
Run Code Online (Sandbox Code Playgroud)

如何以有效的方式随机随机地随机移动每一行?我的实际np数组超过100000行和1000列.

Kas*_*mvd 6

由于您只想对列进行随机播放,因此只需对矩阵的转置执行重排即可:

In [86]: np.random.shuffle(Xtrain.T)

In [87]: Xtrain
Out[87]: 
array([[2, 3, 1],
       [5, 6, 4],
       [7, 3, 1]])
Run Code Online (Sandbox Code Playgroud)

请注意,2D数组上的random.suffle()会对行进行洗牌,而不是每行中的项目.即改变行的位置.因此,如果你改变转置矩阵行的位置,你实际上是在改组原始数组的列.

如果您仍然想要一个完全独立的shuffle,您可以为每一行创建随机索引,然后使用简单的索引创建最终数组:

In [172]: def crazyshuffle(arr):
     ...:     x, y = arr.shape
     ...:     rows = np.indices((x,y))[0]
     ...:     cols = [np.random.permutation(y) for _ in range(x)]
     ...:     return arr[rows, cols]
     ...: 
Run Code Online (Sandbox Code Playgroud)

演示:

In [173]: crazyshuffle(Xtrain)
Out[173]: 
array([[1, 3, 2],
       [6, 5, 4],
       [7, 3, 1]])

In [174]: crazyshuffle(Xtrain)
Out[174]: 
array([[2, 3, 1],
       [4, 6, 5],
       [1, 3, 7]])
Run Code Online (Sandbox Code Playgroud)


Jac*_*tad 3

来自: https: //github.com/numpy/numpy/issues/5173

def disarrange(a, axis=-1):
    """
    Shuffle `a` in-place along the given axis.

    Apply numpy.random.shuffle to the given axis of `a`.
    Each one-dimensional slice is shuffled independently.
    """
    b = a.swapaxes(axis, -1)
    # Shuffle `b` in-place along the last axis.  `b` is a view of `a`,
    # so `a` is shuffled in place, too.
    shp = b.shape[:-1]
    for ndx in np.ndindex(shp):
        np.random.shuffle(b[ndx])
    return
Run Code Online (Sandbox Code Playgroud)