如何随机洗牌在一个numpy数组中 [英] How to randomly shuffle "tiles" in a numpy array

查看:157
本文介绍了如何随机洗牌在一个numpy数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个nxn的numpy数组,我想将其均匀地划分为nxn个图块,并在将图样保留在图块内部的同时随机将它们随机洗牌.

I have an nxn numpy array, and I would like to divide it evenly into nxn tiles and randomly shuffle these, while retaining the pattern inside the tiles.

例如,如果我有一个大小为(200,200)的数组,我希望能够将其划分为16个大小为(50,50)的数组,甚至64个大小为(25,25)的数组,并且在保留原始数组(200,200)的相同形状并保留较小数组内部的数字顺序的同时,随机地随机排列它们.

For example, if I have an array that's size (200,200), I want to be able to divide this into say 16 arrays of size (50,50), or even 64 arrays of size (25,25), and randomly shuffle these, while retaining the same shape of the original array (200,200) and retaining the order of numbers inside of the smaller arrays.

我已经查看了特定的numpy函数,并且找到了numpy.random.shuffle(x)函数,但这将随机地对数组的各个元素进行混洗.我只想在较大的数组中随机排列这些较小的数组.

I have looked up specific numpy functions, and I found the numpy.random.shuffle(x) function, but this will randomly shuffle the individual elements of an array. I would only like to shuffle these smaller arrays within the larger array.

是否有任何numpy函数或快捷方式可以做到这一点?我不确定从哪里开始.

Is there any numpy function or quick way that will do this? I'm not sure where to begin.

编辑:进一步明确我想要的内容:

EDIT: To further clarify exactly what I want:

假设我有一个形状为(10,10)的值的输入2D数组:

Let's say I have an input 2D array of shape (10,10) of values:

0   1   2   3   4   5   6   7   8   9
10  11  12  13  14  15  16  17  18  19
20  21  22  23  24  25  26  27  28  29
30  31  32  33  34  35  36  37  38  39
40  41  42  43  44  45  46  47  48  49
50  51  52  53  54  55  56  57  58  59
60  61  62  63  64  65  66  67  68  69
70  71  72  73  74  75  76  77  78  79
80  81  82  83  84  85  86  87  88  89
90  91  92  93  94  95  96  97  98  99

我选择的图块大小应使其均匀地适合此数组,因此,由于该数组的形状为(10,10),因此我可以选择将其拆分为4(5,5)个图块或25(2, 2)瓷砖.因此,如果我选择4(5,5)个图块,则我想随机地对这些图块进行混洗,以产生一个看起来像这样的输出数组:

I choose a tile size such that it fits evenly into this array, so since this array has shape (10,10), I can either choose to split this into 4 (5,5) tiles, or 25 (2,2) tiles. So if I choose 4 (5,5) tiles, I want to randomly shuffle these tiles that results in an output array that could look like this:

50  51  52  53  54  0   1   2   3   4
60  61  62  63  64  10  11  12  13  14
70  71  72  73  74  20  21  22  23  24
80  81  82  83  84  30  31  32  33  34
90  91  92  93  94  40  41  42  43  44
55  56  57  58  59  5   6   7   8   9
65  66  67  68  69  15  16  17  18  19
75  76  77  78  79  25  26  27  28  29
85  86  87  88  89  35  36  37  38  39
95  96  97  98  99  45  46  47  48  49

每个数组(输入数组,输出数组和单独的图块)都将是正方形,因此,在随机混洗时,主数组的大小和尺寸保持不变(10,10).

Every array (both the input array, the output array, and the separate tiles) would be squares, so that when randomly shuffled the size and dimension of the main array stays the same (10,10).

推荐答案

这是我使用循环的解决方案

here is my solution using loop

import numpy as np

arr = np.arange(36).reshape(6,6)

def suffle_section(arr, n_sections):

    assert arr.shape[0]==arr.shape[1], "arr must be square"
    assert arr.shape[0]%n_sections == 0, "arr size must divideable into equal n_sections"

    size = arr.shape[0]//n_sections


    new_arr = np.empty_like(arr)
    ## randomize section's row index

    rand_indxes = np.random.permutation(n_sections*n_sections)

    for i in range(n_sections):
        ## randomize section's column index
        for j in  range(n_sections):

            rand_i = rand_indxes[i*n_sections + j]//n_sections
            rand_j = rand_indxes[i*n_sections + j]%n_sections

            new_arr[i*size:(i+1)*size, j*size:(j+1)*size] = \
                arr[rand_i*size:(rand_i+1)*size, rand_j*size:(rand_j+1)*size]

    return new_arr


result = suffle_section(arr, 3)


display(arr)
display(result)

array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35]])

array([[ 4,  5, 16, 17, 24, 25],
       [10, 11, 22, 23, 30, 31],
       [14, 15,  2,  3,  0,  1],
       [20, 21,  8,  9,  6,  7],
       [26, 27, 12, 13, 28, 29],
       [32, 33, 18, 19, 34, 35]])

这篇关于如何随机洗牌在一个numpy数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆