如何在 numpy 中获取给定大小的所有子矩阵?

How to get all submatrices of given size in numpy?

例如,x = np.random.randint(low=0, high=10, shape=(6,6)) 给我一个 6x6 numpy 数组:

array([[3, 1, 0, 1, 5, 4],
       [2, 9, 9, 4, 8, 8],
       [2, 3, 4, 3, 2, 9],
       [5, 8, 4, 5, 7, 6],
       [3, 0, 8, 1, 8, 0],
       [6, 7, 1, 9, 0, 5]])

如何获得所有 2x3 子矩阵的列表?不重叠的呢?

我可以自己编写代码,但我确信这是一个足够常见的操作,它已经存在于 numpy 中,我只是找不到它。

在此 post 中列出的是获取具有给定形状的子矩阵列表的通用方法。根据子矩阵的顺序是行(C 风格)还是列主要(fortran 方式),您将有两种选择。这是 np.reshape , np.transpose and np.array_split -

的实现
def split_submatrix(x,submat_shape,order='C'):
    p,q = submat_shape      # Store submatrix shape
    m,n = x.shape

    if np.any(np.mod(x.shape,np.array(submat_shape))!=0):
        raise Exception('Input array shape is not divisible by submatrix shape!')

    if order == 'C':
        x4D = x.reshape(-1,p,n/q,q).transpose(0,2,1,3).reshape(-1,p,q)
        return np.array_split(x4D,x.size/(p*q),axis=0)

    elif order == 'F':
        x2D = x.reshape(-1,n/q,q).transpose(1,0,2).reshape(-1,q)
        return np.array_split(x2D,x.size/(p*q),axis=0)

    else:
        print "Invalid output order."
        return x

样本 运行 修改样本输入 -

In [201]: x
Out[201]: 
array([[5, 2, 5, 6, 5, 6, 1, 5],
       [1, 1, 8, 4, 4, 5, 2, 5],
       [4, 1, 6, 5, 6, 4, 6, 1],
       [5, 3, 7, 0, 5, 8, 6, 5],
       [7, 7, 0, 6, 5, 2, 5, 4],
       [3, 4, 2, 5, 0, 7, 5, 0]])

In [202]: split_submatrix(x,(3,4))
Out[202]: 
[array([[[5, 2, 5, 6],
         [1, 1, 8, 4],
         [4, 1, 6, 5]]]), array([[[5, 6, 1, 5],
         [4, 5, 2, 5],
         [6, 4, 6, 1]]]), array([[[5, 3, 7, 0],
         [7, 7, 0, 6],
         [3, 4, 2, 5]]]), array([[[5, 8, 6, 5],
         [5, 2, 5, 4],
         [0, 7, 5, 0]]])]

In [203]: split_submatrix(x,(3,4),order='F')
Out[203]: 
[array([[5, 2, 5, 6],
        [1, 1, 8, 4],
        [4, 1, 6, 5]]), array([[5, 3, 7, 0],
        [7, 7, 0, 6],
        [3, 4, 2, 5]]), array([[5, 6, 1, 5],
        [4, 5, 2, 5],
        [6, 4, 6, 1]]), array([[5, 8, 6, 5],
        [5, 2, 5, 4],
        [0, 7, 5, 0]])]