在numpy中,如何有效列出所有固定大小的子矩阵? [英] In numpy, how to efficiently list all fixed-size submatrices?

查看:94
本文介绍了在numpy中,如何有效列出所有固定大小的子矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任意的NxM矩阵,例如:

I have an arbitrary NxM matrix, for example:

1 2 3 4 5 6
7 8 9 0 1 2
3 4 5 6 7 8
9 0 1 2 3 4

我想获得此矩阵中所有3x3子矩阵的列表:

I want to get a list of all 3x3 submatrices in this matrix:

1 2 3       2 3 4               0 1 2
7 8 9   ;   8 9 0   ;  ...  ;   6 7 8
3 4 5       4 5 6               2 3 4

我可以通过两个嵌套循环来做到这一点:

I can do this with two nested loops:

rows, cols = input_matrix.shape
patches = []
for row in np.arange(0, rows - 3):
    for col in np.arange(0, cols - 3):
        patches.append(input_matrix[row:row+3, col:col+3])

但是对于大型输入矩阵,这很慢. 是否可以使用numpy更快地完成此操作?

But for a large input matrix, this is slow. Is there a way to do this faster with numpy?

我看过np.split,但是这给了我不重叠的子矩阵,而我想要所有可能的子矩阵,而不管它们是否重叠.

I've looked at np.split, but that gives me non-overlapping sub-matrices, whereas I want all possible submatrices, regardless of overlap.

推荐答案

您想要一个窗口视图:

from numpy.lib.stride_tricks import as_strided

arr = np.arange(1, 25).reshape(4, 6) % 10
sub_shape = (3, 3)
view_shape = tuple(np.subtract(arr.shape, sub_shape) + 1) + sub_shape
arr_view = as_strided(arr, view_shape, arr.strides * 2
arr_view = arr_view.reshape((-1,) + sub_shape)

>>> arr_view
array([[[[1, 2, 3],
         [7, 8, 9],
         [3, 4, 5]],

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

        ...

        [[9, 0, 1],
         [5, 6, 7],
         [1, 2, 3]],

        [[0, 1, 2],
         [6, 7, 8],
         [2, 3, 4]]]])

这样做的好处在于,您无需复制任何数据,而只是以不同的方式访问原始数组的数据.对于大型阵列,这可以节省大量内存.

The good part of doing it like this is that you are not copying any data, you are simply accessing the data of your original array in a different way. For large arrays this can result in tremendous memory savings.

这篇关于在numpy中,如何有效列出所有固定大小的子矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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