用Numpy洗牌阵列

时间:2021-01-10 21:22:03

Let's say I have an array r of dimension (n, m). I would like to shuffle the columns of that array.

假设我有一个维数(n,m)的数组r。我想改组那个数组的列。

If I use numpy.random.shuffle(r) it shuffles the lines. How can I only shuffle the columns? So that the first column become the second one and the third the first, etc, randomly.

如果我使用numpy.random.shuffle(r)它会改变行。我怎么才能洗牌?因此第一列成为第二列,第三列成为第一列,等等。

Example:

例:

input:

输入:

array([[  1,  20, 100],
       [  2,  31, 401],
       [  8,  11, 108]])

output:

输出:

array([[  20, 1, 100],
       [  31, 2, 401],
       [  11,  8, 108]])

5 个解决方案

#1


15  

While asking I thought about maybe I could shuffle the transposed array:

在问我想的时候我可能会改变转置的数组:

 np.random.shuffle(np.transpose(r))

It looks like it does the job. I'd appreciate comments to know if it's a good way of achieving this.

看起来它完成了这项工作。我很感激评论,知道这是否是实现这一目标的好方法。

#2


6  

For a general axis you could follow the pattern:

对于一般轴,您可以遵循以下模式:

>>> import numpy as np
>>> 
>>> a = np.array([[  1,  20, 100, 4],
...               [  2,  31, 401, 5],
...               [  8,  11, 108, 6]])
>>> 
>>> print a[:, np.random.permutation(a.shape[1])]
[[  4   1  20 100]
 [  5   2  31 401]
 [  6   8  11 108]]
>>> 
>>> print a[np.random.permutation(a.shape[0]), :]
[[  1  20 100   4]
 [  2  31 401   5]
 [  8  11 108   6]]
>>> 

#3


2  

So, one step further from your answer:

那么,离你的答案更进一步:

Edit: I very easily could be mistaken how this is working, so I'm inserting my understanding of the state of the matrix at each step.

编辑:我很容易被误解为这是如何工作的,所以我在每一步都插入了对矩阵状态的理解。

r == 1 2 3
     4 5 6
     6 7 8

r = np.transpose(r)  

r == 1 4 6
     2 5 7
     3 6 8           # Columns are now rows

np.random.shuffle(r)

r == 2 5 7
     3 6 8 
     1 4 6           # Columns-as-rows are shuffled

r = np.transpose(r)  

r == 2 3 1
     5 6 4
     7 8 6           # Columns are columns again, shuffled.

which would then be back in the proper shape, with the columns rearranged.

然后它将以适当的形状返回,并重新排列列。

The transpose of the transpose of a matrix == that matrix, or, [A^T]^T == A. So, you'd need to do a second transpose after the shuffle (because a transpose is not a shuffle) in order for it to be in its proper shape again.

矩阵的转置的转置==那个矩阵,或者,[A ^ T] ^ T == A.所以,你需要在shuffle之后进行第二次转置(因为转置不是一个shuffle)为了使它再次处于适当的形状。

Edit: The OP's answer skips storing the transpositions and instead lets the shuffle operate on r as if it were.

编辑:OP的答案跳过存储转置,而是让shuffle在r上运行,就好像它一样。

#4


0  

In general if you want to shuffle a numpy array along axis i:

一般来说,如果你想沿着轴i洗牌一个numpy数组:

def shuffle(x, axis = 0):
    n_axis = len(x.shape)
    t = np.arange(n_axis)
    t[0] = axis
    t[axis] = 0
    xt = np.transpose(x.copy(), t)
    np.random.shuffle(xt)
    shuffled_x = np.transpose(xt, t)
    return shuffled_x

shuffle(array, axis=i)

#5


0  

>>> print(s0)
>>> [[0. 1. 0. 1.]
     [0. 1. 0. 0.]
     [0. 1. 0. 1.]
     [0. 0. 0. 1.]]
>>> print(np.random.permutation(s0.T).T)
>>> [[1. 0. 1. 0.]
     [0. 0. 1. 0.]
     [1. 0. 1. 0.]
     [1. 0. 0. 0.]]

np.random.permutation(), does the row permutation.

np.random.permutation(),行排列。

#1


15  

While asking I thought about maybe I could shuffle the transposed array:

在问我想的时候我可能会改变转置的数组:

 np.random.shuffle(np.transpose(r))

It looks like it does the job. I'd appreciate comments to know if it's a good way of achieving this.

看起来它完成了这项工作。我很感激评论,知道这是否是实现这一目标的好方法。

#2


6  

For a general axis you could follow the pattern:

对于一般轴,您可以遵循以下模式:

>>> import numpy as np
>>> 
>>> a = np.array([[  1,  20, 100, 4],
...               [  2,  31, 401, 5],
...               [  8,  11, 108, 6]])
>>> 
>>> print a[:, np.random.permutation(a.shape[1])]
[[  4   1  20 100]
 [  5   2  31 401]
 [  6   8  11 108]]
>>> 
>>> print a[np.random.permutation(a.shape[0]), :]
[[  1  20 100   4]
 [  2  31 401   5]
 [  8  11 108   6]]
>>> 

#3


2  

So, one step further from your answer:

那么,离你的答案更进一步:

Edit: I very easily could be mistaken how this is working, so I'm inserting my understanding of the state of the matrix at each step.

编辑:我很容易被误解为这是如何工作的,所以我在每一步都插入了对矩阵状态的理解。

r == 1 2 3
     4 5 6
     6 7 8

r = np.transpose(r)  

r == 1 4 6
     2 5 7
     3 6 8           # Columns are now rows

np.random.shuffle(r)

r == 2 5 7
     3 6 8 
     1 4 6           # Columns-as-rows are shuffled

r = np.transpose(r)  

r == 2 3 1
     5 6 4
     7 8 6           # Columns are columns again, shuffled.

which would then be back in the proper shape, with the columns rearranged.

然后它将以适当的形状返回,并重新排列列。

The transpose of the transpose of a matrix == that matrix, or, [A^T]^T == A. So, you'd need to do a second transpose after the shuffle (because a transpose is not a shuffle) in order for it to be in its proper shape again.

矩阵的转置的转置==那个矩阵,或者,[A ^ T] ^ T == A.所以,你需要在shuffle之后进行第二次转置(因为转置不是一个shuffle)为了使它再次处于适当的形状。

Edit: The OP's answer skips storing the transpositions and instead lets the shuffle operate on r as if it were.

编辑:OP的答案跳过存储转置,而是让shuffle在r上运行,就好像它一样。

#4


0  

In general if you want to shuffle a numpy array along axis i:

一般来说,如果你想沿着轴i洗牌一个numpy数组:

def shuffle(x, axis = 0):
    n_axis = len(x.shape)
    t = np.arange(n_axis)
    t[0] = axis
    t[axis] = 0
    xt = np.transpose(x.copy(), t)
    np.random.shuffle(xt)
    shuffled_x = np.transpose(xt, t)
    return shuffled_x

shuffle(array, axis=i)

#5


0  

>>> print(s0)
>>> [[0. 1. 0. 1.]
     [0. 1. 0. 0.]
     [0. 1. 0. 1.]
     [0. 0. 0. 1.]]
>>> print(np.random.permutation(s0.T).T)
>>> [[1. 0. 1. 0.]
     [0. 0. 1. 0.]
     [1. 0. 1. 0.]
     [1. 0. 0. 0.]]

np.random.permutation(), does the row permutation.

np.random.permutation(),行排列。