##
## CLUSTERING
##

## 1. Gaussian mixture models
##
## See the documentation at
## http://scikit-learn.org/stable/modules/mixture.html#gmm
## section 2.1.1
##


import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

from sklearn import datasets
from sklearn.cross_validation import StratifiedKFold
from sklearn.externals.six.moves import xrange
from sklearn.mixture import GMM

## Main steps for GMM clustering:

#-get the data
iris = datasets.load_iris()
X = iris.data
y = iris.target

#-fit a model (example):
n_classes = 3
clst = GMM(n_components=n_classes, covariance_type='diag', init_params='wm', n_iter=20)
clst.fit(X)

#-inspect the fitted parameters:
clst.means_    # is the matrix of component centers, one per row
clst.weights_  # these are the mixing coefficients

#-you can assign the data points to a cluster:
y_pred = clst.predict(X)

#-and compare with the true labels:
sum(y != y_pred) / y.size

## See also the example at
## http://scikit-learn.org/stable/auto_examples/mixture/plot_gmm_classifier.html#example-mixture-plot-gmm-classifier-py
##.Please note that while in the example the GMMs are used for classification, 
## the basic usage is for clustering (or density estimation). The .fit() method 
## does not need the labels...


## 2. K-means clustering
##
## See the documentation at
## http://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html#sklearn.cluster.KMeans
##
## Note that there is a parameter, n_jobs, which allows you running the
## code on several CPUs. Great spead-up for large data sets.

## The basic steps are as before:
from sklearn.cluster import  KMeans

#-fit a model (example):
n_classes = 3
clst = KMeans(n_clusters=n_classes)
clst.fit(X)

#-inspect the fitted parameters:
clst.cluster_centers_    # is the matrix of component centers, one per row

#-you can assign the data points to a cluster:
y_pred = clst.predict(X)

#-and compare with the true labels:
sum(y != y_pred) / y.size  ## Why does it not work, as you woud expect??

## Keep in mind this! (we were lucky for GMMs :)  )

## NOTE: For large data sets there is a "batch" version of KMeans, which
## converges much faster. Look at:
## http://scikit-learn.org/stable/modules/generated/sklearn.cluster.MiniBatchKMeans.html#sklearn.cluster.MiniBatchKMeans
##
## And see the discussion:
## http://scikit-learn.org/stable/auto_examples/cluster/plot_mini_batch_kmeans.html


## 2.1 Color quantization
## An important application of KMeans is in image processing. For example in
## re-quantizing the color levels. For this, execute the example at
## http://scikit-learn.org/stable/auto_examples/cluster/plot_color_quantization.html

## TODO: (it time allows)
## use a different clustering method - explore the options from
## -mean shift: http://scikit-learn.org/stable/modules/generated/sklearn.cluster.MeanShift.html#sklearn.cluster.MeanShift
## -spectral clustering: http://scikit-learn.org/stable/modules/generated/sklearn.cluster.SpectralClustering.html#sklearn.cluster.SpectralClustering
## -hierarchical clustering (Ward): http://scikit-learn.org/stable/modules/generated/sklearn.cluster.Ward.html#sklearn.cluster.Ward


