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

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

Introduction

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

Prototype

@Override
    public int getId() 

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  ww  .jav a  2 s . co m*/
            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./* w w w.  j  av  a 2 s .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));
}