## org.apache.mahout.clustering.kmeans
Class KMeansClusterer

    java.lang.Object
      org.apache.mahout.clustering.kmeans.KMeansClusterer

    public class KMeansClusterer

extends java.lang.Object

This class implements the k-means clustering algorithm. It uses `Cluster` as a cluster representation. The class can be used as part of a clustering job to be started as map/reduce job.

Constructor Summary
`KMeansClusterer(org.apache.hadoop.conf.Configuration conf)`
`KMeansClusterer(DistanceMeasure measure)` Init the k-means clusterer with the distance measure to use for comparison.

Method Summary
`protected void` `addPointToNearestCluster(Vector point, java.lang.Iterable<Cluster> clusters)` Sequential implementation to add point to the nearest cluster
`static java.util.List<java.util.List<Cluster>>` `clusterPoints(java.lang.Iterable<Vector> points, java.util.List<Cluster> clusters, DistanceMeasure measure, int maxIter, double distanceThreshold)` This is the reference k-means implementation.
`boolean` `computeConvergence(Cluster cluster)`
`void` `emitPointToNearestCluster(Vector point, java.lang.Iterable<Cluster> clusters, org.apache.hadoop.mapreduce.Mapper.Context context)` Iterates over all clusters and identifies the one closes to the given point.
`protected void` `emitPointToNearestCluster(Vector point, java.lang.Iterable<Cluster> clusters, org.apache.hadoop.io.SequenceFile.Writer writer)` Iterates over all clusters and identifies the one closes to the given point.
`void` `outputPointWithClusterInfo(Vector vector, java.lang.Iterable<Cluster> clusters, org.apache.hadoop.mapreduce.Mapper.Context context)`
`protected static boolean` `runKMeansIteration(java.lang.Iterable<Vector> points, java.lang.Iterable<Cluster> clusters, DistanceMeasure measure, double distanceThreshold)` Perform a single iteration over the points and clusters, assigning points to clusters and returning if the iterations are completed.
`protected boolean` `testConvergence(java.lang.Iterable<Cluster> clusters, double distanceThreshold)` Sequential implementation to test convergence and update cluster centers

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### KMeansClusterer

    public KMeansClusterer(DistanceMeasure measure)

  Init the k-means clusterer with the distance measure to use for comparison.

  Parameters:
    `measure` - The distance measure to use for comparing clusters against points.

### KMeansClusterer

    public KMeansClusterer(org.apache.hadoop.conf.Configuration conf)
                    throws java.lang.ClassNotFoundException,
                           java.lang.InstantiationException,
                           java.lang.IllegalAccessException

Throws:
  `java.lang.ClassNotFoundException`
  `java.lang.InstantiationException`
  `java.lang.IllegalAccessException`

Method Detail

### emitPointToNearestCluster

    public void emitPointToNearestCluster(Vector point,
                                          java.lang.Iterable<Cluster> clusters,
                                          org.apache.hadoop.mapreduce.Mapper.Context context)
                                   throws java.io.IOException,
                                          java.lang.InterruptedException

  Iterates over all clusters and identifies the one closes to the given point. Distance measure used is configured at creation time.

  Parameters:
    `point` - a point to find a cluster for.
    `clusters` - a List<Cluster> to test.
  Throws:
    `java.io.IOException`
    `java.lang.InterruptedException`

### addPointToNearestCluster

    protected void addPointToNearestCluster(Vector point,
                                            java.lang.Iterable<Cluster> clusters)

  Sequential implementation to add point to the nearest cluster

  Parameters:
    `point` -
    `clusters` -

### testConvergence

    protected boolean testConvergence(java.lang.Iterable<Cluster> clusters,
                                      double distanceThreshold)

  Sequential implementation to test convergence and update cluster centers

### outputPointWithClusterInfo

    public void outputPointWithClusterInfo(Vector vector,
                                           java.lang.Iterable<Cluster> clusters,
                                           org.apache.hadoop.mapreduce.Mapper.Context context)
                                    throws java.io.IOException,
                                           java.lang.InterruptedException

  Throws:
    `java.io.IOException`
    `java.lang.InterruptedException`

### emitPointToNearestCluster

    protected void emitPointToNearestCluster(Vector point,
                                             java.lang.Iterable<Cluster> clusters,
                                             org.apache.hadoop.io.SequenceFile.Writer writer)
                                      throws java.io.IOException,
                                             java.lang.InterruptedException

  Iterates over all clusters and identifies the one closes to the given point. Distance measure used is configured at creation time.

  Parameters:
    `point` - a point to find a cluster for.
    `clusters` - a List<Cluster> to test.
  Throws:
    `java.io.IOException`
    `java.lang.InterruptedException`

### clusterPoints

    public static java.util.List<java.util.List<Cluster>> clusterPoints(java.lang.Iterable<Vector> points,
                                                                        java.util.List<Cluster> clusters,
                                                                        DistanceMeasure measure,
                                                                        int maxIter,
                                                                        double distanceThreshold)

  This is the reference k-means implementation. Given its inputs it iterates over the points and clusters until their centers converge or until the maximum number of iterations is exceeded.

  Parameters:
    `points` - the input List<Vector> of points
    `clusters` - the List<Cluster> of initial clusters
    `measure` - the DistanceMeasure to use
    `maxIter` - the maximum number of iterations

### runKMeansIteration

    protected static boolean runKMeansIteration(java.lang.Iterable<Vector> points,
                                                java.lang.Iterable<Cluster> clusters,
                                                DistanceMeasure measure,
                                                double distanceThreshold)

  Perform a single iteration over the points and clusters, assigning points to clusters and returning if the iterations are completed.

  Parameters:
    `points` - the List<Vector> having the input points
    `clusters` - the List<Cluster> clusters
    `measure` - a DistanceMeasure to use

### computeConvergence

    public boolean computeConvergence(Cluster cluster)

Copyright © 2008-2010 The Apache Software Foundation. All Rights Reserved.