title: Python Clustering 'purity' metric
body: <p>I'm using a <a href="http://scikit-learn.org/stable/modules/generated/sklearn.mixture.GMM.html" rel="noreferrer">Gaussian Mixture Model (GMM)</a> from <code>sklearn.mixture</code> to perform clustering of my data set.</p>
  
  <p>I could use the function <code>score()</code> to compute the log probability under the model.</p>
  
  <p>However, I am looking for a metric called 'purity' which is defined in <a href="http://nlp.stanford.edu/IR-book/html/htmledition/evaluation-of-clustering-1.html" rel="noreferrer">this article</a>.</p>
  
  <p>How can I implement it in Python?  My current implementation looks like this:</p>
  
  <pre><code>from sklearn.mixture import GMM
  
  # X is a 1000 x 2 array (1000 samples of 2 coordinates).
  # It is actually a 2 dimensional PCA projection of data
  # extracted from the MNIST dataset, but this random array
  # is equivalent as far as the code is concerned.
  X = np.random.rand(1000, 2)
  
  clusterer = GMM(3, 'diag')
  clusterer.fit(X)
  cluster_labels = clusterer.predict(X)
  
  # Now I can count the labels for each cluster..
  count0 = list(cluster_labels).count(0)
  count1 = list(cluster_labels).count(1)
  count2 = list(cluster_labels).count(2)
  </code></pre>
  
  <p>But I can not loop through each cluster in order to compute the confusion matrix (according this <a href="https://stats.stackexchange.com/a/154379/89612">question</a>)</p>