Package org.apache.mahout.clustering

Examples of org.apache.mahout.clustering.AbstractCluster


  public void outputPointWithClusterInfo(Vector vector,
                                         Iterable<Cluster> clusters,
                                         Mapper<?,?,IntWritable,WeightedVectorWritable>.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;
        nearestDistance = distance;
      }
    }
    context.write(new IntWritable(nearestCluster.getId()), new WeightedVectorWritable(1, vector));
  }
View Full Code Here


   * @param clusters
   *          a List<Cluster> to test.
   */
  protected void emitPointToNearestCluster(Vector point, Iterable<Cluster> clusters, Writer writer)
      throws IOException, InterruptedException {
    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));
  }
View Full Code Here

  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;
        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));
  }
View Full Code Here

   * @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));
  }
View Full Code Here

      // iterate clusters until they converge
      int maxIter = 10;
      List<List<Cluster>> clustersList = KMeansClusterer.clusterPoints(points, clusters, measure, maxIter, 0.001);
      clusters = clustersList.get(clustersList.size() - 1);
      for (int c = 0; c < clusters.size(); c++) {
        AbstractCluster cluster = clusters.get(c);
        System.out.println(cluster.asFormatString(null));
        assertEquals("Cluster " + c + " test " + (k + 1), EXPECTED_NUM_POINTS[k][c], cluster.getNumPoints());
      }
    }
  }
View Full Code Here

        mapper.map(new Text(), point, mapContext);
      }
      assertEquals("Number of map results", k + 1, mapWriter.getData().size());
      Map<String, Cluster> clusterMap = loadClusterMap(clusters);
      for (Text key : mapWriter.getKeys()) {
        AbstractCluster cluster = clusterMap.get(key.toString());
        List<ClusterObservations> values = mapWriter.getValue(key);
        for (ClusterObservations value : values) {
          double distance = measure.distance(cluster.getCenter(), value.getS1());
          for (AbstractCluster c : clusters) {
            assertTrue("distance error", distance <= measure.distance(value.getS1(), c.getCenter()));
          }
        }
      }
View Full Code Here

   
    RandomSeedGenerator.buildRandom(input, output, 4, new ManhattanDistanceMeasure());
   
    SequenceFile.Reader reader = new SequenceFile.Reader(fs, new Path(output, "part-randomSeed"), conf);
    Writable key = reader.getKeyClass().asSubclass(Writable.class).newInstance();
    AbstractCluster value = (AbstractCluster) reader.getValueClass().newInstance();
   
    int clusterCount = 0;
    Collection<Integer> set = new HashSet<Integer>();
    while (reader.next(key, value)) {
      clusterCount++;
      int id = value.getId();
      assertTrue(set.add(id)); // validate unique id's
     
      Vector v = value.getCenter();
      assertVectorEquals(RAW[id], v); // validate values match
    }

    assertEquals(4, clusterCount); // validate sample count
  }
View Full Code Here

      // iterate clusters until they converge
      int maxIter = 10;
      List<List<Cluster>> clustersList = KMeansClusterer.clusterPoints(points, clusters, measure, maxIter, 0.001);
      clusters = clustersList.get(clustersList.size() - 1);
      for (int c = 0; c < clusters.size(); c++) {
        AbstractCluster cluster = clusters.get(c);
        System.out.println(cluster.asFormatString(null));
        assertEquals("Cluster " + c + " test " + (k + 1), EXPECTED_NUM_POINTS[k][c], cluster.getNumPoints());
      }
    }
  }
View Full Code Here

        mapper.map(new Text(), point, mapContext);
      }
      assertEquals("Number of map results", k + 1, mapWriter.getData().size());
      Map<String, Cluster> clusterMap = loadClusterMap(clusters);
      for (Text key : mapWriter.getKeys()) {
        AbstractCluster cluster = clusterMap.get(key.toString());
        List<ClusterObservations> values = mapWriter.getValue(key);
        for (ClusterObservations value : values) {
          double distance = measure.distance(cluster.getCenter(), value.getS1());
          for (AbstractCluster c : clusters) {
            assertTrue("distance error", distance <= measure.distance(value.getS1(), c.getCenter()));
          }
        }
      }
View Full Code Here

      // iterate clusters until they converge
      int maxIter = 10;
      List<List<Cluster>> clustersList = KMeansClusterer.clusterPoints(points, clusters, measure, maxIter, 0.001);
      clusters = clustersList.get(clustersList.size() - 1);
      for (int c = 0; c < clusters.size(); c++) {
        AbstractCluster cluster = clusters.get(c);
        System.out.println(cluster.asFormatString(null));
        assertEquals("Cluster " + c + " test " + (k + 1), EXPECTED_NUM_POINTS[k][c], cluster.getNumPoints());
      }
    }
  }
View Full Code Here

TOP

Related Classes of org.apache.mahout.clustering.AbstractCluster

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.