Sum pattern across array Sum pattern across array numpy numpy

Sum pattern across array


This operation is called a 2-dimensional convolution:

>>> import numpy as np>>> from scipy.signal import convolve2d>>> kernel = np.eye(2, dtype=int)>>> a = np.array([[5,3,7,1,2],[3,2,9,4,7],[8,9,4,2,3]])>>> convolve2d(a, kernel, mode='valid')array([[ 7, 12, 11,  8],       [12,  6, 11,  7]])

Should you want to generalize it to arbitrary dimensions, there is also scipy.ndimage.convolve available. It will also work for this 2d case, but does not offer the mode='valid' convenience.


l = [[5,3,7,1,2],[3,2,9,4,7],[8,9,4,2,3]][q+l[w+1][t+1] for w,i in enumerate(l[:-1]) for t,q in enumerate(i[:-1])]

then you can avoid using numpy :) and the output is

[7,12,11,8,12,6,11,7]