private static Cluster createSampleCluster() { double[][] distances = new double[][] { { 0, 1, 9, 7, 11, 14 }, { 1, 0, 4, 3, 8, 10 }, { 9, 4, 0, 9, 2, 8 }, { 7, 3, 9, 0, 6, 13 }, { 11, 8, 2, 6, 0, 10 }, { 14, 10, 8, 13, 10, 0 } }; String[] names = new String[] { "O1", "O2", "O3", "O4", "O5", "O6" }; ClusteringAlgorithm alg = new DefaultClusteringAlgorithm(); Cluster cluster = alg.performClustering(distances, names, new AverageLinkageStrategy()); cluster.toConsole(0); return cluster; }
/** * Compute some kind of unique ID for a given cluster pair. * @return The ID */ String hashCodePair(ClusterPair link) { return hashCodePair(link.getlCluster(), link.getrCluster()); }
@Override public List<Cluster> performFlatClustering(double[][] distances, String[] clusterNames, LinkageStrategy linkageStrategy, Double threshold) { checkArguments(distances, clusterNames, linkageStrategy); /* Setup model */ List<Cluster> clusters = createClusters(clusterNames); DistanceMap linkages = createLinkages(distances, clusters); /* Process */ HierarchyBuilder builder = new HierarchyBuilder(clusters, linkages); return builder.flatAgg(linkageStrategy, threshold); }
public int countLeafs(Cluster node, int count) { if (node.isLeaf()) count++; for (Cluster child : node.getChildren()) { count += child.countLeafs(); } return count; }
public double getTotalDistance() { Double dist = getDistance() == null ? 0 : getDistance().getDistance(); if (getChildren().size() > 0) { dist += children.get(0).getTotalDistance(); } return dist; }
/** * @return a new ClusterPair with the two left/right inverted */ public ClusterPair reverse() { return new ClusterPair(getrCluster(), getlCluster(), getLinkageDistance()); }
String hashCodePair(Cluster lCluster, Cluster rCluster) { return hashCodePairNames(lCluster.getName(), rCluster.getName()); }
@Override public Distance calculateDistance(Collection<Distance> distances) { double sum = 0; double weightTotal = 0; for (Distance distance : distances) { weightTotal += distance.getWeight(); sum += distance.getDistance() * distance.getWeight(); } return new Distance(sum / weightTotal, weightTotal); } }
@Override public Cluster performWeightedClustering(double[][] distances, String[] clusterNames, double[] weights, LinkageStrategy linkageStrategy) { return performClustering(distances, clusterNames, linkageStrategy); }
private static Cluster createSampleCluster() { double[][] distances = new double[][] { { 0, 1, 9, 7, 11, 14 }, { 1, 0, 4, 3, 8, 10 }, { 9, 4, 0, 9, 2, 8 }, { 7, 3, 9, 0, 6, 13 }, { 11, 8, 2, 6, 0, 10 }, { 14, 10, 8, 13, 10, 0 } }; String[] names = new String[] { "O1", "O2", "O3", "O4", "O5", "O6" }; ClusteringAlgorithm alg = new DefaultClusteringAlgorithm(); Cluster cluster = alg.performClustering(distances, names, new AverageLinkageStrategy()); cluster.toConsole(0); return cluster; }
public int countLeafs(Cluster node, int count) { if (node.isLeaf()) count++; for (Cluster child : node.getChildren()) { count += child.countLeafs(); } return count; }
public double getTotalDistance() { Double dist = getDistance() == null ? 0 : getDistance().getDistance(); if (getChildren().size() > 0) { dist += children.get(0).getTotalDistance(); } return dist; }
@Override public List<Cluster> performFlatClustering(double[][] distances, String[] clusterNames, LinkageStrategy linkageStrategy, Double threshold) { checkArguments(distances, clusterNames, linkageStrategy); /* Setup model */ List<Cluster> clusters = createClusters(clusterNames); DistanceMap linkages = createLinkages(distances, clusters); /* Process */ HierarchyBuilder builder = new HierarchyBuilder(clusters, linkages); return builder.flatAgg(linkageStrategy, threshold); }
/** * @return a new ClusterPair with the two left/right inverted */ public ClusterPair reverse() { return new ClusterPair(getrCluster(), getlCluster(), getLinkageDistance()); }
/** * Compute some kind of unique ID for a given cluster pair. * @return The ID */ String hashCodePair(ClusterPair link) { return hashCodePair(link.getlCluster(), link.getrCluster()); }
String hashCodePair(Cluster lCluster, Cluster rCluster) { return hashCodePairNames(lCluster.getName(), rCluster.getName()); }
@Override public Distance calculateDistance(Collection<Distance> distances) { double sum = 0; double weightTotal = 0; for (Distance distance : distances) { weightTotal += distance.getWeight(); sum += distance.getDistance() * distance.getWeight(); } return new Distance(sum / weightTotal, weightTotal); } }
@Override public Cluster performWeightedClustering(double[][] distances, String[] clusterNames, double[] weights, LinkageStrategy linkageStrategy) { return performClustering(distances, clusterNames, linkageStrategy); }
private static Cluster createSampleCluster() { double[][] distances = new double[][] { { 0, 1, 9, 7, 11, 14 }, { 1, 0, 4, 3, 8, 10 }, { 9, 4, 0, 9, 2, 8 }, { 7, 3, 9, 0, 6, 13 }, { 11, 8, 2, 6, 0, 10 }, { 14, 10, 8, 13, 10, 0 } }; String[] names = new String[] { "O1", "O2", "O3", "O4", "O5", "O6" }; ClusteringAlgorithm alg = new DefaultClusteringAlgorithm(); Cluster cluster = alg.performClustering(distances, names, new AverageLinkageStrategy()); cluster.toConsole(0); return cluster; }
public int countLeafs(Cluster<T> node, int count) { if (node.isLeaf()) count++; for (Cluster child : node.getChildren()) { count += child.countLeafs(); } return count; }