public Graph createGraph(Clustering[] clusts) { Clustering c = clusts[0]; // total number of items int n = c.instancesCount(); Graph graph = new AdjListGraph(); Object2LongOpenHashMap<Instance> mapping = new Object2LongOpenHashMap(); Instance a, b; Node na, nb; // cluster membership int ca, cb; int x = 0; Edge edge; // accumulate evidence for (Clustering clust : clusts) { System.out.println("reducing " + (x++)); for (int i = 1; i < n; i++) { a = clust.instance(i); na = fetchNode(graph, mapping, a); ca = clust.assignedCluster(a.getIndex()); for (int j = 0; j < i; j++) { b = clust.instance(j); nb = fetchNode(graph, mapping, b); // for each pair of instances check if placed in the same cluster cb = clust.assignedCluster(b.getIndex()); if (ca == cb) { edge = graph.getEdge(na, nb); // check if exists if (edge == null) { edge = graph.getFactory().newEdge(na, nb, 0, 0, false); graph.addEdge(edge); } // increase weight by 1 edge.setWeight(edge.getWeight() + 1.0); } } } } return graph; }
@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; }