title: Python & Numpy - Finding the Mode of Values in an Array that aren't Zero
body: <p>I have an Numpy array (it's the red channel from an image).</p>
  
  <p>I have masked a portion of it (making those values 0), and now I would like to find the Mode of the values in my non masked area.</p>
  
  <p>The problem I'm running into is that the Mode command keeps coming back with [0]. I want to exclude the 0 values (the masked area), but I'm not sure how to do this?</p>
  
  <p>This is the command I was using to try and get mode:</p>
  
  <pre><code>#mR is the Numpy Array of the Red channel with the values of the areas I don't want at 0
  
  
  print(stats.mode(mR[:, :], axis=None))
  </code></pre>
  
  <p>Returns 0 as my Mode.
  How do I exclude 0 or the masked area?</p>
  
  <p><strong>Update - Full Code:</strong></p>
  
  <p>Here's my full code using the "face" from scipy.misc - still seems slow with that image and the result is "107" which is way to high for the masked area (shadows) so seems like it's processing the whole image, not just the area in the mask.</p>
  
  <pre><code>
  import cv2
  import numpy as np
  from scipy import stats
  import scipy.misc
  
  
  img = scipy.misc.face()
  
  img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  r, g, b = cv2.split(img_rgb)
  
  
  img_lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
  l_channel ,a_channel, b_channel = cv2.split(img_lab)
  
  
  mask = cv2.inRange(l_channel, 5, 10)
  cv2.imshow("mask", mask)
  
  
  print(stats.mode(r[mask],axis=None))
  
  
  cv2.waitKey(0)
  cv2.destroyAllWindows()
  cv2.waitKey(1)
  </code></pre>