List of usage examples for com.google.common.math Stats meanOf
public static double meanOf(long... values)
From source file:edu.uci.ics.jung.algorithms.util.KMeansClusterer.java
/** * Returns a <code>Collection</code> of clusters, where each cluster is represented as a <code>Map * </code> of <code>Objects</code> to locations in d-dimensional space. * * @param object_locations a map of the items to cluster, to <code>double</code> arrays that * specify their locations in d-dimensional space. * @param num_clusters the number of clusters to create * @return a clustering of the input objects in d-dimensional space * @throws NotEnoughClustersException if {@code num_clusters} is larger than the number of * distinct points in object_locations *//*from w w w . j av a 2 s .c o m*/ @SuppressWarnings("unchecked") public Collection<Map<T, double[]>> cluster(Map<T, double[]> object_locations, int num_clusters) { Preconditions.checkNotNull(object_locations); Preconditions.checkArgument(!object_locations.isEmpty(), "'objects' must be non-empty"); Preconditions.checkArgument(num_clusters >= 2 && num_clusters <= object_locations.size(), "number of clusters must be >= 2 and <= number of objects"); Set<double[]> centroids = new HashSet<double[]>(); Object[] obj_array = object_locations.keySet().toArray(); Set<T> tried = new HashSet<T>(); // create the specified number of clusters while (centroids.size() < num_clusters && tried.size() < object_locations.size()) { T o = (T) obj_array[(int) (rand.nextDouble() * obj_array.length)]; tried.add(o); double[] mean_value = object_locations.get(o); boolean duplicate = false; for (double[] cur : centroids) { if (Arrays.equals(mean_value, cur)) { duplicate = true; } } if (!duplicate) { centroids.add(mean_value); } } if (tried.size() >= object_locations.size()) { throw new NotEnoughClustersException(); } // put items in their initial clusters Map<double[], Map<T, double[]>> clusterMap = assignToClusters(object_locations, centroids); // keep reconstituting clusters until either // (a) membership is stable, or // (b) number of iterations passes max_iterations, or // (c) max movement of any centroid is <= convergence_threshold int iterations = 0; double max_movement = Double.POSITIVE_INFINITY; while (iterations++ < max_iterations && max_movement > convergence_threshold) { max_movement = 0; Set<double[]> new_centroids = new HashSet<double[]>(); // calculate new mean for each cluster for (Map.Entry<double[], Map<T, double[]>> entry : clusterMap.entrySet()) { double[] centroid = entry.getKey(); Map<T, double[]> elements = entry.getValue(); ArrayList<double[]> locations = new ArrayList<double[]>(elements.values()); double[] means = new double[locations.size()]; int index = 0; for (double[] location : locations) { means[index++] = Stats.meanOf(location); } max_movement = Math.max(max_movement, Math.sqrt(squaredError(centroid, means))); new_centroids.add(means); } // TODO: check membership of clusters: have they changed? // regenerate cluster membership based on means clusterMap = assignToClusters(object_locations, new_centroids); } return clusterMap.values(); }
From source file:edu.uci.ics.jung.algorithms.cluster.VoltageClusterer.java
private static double[] meansOf(Collection<double[]> collectionOfDoubleArrays) { double[] result = new double[collectionOfDoubleArrays.size()]; int index = 0; for (double[] array : collectionOfDoubleArrays) { result[index++] = Stats.meanOf(array); }/*from w w w . ja v a 2 s.co m*/ return result; }