简体   繁体   English

Numpy 二维数组在行之间而不是列之间随机排列元素

[英]Numpy 2D array shuffle elements between rows and not columns

I have following two dimensional array:我有以下二维数组:

seq_length = 5
x = np.array([[0, 2, 0, 4], [5,6,7,8]])
x_repeated = np.repeat(x, seq_length, axis=1)


[[0 0 0 0 0 2 2 2 2 2 0 0 0 0 0 4 4 4 4 4]
 [5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8]]

I want to shuffle x_repeated according to seq_length that all items of seq will be shuffled together.我想根据x_repeatedseq_length进行洗牌,以便将 seq 的所有项目一起洗牌。

For example, possible shuffle:例如,可能的洗牌:

[[0 0 0 0 0 6 6 6 6 6 0 0 0 0 0 8 8 8 8 8]
 [5 5 5 5 5 2 2 2 2 2 7 7 7 7 7 4 4 4 4 4]]

Thanks谢谢

I have following two dimensional array:我有以下二维数组:

seq_length = 5
x = np.array([[0, 2, 0, 4], [5,6,7,8]])
x_repeated = np.repeat(x, seq_length, axis=1)


[[0 0 0 0 0 2 2 2 2 2 0 0 0 0 0 4 4 4 4 4]
 [5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8]]

I want to shuffle x_repeated according to seq_length that all items of seq will be shuffled together.我想根据x_repeatedseq_length进行洗牌,即 seq 的所有项目都将被洗牌。

For example, possible shuffle:例如,可能的洗牌:

[[0 0 0 0 0 6 6 6 6 6 0 0 0 0 0 8 8 8 8 8]
 [5 5 5 5 5 2 2 2 2 2 7 7 7 7 7 4 4 4 4 4]]

Thanks谢谢

I have following two dimensional array:我有以下二维数组:

seq_length = 5
x = np.array([[0, 2, 0, 4], [5,6,7,8]])
x_repeated = np.repeat(x, seq_length, axis=1)


[[0 0 0 0 0 2 2 2 2 2 0 0 0 0 0 4 4 4 4 4]
 [5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8]]

I want to shuffle x_repeated according to seq_length that all items of seq will be shuffled together.我想根据x_repeatedseq_length进行洗牌,即 seq 的所有项目都将被洗牌。

For example, possible shuffle:例如,可能的洗牌:

[[0 0 0 0 0 6 6 6 6 6 0 0 0 0 0 8 8 8 8 8]
 [5 5 5 5 5 2 2 2 2 2 7 7 7 7 7 4 4 4 4 4]]

Thanks谢谢

I have following two dimensional array:我有以下二维数组:

seq_length = 5
x = np.array([[0, 2, 0, 4], [5,6,7,8]])
x_repeated = np.repeat(x, seq_length, axis=1)


[[0 0 0 0 0 2 2 2 2 2 0 0 0 0 0 4 4 4 4 4]
 [5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8]]

I want to shuffle x_repeated according to seq_length that all items of seq will be shuffled together.我想根据x_repeatedseq_length进行洗牌,即 seq 的所有项目都将被洗牌。

For example, possible shuffle:例如,可能的洗牌:

[[0 0 0 0 0 6 6 6 6 6 0 0 0 0 0 8 8 8 8 8]
 [5 5 5 5 5 2 2 2 2 2 7 7 7 7 7 4 4 4 4 4]]

Thanks谢谢

I have following two dimensional array:我有以下二维数组:

seq_length = 5
x = np.array([[0, 2, 0, 4], [5,6,7,8]])
x_repeated = np.repeat(x, seq_length, axis=1)


[[0 0 0 0 0 2 2 2 2 2 0 0 0 0 0 4 4 4 4 4]
 [5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8]]

I want to shuffle x_repeated according to seq_length that all items of seq will be shuffled together.我想根据x_repeatedseq_length进行洗牌,即 seq 的所有项目都将被洗牌。

For example, possible shuffle:例如,可能的洗牌:

[[0 0 0 0 0 6 6 6 6 6 0 0 0 0 0 8 8 8 8 8]
 [5 5 5 5 5 2 2 2 2 2 7 7 7 7 7 4 4 4 4 4]]

Thanks谢谢

Here is a solution that does is completely in-place and does not require allocating and generating random indices:这是一个完全就地的解决方案,不需要分配和生成随机索引:

import numpy as np


def row_block_shuffle(a: np.ndarray, seq_len: int):
    cols = a.shape[1]
    rng = np.random.default_rng()
    for block in x_repeated.T.reshape(cols // seq_len, seq_length, -1).transpose(0, 2, 1):
        rng.shuffle(block)


if __name__ == "__main__":
    seq_length = 5
    x = np.array([[0, 2, 0, 4], [5, 6, 7, 8]])
    x_repeated = np.repeat(x, seq_length, axis=1)

    row_block_shuffle(x_repeated, seq_length)
    print(x_repeated)

Output:输出:

[[5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8]
 [0 0 0 0 0 2 2 2 2 2 0 0 0 0 0 4 4 4 4 4]]

What I do is to create "blocks" that shares memory with the original array:我所做的是创建与原始数组共享内存的“块”:

>>> x_repeated.T.reshape(cols // seq_len, seq_length, -1).transpose(0, 2, 1)
[[[0 0 0 0 0]
  [5 5 5 5 5]]

 [[2 2 2 2 2]
  [6 6 6 6 6]]

 [[0 0 0 0 0]
  [7 7 7 7 7]]

 [[4 4 4 4 4]
  [8 8 8 8 8]]]

Then I shuffle each "block", which will in turn shuffles the original array as well.然后我打乱每个“块”,这又会打乱原始数组。 I believe this is the most effective solution for large arrays as this solution is as in-place as it can be.我相信这是对大型阵列最有效的解决方案,因为该解决方案已尽可能就地使用。 This answer at least backs up my hypothesis:这个答案至少支持了我的假设:

https://stackoverflow.com/a/5044364/13091658 https://stackoverflow.com/a/5044364/13091658

Also, The general problem you are facing is sorting "sliding window views" of your array: so if you would like to sort "windows" within your array that both moves horizontally and vertically you can for example see my previous answers for problems related to sliding windows here:此外,您面临的一般问题是对数组的“滑动窗口视图”进行排序:因此,如果您想对数组中水平和垂直移动的“窗口”进行排序,例如,您可以查看我之前对相关问题的回答滑动窗口在这里:

https://stackoverflow.com/a/67416335/13091658 https://stackoverflow.com/a/67416335/13091658

https://stackoverflow.com/a/69924828/13091658 https://stackoverflow.com/a/69924828/13091658

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM