title: Fill Holes with Majority of Surrounding Values (Python)
body: <p>I use Python and have an array with values 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 and np.nan as NoData.</p>
  
  <p>I want to fill all "nan" with a value. This value should be the majority of the surrounding values.</p>
  
  <p>For example:</p>
  
  <pre><code>1 1 1 1 1
  1 n 1 2 2
  1 3 3 2 1
  1 3 2 3 1
  </code></pre>
  
  <p>"n" shall present "nan" in this example. The majority of its neighbors have the value 1. Thus, "nan" shall get replaced by value 1.</p>
  
  <p>Note, that the holes consisting of "nan" can be of the size 1 to 5. For example (maximum size of 5 nan):</p>
  
  <pre><code>1 1 1 1 1
  1 n n n 2
  1 n n 2 1
  1 3 2 3 1
  </code></pre>
  
  <p>Here the hole of "nan" have the following surrounding values:</p>
  
  <pre><code>surrounding_values = [1,1,1,1,1,2,1,2,3,2,3,1,1,1] -&gt; Majority = 1
  </code></pre>
  
  <p>I tried the following code:</p>
  
  <pre><code>from sklearn.preprocessing import Imputer
  
  array = np.array(.......)   #consisting of 1.0-6.0 &amp; np.nan
  imp = Imputer(strategy="most_frequent")
  fill = imp.fit_transform(array)
  </code></pre>
  
  <p>This works pretty good. However, it only uses one axis (0 = column, 1 = row). The default is 0 (column), so it uses the majority of the surrounding values of the same column. For example:</p>
  
  <pre><code>Array
  2 1 2 1 1
  2 n 2 2 2
  2 1 2 2 1
  1 3 2 3 1
  
  Filled Array
  2 1 2 1 1
  2 1 2 2 2
  2 1 2 2 1
  1 3 2 3 1
  </code></pre>
  
  <p>So here you see, although the majority is 2, the majority of the surrounding column-values is 1 and thus it becomes 1 instead of 2.</p>
  
  <p>As a result, I need to find another method using python. Any suggestions or ideas?</p>
  
  <hr>
  
  <p>SUPPLEMENT:</p>
  
  <p>Here you see the result, after I added the very helpfull improvement of Martin Valgur.</p>
  
  <p><a href="https://i.stack.imgur.com/KlgEc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KlgEc.png" alt="enter image description here"></a></p>
  
  <p>Think of "0" as sea (blue) and of the other values (> 0) as land (red).</p>
  
  <p>If there is a "little" sea surrounded by land (the sea can again have the size 1-5 px) it will get land, as you can successfully see in the result-image. If the surrounded sea is bigger than 5px or outside the land, the sea wont gain land (This is not visible in the image, because it is not the case).</p>
  
  <p>If there is 1px "nan" with more majority of sea than land, it will still become land (In this example it has 50/50).</p>
  
  <p>The following picture shows what I need. At the border between sea (value=0) and land (value>0), the "nan"-pixel needs to get the value of the majority of the land-values.</p>
  
  <p><a href="https://i.stack.imgur.com/6aDFm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6aDFm.png" alt="enter image description here"></a></p>
  
  <p>That sounds difficult and I hope that I could explain it vividly.</p>