示例#1
0
  @Override
  public Clustering<E, C> reduce(
      Clustering[] clusts, Algorithm<E, C> alg, ColorGenerator cg, Props props) {
    int k = props.getInt(KMeans.K);

    Clustering<E, C> result = new ClusterList<>(k); // reducer - find consensus
    // vote about final result
    E curr;
    Iterator<E> it = clusts[0].instancesIterator();
    Cluster<E> cluster;
    int[][] mapping = findMapping(clusts, k, alg.getDistanceFunction());

    if (cg != null) {
      cg.reset();
    }

    int idx;
    while (it.hasNext()) {
      curr = it.next();
      int[] assign = new int[k];
      for (int i = 0; i < clusts.length; i++) {
        cluster = clusts[i].assignedCluster(curr);
        if (i > 0) {
          assign[map(mapping, i, cluster.getClusterId())]++;
        } else {
          assign[cluster.getClusterId()]++;
        }
      }
      idx = findMax(assign);
      // check if cluster already exists
      if (!result.hasAt(idx)) {
        result.createCluster(idx);
        if (cg != null) {
          result.get(idx).setColor(cg.next());
        }
      }
      // final cluster assignment
      result.get(idx).add(curr);
    }
    result.compact();

    return result;
  }
示例#2
0
 @Override
 public Clustering updateCutoff(double cutoff) {
   this.cutoff = cutoff;
   int[] assign = new int[dataset.size()];
   int estClusters = (int) Math.sqrt(dataset.size());
   colorGenerator.reset();
   num = 0; // human readable
   Clustering clusters = new ClusterList(estClusters);
   DendroNode root = treeData.getRoot();
   if (root != null) {
     checkCutoff(root, cutoff, clusters, assign);
     if (clusters.size() > 0) {
       mapping = assign;
     } else {
       LOG.info("failed to cutoff dendrogram, cut = {}", cutoff);
     }
   }
   // add input dataset to clustering lookup
   if (noise != null) {
     Cluster clust = new BaseCluster<>(noise.size());
     clust.setColor(colorGenerator.next());
     clust.setClusterId(num++);
     clust.setParent(getDataset());
     clust.setName("Noise");
     clust.setAttributes(getDataset().getAttributes());
     for (Instance ins : noise) {
       clust.add(ins);
       mapping[ins.getIndex()] = num - 1;
     }
     clusters.add(clust);
   }
   clusters.lookupAdd(dataset);
   if (dendroMapping != null) {
     clusters.lookupAdd(dendroMapping);
   }
   clusters.lookupAdd(this);
   return clusters;
 }