Example usage for org.apache.mahout.clustering AbstractCluster getCenter

List of usage examples for org.apache.mahout.clustering AbstractCluster getCenter

Introduction

In this page you can find the example usage for org.apache.mahout.clustering AbstractCluster getCenter.

Prototype

@Override
    public Vector getCenter() 

Source Link

Usage

From source file:edu.indiana.d2i.htrc.kmeans.KMeansClusterer.java

License:Apache License

public void outputPointWithClusterInfo(Vector vector, Iterable<Cluster> clusters,
        Mapper<?, ?, IntWritable, WeightedPropertyVectorWritable>.Context context)
        throws IOException, InterruptedException {
    AbstractCluster nearestCluster = null;
    double nearestDistance = Double.MAX_VALUE;
    for (AbstractCluster cluster : clusters) {
        Vector clusterCenter = cluster.getCenter();
        double distance = measure.distance(clusterCenter.getLengthSquared(), clusterCenter, vector);
        if (distance < nearestDistance || nearestCluster == null) {
            nearestCluster = cluster;/*from   w  w w .ja v  a  2s .c  om*/
            nearestDistance = distance;
        }
    }
    Map<Text, Text> props = new HashMap<Text, Text>();
    props.put(new Text("distance"), new Text(String.valueOf(nearestDistance)));
    context.write(new IntWritable(nearestCluster.getId()),
            new WeightedPropertyVectorWritable(1, vector, props));
}

From source file:edu.indiana.d2i.htrc.kmeans.KMeansClusterer.java

License:Apache License

/**
 * Iterates over all clusters and identifies the one closes to the given point. Distance measure used is
 * configured at creation time./*from  w  ww .j a  va 2s.c  o  m*/
 * 
 * @param point
 *          a point to find a cluster for.
 * @param clusters
 *          a List<Cluster> to test.
 */
protected void emitPointToNearestCluster(Vector point, Iterable<Cluster> clusters, Writer writer)
        throws IOException {
    AbstractCluster nearestCluster = null;
    double nearestDistance = Double.MAX_VALUE;
    for (AbstractCluster cluster : clusters) {
        Vector clusterCenter = cluster.getCenter();
        double distance = this.measure.distance(clusterCenter.getLengthSquared(), clusterCenter, point);
        if (log.isDebugEnabled()) {
            log.debug("{} Cluster: {}", distance, cluster.getId());
        }
        if (distance < nearestDistance || nearestCluster == null) {
            nearestCluster = cluster;
            nearestDistance = distance;
        }
    }
    writer.append(new IntWritable(nearestCluster.getId()), new WeightedVectorWritable(1, point));
}