Package org.apache.mahout.math

Examples of org.apache.mahout.math.Vector


    assertEquals("format", "C123: [fee:1.100, 1:2.200, foo:3.300]", formatString);
  }

  public void testMSCanopyAsFormatStringSparseWithBindings() {
    double[] d = { 1.1, 0.0, 3.3 };
    Vector m = new SequentialAccessSparseVector(3);
    m.assign(d);
    Printable cluster = new MeanShiftCanopy(m, 123);
    String[] bindings = { "fee", null, "foo" };
    String formatString = cluster.asFormatString(bindings);
    System.out.println(formatString);
    assertEquals("format", "C123: [fee:1.100, foo:3.300]", formatString);
View Full Code Here


   * */
  @Override
  protected void update(double label, Vector dataPoint, LinearModel model) {
    if (label > 0) {
      // case one
      Vector updateVector = dataPoint.times(1 / this.promotionStep);
      log.info("Winnow update positive: {}", updateVector);
      Iterator<Element> iter = updateVector.iterateNonZero();
      while (iter.hasNext()) {
        Element element = iter.next();
        model.timesDelta(element.index(), element.get());
      }
    } else {
      // case two
      Vector updateVector = dataPoint.times(1 / this.promotionStep);
      log.info("Winnow update negative: {}", updateVector);
      Iterator<Element> iter = updateVector.iterateNonZero();
      while (iter.hasNext()) {
        Element element = iter.next();
        model.timesDelta(element.index(), element.get());
      }
    }
View Full Code Here

    Path outputFile = new Path(outputPath, "part-00000");
    SequenceFile.Reader reader = new SequenceFile.Reader(fs, outputFile, conf);
    NullWritable n = NullWritable.get();
    VectorWritable v = new VectorWritable();
    reader.next(n,v);
    Vector vector = v.get();
    reader.close();
    fs.deleteOnExit(outputFile);
    return vector;
  }
View Full Code Here

      }
     
      converged = true;
      int columnCount = dataset.size()[1];
      for (int i = 0; i < columnCount; i++) {
        Vector dataPoint = dataset.getColumn(i);
        LinearTrainer.LOG.debug("Training point: " + dataPoint);
       
        synchronized (this.model) {
          boolean prediction = model.classify(dataPoint);
          double label = labelset.get(i);
View Full Code Here

      System.out.println("test k= " + k);
     
      List<SoftCluster> clusterList = new ArrayList<SoftCluster>();
      // pick k initial cluster centers at random
      for (int i = 0; i < k + 1; i++) {
        Vector vec = tweakValue(points.get(i));
        SoftCluster cluster = new SoftCluster(vec);
        // add the center so the centroid will be correct upon output
        cluster.addPoint(cluster.getCenter(), 1);
       
        clusterList.add(cluster);
View Full Code Here

       * FileOutputStream("testdata/clusters/part-00000"), Charset .forName("UTF-8")));
       */
      SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf,
          new Path("testdata/clusters/part-00000"), Text.class, SoftCluster.class);
      for (int i = 0; i < k + 1; i++) {
        Vector vec = tweakValue(points.get(i).get());
       
        SoftCluster cluster = new SoftCluster(vec);
        // add the center so the centroid will be correct upon output
        cluster.addPoint(cluster.getCenter(), 1);
        /*
 
View Full Code Here

      System.out.println("testKFuzzyKMeansMRJob k= " + k);
      // pick k initial cluster centers at random
      List<SoftCluster> clusterList = new ArrayList<SoftCluster>();
     
      for (int i = 0; i < k + 1; i++) {
        Vector vec = tweakValue(points.get(i).get());
       
        SoftCluster cluster = new SoftCluster(vec, i);
        cluster.addPoint(cluster.getCenter(), 1);
        clusterList.add(cluster);
      }
     
      // run mapper
      FuzzyKMeansMapper mapper = new FuzzyKMeansMapper();
      mapper.config(clusterList);
     
      JobConf conf = new JobConf();
      conf.set(FuzzyKMeansConfigKeys.DISTANCE_MEASURE_KEY,
        "org.apache.mahout.common.distance.EuclideanDistanceMeasure");
      conf.set(FuzzyKMeansConfigKeys.CLUSTER_CONVERGENCE_KEY, "0.001");
      conf.set(FuzzyKMeansConfigKeys.M_KEY, "2");
      mapper.configure(conf);
     
      DummyOutputCollector<Text,FuzzyKMeansInfo> mapCollector = new DummyOutputCollector<Text,FuzzyKMeansInfo>();
      for (VectorWritable point : points) {
        mapper.map(new Text(), point, mapCollector, null);
      }
     
      // now verify mapper output
      assertEquals("Mapper Keys", k + 1, mapCollector.getData().size());
     
      Map<Vector,Double> pointTotalProbMap = new HashMap<Vector,Double>();
     
      for (String key : mapCollector.getKeys()) {
        // SoftCluster cluster = SoftCluster.decodeCluster(key);
        List<FuzzyKMeansInfo> values = mapCollector.getValue(key);
       
        for (FuzzyKMeansInfo value : values) {
         
          Double val = pointTotalProbMap.get(value.getVector());
          double probVal = 0.0;
          if (val != null) {
            probVal = val;
          }
         
          pointTotalProbMap.put(value.getVector(), probVal + value.getProbability());
        }
      }
     
      for (Map.Entry<Vector,Double> entry : pointTotalProbMap.entrySet()) {
        Vector key = entry.getKey();
        double value = round(entry.getValue(), 1);
       
        assertEquals("total Prob for Point:" + key, 1.0, value);
      }
    }
View Full Code Here

      System.out.println("testKFuzzyKMeansMRJob k= " + k);
      // pick k initial cluster centers at random
      List<SoftCluster> clusterList = new ArrayList<SoftCluster>();
     
      for (int i = 0; i < k + 1; i++) {
        Vector vec = tweakValue(points.get(i).get());
       
        SoftCluster cluster = new SoftCluster(vec, i);
        cluster.addPoint(cluster.getCenter(), 1);
        clusterList.add(cluster);
      }
View Full Code Here

      System.out.println("testKFuzzyKMeansMRJob k= " + k);
      // pick k initial cluster centers at random
      List<SoftCluster> clusterList = new ArrayList<SoftCluster>();
     
      for (int i = 0; i < k + 1; i++) {
        Vector vec = tweakValue(points.get(i).get());
       
        SoftCluster cluster = new SoftCluster(vec, i);
        // cluster.addPoint(cluster.getCenter(), 1);
        clusterList.add(cluster);
      }
     
      // run mapper
      FuzzyKMeansMapper mapper = new FuzzyKMeansMapper();
      mapper.config(clusterList);
     
      JobConf conf = new JobConf();
      conf.set(FuzzyKMeansConfigKeys.DISTANCE_MEASURE_KEY,
        "org.apache.mahout.common.distance.EuclideanDistanceMeasure");
      conf.set(FuzzyKMeansConfigKeys.CLUSTER_CONVERGENCE_KEY, "0.001");
      conf.set(FuzzyKMeansConfigKeys.M_KEY, "2");
      mapper.configure(conf);
     
      DummyOutputCollector<Text,FuzzyKMeansInfo> mapCollector = new DummyOutputCollector<Text,FuzzyKMeansInfo>();
      for (VectorWritable point : points) {
        mapper.map(new Text(), point, mapCollector, null);
      }
     
      // run combiner
      DummyOutputCollector<Text,FuzzyKMeansInfo> combinerCollector = new DummyOutputCollector<Text,FuzzyKMeansInfo>();
      FuzzyKMeansCombiner combiner = new FuzzyKMeansCombiner();
      combiner.configure(conf);
     
      for (String key : mapCollector.getKeys()) {
        List<FuzzyKMeansInfo> values = mapCollector.getValue(key);
        combiner.reduce(new Text(key), values.iterator(), combinerCollector, null);
      }
     
      // run reducer
      DummyOutputCollector<Text,SoftCluster> reducerCollector = new DummyOutputCollector<Text,SoftCluster>();
      FuzzyKMeansReducer reducer = new FuzzyKMeansReducer();
      reducer.config(clusterList);
      reducer.configure(conf);
     
      for (String key : combinerCollector.getKeys()) {
        List<FuzzyKMeansInfo> values = combinerCollector.getValue(key);
        reducer.reduce(new Text(key), values.iterator(), reducerCollector, new DummyReporter());
      }
     
      // now verify the reducer output
      assertEquals("Reducer Output", k + 1, combinerCollector.getData().size());
     
      // compute the reference result after one iteration and compare
      List<SoftCluster> reference = new ArrayList<SoftCluster>();
      for (int i = 0; i < k + 1; i++) {
        Vector vec = tweakValue(points.get(i).get());
        reference.add(new SoftCluster(vec, i));
      }
      List<Vector> pointsVectors = new ArrayList<Vector>();
      for(VectorWritable point : points)
        pointsVectors.add(point.get());
View Full Code Here

      System.out.println("testKFuzzyKMeansMRJob k= " + k);
      // pick k initial cluster centers at random
      List<SoftCluster> clusterList = new ArrayList<SoftCluster>();
     
      for (int i = 0; i < k + 1; i++) {
        Vector vec = tweakValue(points.get(i).get());
       
        SoftCluster cluster = new SoftCluster(vec, i);
        cluster.addPoint(cluster.getCenter(), 1);
        clusterList.add(cluster);
      }
     
      // run mapper
      FuzzyKMeansMapper mapper = new FuzzyKMeansMapper();
      mapper.config(clusterList);
     
      JobConf conf = new JobConf();
      conf.set(FuzzyKMeansConfigKeys.DISTANCE_MEASURE_KEY,
        "org.apache.mahout.common.distance.EuclideanDistanceMeasure");
      conf.set(FuzzyKMeansConfigKeys.CLUSTER_CONVERGENCE_KEY, "0.001");
      conf.set(FuzzyKMeansConfigKeys.M_KEY, "2");
      mapper.configure(conf);
     
      DummyOutputCollector<Text,FuzzyKMeansInfo> mapCollector = new DummyOutputCollector<Text,FuzzyKMeansInfo>();
      for (VectorWritable point : points) {
        mapper.map(new Text(), point, mapCollector, null);
      }
      for (SoftCluster softCluster : clusterList) {
        softCluster.recomputeCenter();
      }
      // run combiner
      DummyOutputCollector<Text,FuzzyKMeansInfo> combinerCollector = new DummyOutputCollector<Text,FuzzyKMeansInfo>();
      FuzzyKMeansCombiner combiner = new FuzzyKMeansCombiner();
      combiner.configure(conf);
     
      for (String key : mapCollector.getKeys()) {
       
        List<FuzzyKMeansInfo> values = mapCollector.getValue(key);
        combiner.reduce(new Text(key), values.iterator(), combinerCollector, null);
      }
     
      // run reducer
      DummyOutputCollector<Text,SoftCluster> reducerCollector = new DummyOutputCollector<Text,SoftCluster>();
      FuzzyKMeansReducer reducer = new FuzzyKMeansReducer();
      reducer.config(clusterList);
      reducer.configure(conf);
     
      for (String key : combinerCollector.getKeys()) {
        List<FuzzyKMeansInfo> values = combinerCollector.getValue(key);
        reducer.reduce(new Text(key), values.iterator(), reducerCollector, null);
      }
     
      // run clusterMapper
      List<SoftCluster> reducerCluster = new ArrayList<SoftCluster>();
     
      for (String key : reducerCollector.getKeys()) {
        List<SoftCluster> values = reducerCollector.getValue(key);
        reducerCluster.add(values.get(0));
      }
      for (SoftCluster softCluster : reducerCluster) {
        softCluster.recomputeCenter();
      }
     
      DummyOutputCollector<Text,FuzzyKMeansOutput> clusterMapperCollector = new DummyOutputCollector<Text,FuzzyKMeansOutput>();
     
      FuzzyKMeansClusterMapper clusterMapper = new FuzzyKMeansClusterMapper();
      clusterMapper.config(reducerCluster);
      clusterMapper.configure(conf);
     
      for (VectorWritable point : points) {
        clusterMapper.map(new Text(), point, clusterMapperCollector, null);
      }
     
      // now run for one iteration of referencefuzzykmeans and compare the
      // results
      // compute the reference result after one iteration and compare
      List<SoftCluster> reference = new ArrayList<SoftCluster>();
      for (int i = 0; i < k + 1; i++) {
        Vector vec = tweakValue(points.get(i).get());
        reference.add(new SoftCluster(vec, i));
      }
      Map<String,String> pointClusterInfo = new HashMap<String,String>();
      List<Vector> pointsVectors = new ArrayList<Vector>();
      for(VectorWritable point : points)
View Full Code Here

TOP

Related Classes of org.apache.mahout.math.Vector

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.