Easiest way to return sum of a matrix's neighbors in numpy
<p>I am trying to make a program that needs a matrix's neighbor(excluding itself) sum ex:</p>

<pre><code> matrix([[0, 0, 0],
        [1, 0, 1],
        [0, 1, 0]])  
</code></pre>

<p>would return:</p>

<pre><code>matrix([[1, 2, 1],
        [1, 3, 1],
        [2, 2, 2]])
</code></pre>

<p>I have a working code here but its big and messy and I'm new to numpy so I need some help cleaning it up and optimizing. (I feel like there has to be a better way)</p>

<p>example code:</p>

<pre><code>import numpy as np

def NiSum(m):
    new = []
    for x in range(m.shape[0]-1):
        row = []
        for y in range(m.shape[1]-1):
            Ni = 0
            for a in [1,1],[1,0],[1,-1],[0,1],[0,-1],[-1,1],[-1,0],[-1,-1]:
                Ni += m[x+a[0],y+a[1]]
            row.append(Ni)
        new.append(row)
    return np.matrix(new)


example = np.matrix('0 0 0 0 0 0 0 0; '*3+'0 0 0 1 1 1 0 0; '*3+'0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 ')

NiSum(example)
</code></pre>

<p>Thanks for any help !</p>

<p>You are summing all values in that <code>3x3</code> neighbourhood, but excluding the element itself. So, we can use <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.convolve2d.html" rel="nofollow"><code>Scipy's 2D convolution</code></a> and subtract that input array/matrix from it for the desired output, like so -</p>

<pre><code>from scipy.signal import convolve2d

convolve2d(a,np.ones((3,3),dtype=int),'same') - a
</code></pre>

<p>Sample run -</p>

<pre><code>In [11]: a
Out[11]: 
matrix([[0, 0, 0],
        [1, 0, 1],
        [0, 1, 0]])

In [12]: convolve2d(a,np.ones((3,3),dtype=int),'same') - a
Out[12]: 
matrix([[1, 2, 1],
        [1, 3, 1],
        [2, 2, 2]])
</code></pre>

<p>Or simply form a kernel with all ones but zero at the center and use the same 2D convolution -</p>

<pre><code>In [31]: kernel = np.array([[1,1,1],[1,0,1],[1,1,1]])

In [32]: np.asmatrix(convolve2d(a,kernel,'same'))
Out[32]: 
matrix([[1, 2, 1],
        [1, 3, 1],
        [2, 2, 2]])
</code></pre>

python python-3.x numpy matrix scipy