| Apache Lucene Mahout > index > k-Means |
k-Means is a rather simple but well known algorithms for grouping objects, clustering. Again all objects need to be represented as a set of numerical features. In addition the user has to specify the number of groups (referred to as k) he wishes to identify.
Each object can be thought of as being represented by some feature vector in an n dimensional space, n being the number of all features used to describe the objects to cluster. The algorithm than randomly chooses k points in that vector space, these point serve as the initial centers of the clusters. Afterwards all objects are each assigned to center they are closest to. Usually the distance measure is chosen by the user and determined by the learning task.
After that, for each cluster a new center is computed by averaging the feature vectors of all objects assigned to it. The process of assigning objects and recomputing centers is repeated until the process converges. The algorithm can be proven to converge after a finite number of iterations.
Several tweaks concerning distance measure, initial center choice and computation of new average centers have been explored, as well as the estimation of the number of clusters k. Yet the main principle always remains the same.
Some ideas can be found in Cluster computing and MapReduce
lecture video series [by Google(r)]; k-Mean clustering is discussed in lecture #4
. Slides can be found here
.
Interestingly, Hadoop based implementation using Canopy-clustering
seems to be here: http://code.google.com/p/canopy-clustering/
(GPL 3 licence)
Here's another useful paper http://www2.chass.ncsu.edu/garson/PA765/cluster.htm
.
The initial implementation in MAHOUT-5 accepts two input directories: one for the data points and one for the initial clusters. The data directory contains multiple input files containing dense vectors of Java type Float[] encoded as "[v1, v2, v3, ..., vn, ]", while the clusters directory contains a single file 'part-00000' which is in SequenceFile format and contains all of the initial cluster centers encoded as "Cn - [c1, c2, ..., cn, ]. None of the input directories are modified by the implementation, allowing experimentation with initial clustering and convergence values.
The program iterates over the input points and clusters, outputting a new directory "clustersN" containing a cluster center file "part-00000" for each iteration N. This process uses a mapper/combiner/reducer/driver as follows:
With the latest diff (MAHOUT-5c and newer), Canopy clustering can be used to compute the initial clusters for KMeans:
// now run the CanopyDriver job
CanopyDriver.runJob("testdata/points", "testdata/canopies"
ManhattanDistanceMeasure.class.getName(), (float) 3.1, (float) 2.1,
"dist/apache-mahout-0.1-dev.jar");// now run the KMeansDriver job
KMeansDriver.runJob("testdata/points", "testdata/canopies", "output",
EuclideanDistanceMeasure.class.getName(), "0.001", "10");
In the above example, the input data points are stored in 'testdata/points' and the CanopyDriver is configured to output to the 'testdata/canopies' directory. Once the driver executes it will contain the canopy definition file. Upon running the KMeansDriver the output directory will have two or more new directories: 'clustersN'' containining the clusters for each iteration and 'points' will contain the clustered data points.