洗牌2D数组的行组-NumPy

洗牌2D数组的行组-NumPy,numpy,multidimensional-array,shuffle,Numpy,Multidimensional Array,Shuffle,假设我有一个(50,5)数组。我有没有办法根据数据点的行/序列分组来洗牌,即不是洗牌每一行,而是洗牌5行的块 感谢方法#1:这是一种基于组大小重塑为3D数组的方法,使用从np.random.permutation获得的无序索引索引索引块索引,最后重塑为2D- N = 5 # Blocks of N rows M,n = a.shape[0]//N, a.shape[1] out = a.reshape(M,-1,n)[np.random.permutation(M)].reshape(-1,n)

假设我有一个(50,5)数组。我有没有办法根据数据点的行/序列分组来洗牌,即不是洗牌每一行,而是洗牌5行的块

感谢

方法#1:这是一种基于组大小重塑为
3D
数组的方法,使用从
np.random.permutation
获得的无序索引索引索引块索引,最后重塑为
2D
-

N = 5 # Blocks of N rows
M,n = a.shape[0]//N, a.shape[1]
out = a.reshape(M,-1,n)[np.random.permutation(M)].reshape(-1,n)
样本运行-

In [141]: a
Out[141]: 
array([[89, 26, 12],
       [97, 60, 96],
       [94, 38, 54],
       [41, 63, 29],
       [88, 62, 48],
       [95, 66, 32],
       [28, 58, 80],
       [26, 35, 89],
       [72, 91, 38],
       [26, 70, 93]])

In [142]: N = 2 # Blocks of N rows

In [143]: M,n = a.shape[0]//N, a.shape[1]

In [144]: a.reshape(M,-1,n)[np.random.permutation(M)].reshape(-1,n)
Out[144]: 
array([[94, 38, 54],
       [41, 63, 29],
       [28, 58, 80],
       [26, 35, 89],
       [89, 26, 12],
       [97, 60, 96],
       [72, 91, 38],
       [26, 70, 93],
       [88, 62, 48],
       [95, 66, 32]])
In [156]: a
Out[156]: 
array([[15, 12, 14],
       [55, 39, 35],
       [73, 78, 36],
       [54, 52, 32],
       [83, 34, 91],
       [42, 11, 98],
       [27, 65, 47],
       [78, 75, 82],
       [33, 52, 93],
       [87, 51, 80]])

In [157]: N = 2 # Blocks of N rows

In [158]: M,n = a.shape[0]//N, a.shape[1]

In [159]: np.random.shuffle(a.reshape(M,-1,n))

In [160]: a
Out[160]: 
array([[15, 12, 14],
       [55, 39, 35],
       [27, 65, 47],
       [78, 75, 82],
       [73, 78, 36],
       [54, 52, 32],
       [33, 52, 93],
       [87, 51, 80],
       [83, 34, 91],
       [42, 11, 98]])

方法#2:也可以简单地用于现场变更-

np.random.shuffle(a.reshape(M,-1,n))
样本运行-

In [141]: a
Out[141]: 
array([[89, 26, 12],
       [97, 60, 96],
       [94, 38, 54],
       [41, 63, 29],
       [88, 62, 48],
       [95, 66, 32],
       [28, 58, 80],
       [26, 35, 89],
       [72, 91, 38],
       [26, 70, 93]])

In [142]: N = 2 # Blocks of N rows

In [143]: M,n = a.shape[0]//N, a.shape[1]

In [144]: a.reshape(M,-1,n)[np.random.permutation(M)].reshape(-1,n)
Out[144]: 
array([[94, 38, 54],
       [41, 63, 29],
       [28, 58, 80],
       [26, 35, 89],
       [89, 26, 12],
       [97, 60, 96],
       [72, 91, 38],
       [26, 70, 93],
       [88, 62, 48],
       [95, 66, 32]])
In [156]: a
Out[156]: 
array([[15, 12, 14],
       [55, 39, 35],
       [73, 78, 36],
       [54, 52, 32],
       [83, 34, 91],
       [42, 11, 98],
       [27, 65, 47],
       [78, 75, 82],
       [33, 52, 93],
       [87, 51, 80]])

In [157]: N = 2 # Blocks of N rows

In [158]: M,n = a.shape[0]//N, a.shape[1]

In [159]: np.random.shuffle(a.reshape(M,-1,n))

In [160]: a
Out[160]: 
array([[15, 12, 14],
       [55, 39, 35],
       [27, 65, 47],
       [78, 75, 82],
       [73, 78, 36],
       [54, 52, 32],
       [33, 52, 93],
       [87, 51, 80],
       [83, 34, 91],
       [42, 11, 98]])