Python Clustering 'purity' metric
Question: I'm using a [Gaussian Mixture Model (GMM)](http://scikit-
learn.org/stable/modules/generated/sklearn.mixture.GMM.html) from
`sklearn.mixture` to perform clustering of my data set.

I could use the function `score()` to compute the log probability under the
model.

However, I am looking for a metric called 'purity' which is defined in [this
article](http://nlp.stanford.edu/IR-book/html/htmledition/evaluation-of-
clustering-1.html).

How can I implement it in Python? My current implementation looks like this:

    
    
    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)
    

But I can not loop through each cluster in order to compute the confusion
matrix (according this
[question](http://stats.stackexchange.com/a/154379/89612))


Answer: `sklearn` doesn't implement a cluster purity metric. You have 2 options:

  1. Implement the measurement using `sklearn` data structures yourself. [This](http://www.caner.io/purity-in-python.html) and [this](https://pml.readthedocs.org/en/latest/_modules/pml/unsupervised/clustering.html) have some python source for measuring purity, but either your data or the function bodies need to be adapted to how you're representing your data.

  2. Use the (much less mature) [PML](https://pml.readthedocs.org/en/latest/clustering.html) library, which does implement cluster purity.

