简体   繁体   English

将 2d numpy 数组分成 nxn 个块

[英]separating 2d numpy array into nxn chunks

How would you separate a 2D numpy array into a nxn chunks?如何将 2D numpy数组分成 nxn 个块?

For example, the following array of shape (4,4) :例如,以下形状数组(4,4)

arr = [[1,2,3,4],
       [5,6,7,8],
       [9,10,11,12],
       [13,14,15,16]]

Transformed to this array, of shape (4,2,2) , by subsampling with a different (2x2) array:通过使用不同的(2x2)数组进行子采样,转换为形状为(4,2,2)的此数组:

new_arr = [[[1,2],
            [5,6]],
           [[3,4],
            [7,8]],
           [[9,10],
            [13,14]],
           [[11,12],
            [15,16]]]

You could do the following, and adjust it to your array:您可以执行以下操作,并将其调整为您的阵列:

import numpy as np

arr = [[1,2,3,4],
       [5,6,7,8],
       [9,10,11,12],
       [13,14,15,16]]

arr_new = np.array([[arr[i][j:j+2], arr[i+1][j:j+2]] for j in range(len(arr[0])-2) for i in range(len(arr)-2)])
print(arr_new)
print(arr_new.shape)

This gives the following output:这给出了以下 output:

[[[ 1  2]
  [ 5  6]]

 [[ 5  6]
  [ 9 10]]

 [[ 2  3]
  [ 6  7]]

 [[ 6  7]
  [10 11]]]
(4, 2, 2)

You could use hsplit() and vsplit() methods to achieve the above.您可以使用hsplit()vsplit()方法来实现上述目的。

import numpy as np

arr = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])

ls1,ls2 = np.hsplit(arr, 2)
ls1 = np.vsplit(ls1,2)
ls2 = np.vsplit(ls2,2)
ls = ls1 + ls2
result = np.array(ls)

print(result)

>>> 
 [[[ 1  2]
  [ 5  6]]

 [[ 9 10]
  [13 14]]

 [[ 3  4]
  [ 7  8]]

 [[11 12]
  [15 16]]]

print(result.tolist())

>>> [[[1, 2], [5, 6]], [[9, 10], [13, 14]], [[3, 4], [7, 8]], [[11, 12], [15, 16]]]

You can use np.vsplit to split the array into multiple subarrays vertically.您可以使用np.vsplit将数组垂直拆分为多个子数组。 Similarly you can use np.hsplit to split the array into multiple subarrays horizontally.同样,您可以使用np.hsplit将数组水平拆分为多个子数组。 To better understand this examine the generalized resample function which makes the use of np.vsplit and np.hsplit methods.为了更好地理解这一点,请检查使用np.vsplitnp.hsplit方法的广义resample function。

Use this:用这个:

def ressample(arr, N):
    A = []
    for v in np.vsplit(arr, arr.shape[0] // N):
        A.extend([*np.hsplit(v, arr.shape[0] // N)])
    return np.array(A)

Example 1: The given 2D array is of shape 4x4 and we want to subsample it into the chunks of shape 2x2.示例 1:给定的 2D 数组的形状为 4x4,我们希望将其子采样到形状为 2x2 的块中。

arr = np.array([[1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 16]])  
print(ressample(arr, 2)) #--> chunk size 2

Output 1: Output 1:

[[[ 1  2]
  [ 5  6]]

 [[ 3  4]
  [ 7  8]]

 [[ 9 10]
  [13 14]]

 [[11 12]
  [15 16]]]

Example 2: Consider the given 2D array contains 8 rows and 8 columns.示例 2:考虑给定的二维数组包含 8 行和 8 列。 Now we subsample this array into the chunks of shape 4x4.现在我们将这个数组二次采样成 4x4 形状的块。

arr = np.random.randint(0, 10, 64).reshape(8, 8)   
print(ressample(arr, 4)) #--> chunck size 4

Sample Output 2:样品 Output 2:

[[[8 3 7 5]
  [7 2 6 1]
  [7 9 2 2]
  [3 1 8 8]]

 [[2 0 3 2]
  [2 9 0 8]
  [2 6 3 9]
  [2 4 4 8]]

 [[9 9 1 8]
  [9 1 5 0]
  [8 5 1 2]
  [2 7 5 1]]

 [[7 8 9 6]
  [9 0 9 5]
  [8 9 8 3]
  [7 3 6 3]]]

There is no need to split or anything;不需要拆分或任何东西; the same can be achieved by reshaping and reordering the axes.同样可以通过重塑和重新排序轴来实现。

result = np.swapaxes(arr.reshape(2, 2, 2, 2), 1, 2).reshape(-1, 2, 2)

Dividing an (N, N) array to (n, n) chunks is also basically a sliding window op with an (n, n) window and a stride of n.将 (N, N) 数组划分为 (n, n) 块基本上也是一个滑动 window 操作,带有 (n, n) window 和 n 步长。

from numpy.lib.stride_tricks import sliding_window_view

result = sliding_window_view(arr, (2, 2))[::2, ::2].reshape(-1, 2, 2)

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

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