@Override public DistanceResult call() { double bestDist = Double.MAX_VALUE; Instance bestMatch = null; for (Instance c : clusters) { double d = distance(inst, c); if (d < bestDist) { bestDist = d; bestMatch = c; } } return new DistanceResult(inst, (Cluster)bestMatch, bestDist); } });
/*** * Public method for creating a new cluster instance. The new cluster is associated with * the centroids for each feature. * * @return the new cluster instance */ public Cluster createCluster() { return (new ClusterFactory(this.typeDefs, onlineUpdate)).create(); } }
public static double cohesion(Clusterer clusterer, Cluster cluster) { double norm = cluster.size(); double cohesion = 0; for (Instance inst : cluster.getMembers()) { if (inst instanceof Cluster) { cohesion += cohesion(clusterer, (Cluster)inst); } else { cohesion += clusterer.distance(cluster, inst); } } return cohesion / norm; }
private double error(ClusterResult clusters) { double error = 0; for (Cluster c : clusters) { for (Instance i : c.getMembers()) { error += this.distance(c, i); } } return error; } private boolean isConverged(double oldError, double newError) {
public int instanceCount(Collection<? extends Instance> group) { int count = 0; for (Instance i : group) { if (i instanceof Cluster) { count += instanceCount(((Cluster)i).getMembers()); } else { count++; } } return count; }
@Override public Instance call(Instance inst1, Instance inst2) throws Exception { Cluster c = clusterFactory.create(); // aggregate inst1 and inst2 in a cluster updateCluster(inst1, c); updateCluster(inst2, c); return c; }
@Override public ClusterResult doIncrementalCluster(DataSet ds, List<Cluster> clusters) { return doCluster(ds, clusters); }
public boolean add(Instance inst) { boolean isNew = members.add(inst); updateCentroids(inst, false); return isNew; }
public void run(){ terminate(); } });
@Override public String toString() { return toString(false); }
public static double separation(Clusterer clusterer, Cluster c1, Cluster c2) { return clusterer.distance(c1, c2); }
/*** * Method to register a feature type, centroid class and the distance function associated with the feature type. * * @param name is an internal name for the centroid value in each cluster * @param centroidClass is the class that represents the central values of the instance features associated with this feature group * @param distFunc is the distance function to be used for calculating distance for this feature type */ @SuppressWarnings("rawtypes") public void registerFeatureType(String name, Class<? extends Centroid> centroidClass, DistanceFunction distFunc) { typeDefs.put(name, new FeatureTypeDefinition(name, centroidClass, distFunc)); }
private boolean contains(Instance inst, Cluster c) { for (Instance i : c.getMembers()) { if (i instanceof Cluster) { if ( contains(inst, (Cluster)i) ) return true; } else if (i == inst) return true; } return false; }
@Override public ClusterResult doCluster(DataSet ds) { return doCluster(ds, new LinkedList<Cluster>()); }
public static double validate(Clusterer clusterer, ClusterResult clusters) { double cohesion = 0; double norm = clusters.size(); for (Cluster c : clusters) { cohesion += cohesion(clusterer, c); } return cohesion / norm; } }
public boolean remove(Instance inst) { boolean isAltered = members.remove(inst); updateCentroids(inst, false); return isAltered; }
public int instanceCount(Collection<? extends Instance> group) { int count = 0; for (Instance i : group) { if (i instanceof Cluster) { count += instanceCount(((Cluster)i).getMembers()); } else { count++; } } return count; }