title: Python: Find mean of points within radius of a an element in 2D array
body: <p>I am looking for an efficient way to find the mean of of values with a certain radius of an element in a 2D NumPy array, excluding the center point and values &lt; 0.</p>
  
  <p>My current method is to create a disc shaped mask (using the method <a href="https://stackoverflow.com/questions/8647024/how-to-apply-a-disc-shaped-mask-to-a-numpy-array">here</a>) and find the mean of points within this mask. This is taking too long however...over 10 minutes to calculate ~18000 points within my 300x300 array.</p>
  
  <p>The array I want to find means within is here titled "arr"</p>
  
  <pre><code>def radMask(index,radius,array,insert):
    a,b = index
    nx,ny = array.shape
    y,x = np.ogrid[-a:nx-a,-b:ny-b]
    mask = x*x + y*y &lt;= radius*radius
    array[mask] = insert
  
    return array
  
  arr_mask          = np.zeros_like(arr).astype(int)
  arr_mask          = radMask(center, radius, arr_mask, 1)
  arr_mask[arr &lt; 0] = 0  #Exclude points with no echo
  arr_mask[ind]     = 0  #Exclude center point
  
  arr_mean = 0
  if np.any(dbz_bg):
      arr_mean = sp.mean(arr[arr_mask])
  </code></pre>
  
  <p>Is there any more efficient way to do this? I've looked into some of the image processing filters/tools but can't quite wrap my head around it.</p>