的numpy的数组的固定大小的子矩阵索引 [英] Indexes of fixed size sub-matrices of numpy array

查看:241
本文介绍了的numpy的数组的固定大小的子矩阵索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现它要求我看不重叠的连续子矩阵A(严格二维)numpy的阵列内的算法。例如,对于12 12

I am implementing an algorithm which requires me to look at non-overlapping consecutive submatrices within a (strictly two dimensional) numpy array. eg, for the 12 by 12

>>> a = np.random.randint(20, size=(12, 12)); a
array([[ 4,  0, 12, 14,  3,  8, 14, 12, 11, 18,  6,  6],
       [15, 13,  2, 18, 15, 15, 16,  2,  9, 16,  6,  4],
       [18, 18,  3,  8,  1, 15, 14, 13, 13, 13,  7,  0],
       [ 1,  9,  3,  6,  0,  4,  3, 15,  0,  9, 11, 12],
       [ 5, 15,  5,  6,  4,  4, 18, 13, 10, 17, 11,  8],
       [13, 17,  8, 15, 17, 12,  7,  1, 13, 15,  0, 18],
       [ 2,  1, 11, 12,  3, 16, 11,  9, 10, 15,  4, 16],
       [19, 11, 10,  7, 10, 19,  7, 13, 11,  9, 17,  8],
       [14, 14, 17,  0,  0,  0, 11,  1, 10, 14,  2,  7],
       [ 6, 15,  6,  7, 15, 19,  2,  4,  6, 16,  0,  3],
       [ 5, 10,  7,  5,  0,  8,  5,  8,  9, 14,  4,  3],
       [17,  2,  0,  3, 15, 10, 14,  1,  0,  7, 16,  2]])

和看3x3的子矩阵,我想第一个3×3子矩阵是从左上角:

and looking at 3x3 submatrices, I would want the first 3x3 submatrix to be from the upper left corner:

>>> a[0:3, 0:3]
array([[ 4,  0, 12],
       [15, 13,  2],
       [18, 18,  3]])

接下来一起通过给予

A [0:3,3:6] 等。如果最后一次集中的每一行或列中指数的运行了数组的结束不要紧 - 简单地给存在足以片内的部分numpy的行为

The next along to be given by a[0:3, 3:6] and so on. It doesn't matter if the last such set of indices in each row or column runs off the end of the array - numpy's behavior of simply giving the portion within the slice that exists is sufficient.

我想要的方式来编程生成这些切片指数任意大小的矩阵和子矩阵。目前,我有这样的:

I want a way to generate these slice indices programatically for arbitrarily sized matrices and submatrices. I currently have this:

size = 3
x_max = a.shape[0]
xcoords = range(0, x_max, size)
xcoords = zip(xcoords, xcoords[1:])

和同样产生 y_coords ,使该系列指数是由给出itertools.product(xcoords,ycoords)

and similarly to generate y_coords, so that the series of indices is given by itertools.product(xcoords, ycoords).

我的问题是:是否有一个更直接的方式做到这一点,可能使用<一个href=\"http://docs.scipy.org/doc/numpy/reference/generated/numpy.mgrid.html#numpy.mgrid\"><$c$c>numpy.mgrid或其他一些numpy的技术?

My question is: is there a more direct way to do this, perhaps using numpy.mgrid or some other numpy technique?

推荐答案

下面是一个快速的方法来获取特定的尺寸X尺寸块:

Getting the indexes

Here's a quick way to get a specific size x size block:

base = np.arange(size) # Just the base set of indexes
row = 1                # Which block you want
col = 0                
block = a[base[:, np.newaxis] + row * size, base + col * size]

如果你想你可以建立类似矩阵你的 xcoords 这样的:

If you wanted you could build up matrices similar to your xcoords like:

y, x = np.mgrid[0:a.shape[0]/size, 0:a.shape[1]/size]
y_coords = y[..., np.newaxis] * size + base
x_coords = x[..., np.newaxis] * size + base

然后,你可以访问一个块是这样的:

Then you could access a block like this:

block = a[y_coords[row, col][:, np.newaxis], x_coords[row, col]]


直接获取块

如果你只是想获得块(和块条目不是索引),我会使用的 np.split (两次):

blocks = map(lambda x : np.split(x, a.shape[1]/size, 1), # Split the columns
                        np.split(a, a.shape[0]/size, 0)) # Split the rows

那么你有尺寸X尺寸块的二维列表:

>>> blocks[0][0]
array([[ 4,  0, 12],
       [15, 13,  2],
       [18, 18,  3]])

>>> blocks[1][0]
array([[ 1,  9,  3],
       [ 5, 15,  5],
       [13, 17,  8]])

您可以再此做一个numpy的数组,并使用相同的索引风格如上:

You could then make this a numpy array and use the same indexing style as above:

>>> blocks = np.array(blocks)
>>> blocks.shape
(4, 4, 3, 3)

这篇关于的numpy的数组的固定大小的子矩阵索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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