title: Majority filter Numpy array
body: <p>I have a numpy ndarray comprises of zeros, ones and NaNs. I would like to use a majority filter on that array, meaning that I would like to set a kernel window (e.g., 3X3 cells) that will go over the array and will change the value of the cell in the center to the value that occur the most in its neighbors. This filter should sustain two constrains, it should ignore NaNs and if the value of the center cell is one, then it should keep it one. </p>
  
  <p>Here is a small example of what I'm looking for:
  input array: </p>
  
  <pre><code>array([[ 1.,  1.,  1.,  0.,  0.],
         [ 1.,  1., nan,  1.,  1.],
         [nan,  1.,  1.,  0.,  1.],
         [ 0.,  0.,  0.,  0.,  1.]])
  </code></pre>
  
  <p>Apply majority filter
  output array:</p>
  
  <pre><code>array([[ 1.,  1.,  1.,  1.,  1.],
         [ 1.,  1., nan,  1.,  1.],
         [nan,  1.,  1.,  1.,  1.],
         [ 0.,  0.,  0.,  1.,  1.]])
  </code></pre>
  
  <p>I was looking at <a href="https://docs.scipy.org/doc/scipy-0.14.0/reference/ndimage.html" rel="nofollow noreferrer">scipy filters</a> but could not find anything adequate. I thought to build a <a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.convolve.html" rel="nofollow noreferrer">generic convolved filter</a>, but I'm not sure how to do that for majority purpose. It feels that this is quit basic filter that should be out there, but I can't seem to find it.</p>