title: Determining a threshold value for a bimodal distribution via KMeans clustering
body: <p>I'd like to find a threshold value for a bimodal distribution.  For example, a bimodal distribution could look like the following:</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  np.random.seed(45)
  n = 1000; b = n//10; i = np.random.randint(0,2,n)
  x = i*np.random.normal(-2.0,0.8,n) + (1-i)*np.random.normal(2.0,0.8,n)
  _ = plt.hist(x,bins=b)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Xcj2G.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Xcj2G.png" alt="bimodal_histogram"></a></p>
  
  <p>An attempt to find the cluster centers did not work, as I wasn't sure how the matrix, h, should be formatted:</p>
  
  <pre><code>from sklearn.cluster import KMeans
  h = np.histogram(x,bins=b)
  h = np.vstack((0.5*(h[1][:-1]+h[1][1:]),h[0])).T  # because h[0] and h[1] have different sizes.
  kmeans = KMeans(n_clusters=2).fit(h)
  </code></pre>
  
  <p>I would expect to be able to find the cluster centers around -2 and 2.  The threshold value would then be the midpoint of the two cluster centers.</p>