Wednesday, 17 April 2013

Unsupervised Learning Atoms: K-means for example.

For clustering we can use Scipy.cluster

http://css.dzone.com/articles/k-means-clustering-scipy


>>> from numpy import array
>>> from scipy.cluster.vq import vq, kmeans, whiten
>>> features  = array([[ 1.9,2.3],
...                    [ 1.5,2.5],
...                    [ 0.8,0.6],
...                    [ 0.4,1.8],
...                    [ 0.1,0.1],
...                    [ 0.2,1.8],
...                    [ 2.0,0.5],
...                    [ 0.3,1.5],
...                    [ 1.0,1.0]])
>>> whitened = whiten(features)
>>> book = array((whitened[0],whitened[2]))
>>> kmeans(whitened,book)
(array([[ 2.3110306 ,  2.86287398],
       [ 0.93218041,  1.24398691]]), 0.85684700941625547)
http://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.vq.kmeans.html

or 

PyCluster http://bonsai.hgc.jp/~mdehoon/software/cluster/software.htm

which is documented here. 

http://bonsai.hgc.jp/~mdehoon/software/cluster/cluster.pdf

or 

http://scikit-learn.org/stable/install.html#macports

Choose which of the above implementations are easiest to use quickly. 

http://scikit-learn.org/stable/tutorial/statistical_inference/unsupervised_learning.html#k-means-clustering

VERY SIMPLE K-MEANS IMPLEMENTATION above, i.e. 


from sklearn import cluster, datasets
iris = datasets.load_iris()
X_iris = iris.data
Y_iris = iris.target

k_means = cluster.KMeans(n_clusters = 3)
k_means.fit(X_iris)
print k_means.labels_[::10]
print Y_iris[::10]



No comments:

Post a Comment