@Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line, " "); int dim = tokenizer.countTokens(); double[] coords = new double[dim]; for (int i = 0; i < dim; i++) { coords[i] = Double.valueOf(tokenizer.nextToken()); } Point point = new Point(coords); Centroid nearest = null; double nearestDistance = Double.MAX_VALUE; for (Centroid c : centers) { double dist = point.euclideanDistance(c); if (nearest == null) { nearest = c; nearestDistance = dist; } else { if (dist < nearestDistance) { nearest = c; nearestDistance = dist; } } } context.write(nearest, point); }
public Point deepCopy() { Point out = new Point(); out.n = this.n; out.coord = new double[out.n]; for (int i = 0; i < out.n; i++) { out.coord[i] = this.coord[i]; } return out; }
public void map( LongWritable key, Point value, OutputCollector<LongWritable, Point> output, Reporter reporter) throws IOException { double min = value.sumOfSquares(centers.get(0)); int best = 0; for (int index = 1; index < numberOfCenters; ++index) { double current = value.sumOfSquares(centers.get(index)); if (current < min) { min = current; best = index; } } reporter.incrCounter("NUMBER", "NODES", 1); reporter.incrCounter("CENTER", "" + best, 1); output.collect(new LongWritable(best), value); }
@Override protected void map(Centroid key, Point value, Context context) throws IOException, InterruptedException { Centroid nearest = null; double nearestDistance = Double.MAX_VALUE; for (Centroid c : centers) { double dist = value.euclideanDistance(c); if (nearest == null) { nearest = c; nearestDistance = dist; } else { if (dist < nearestDistance) { nearest = c; nearestDistance = dist; } } } context.write(nearest, value); }
@Override protected void reduce(Centroid key, Iterable<Point> values, Context context) throws IOException, InterruptedException { ArrayList<Point> points = new ArrayList<Point>(); points.clear(); int clusterId = key.id; Point newCenter = null; for (Point p : values) { Point copy = p.deepCopy(); points.add(copy); if (newCenter == null) { newCenter = new Point(copy.deepCopy()); } else { newCenter = newCenter.add(copy); } } newCenter = newCenter.div(points.size()); Centroid center = new Centroid(clusterId, newCenter); centers.add(center); for (Point p : points) { context.write(center, p); } }
@Override protected void map(Centroid key, Point value, Context context) throws IOException, InterruptedException { String out = key.toString() + " " + value.toString(); context.write(new Text(out), new Text("")); }
@Override public void readFields(DataInput dataInput) throws IOException { super.readFields(dataInput); this.id = dataInput.readInt(); }
@Override public void write(DataOutput dataOutput) throws IOException { super.write(dataOutput); dataOutput.writeInt(id); }