Java Code Examples for org.apache.commons.math3.ml.clustering.Cluster#getPoints()
The following examples show how to use
org.apache.commons.math3.ml.clustering.Cluster#getPoints() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SumOfClusterVariances.java From astor with GNU General Public License v2.0 | 6 votes |
@Override public double score(final List<? extends Cluster<T>> clusters) { double varianceSum = 0.0; for (final Cluster<T> cluster : clusters) { if (!cluster.getPoints().isEmpty()) { final Clusterable center = centroidOf(cluster); // compute the distance variance of the current cluster final Variance stat = new Variance(); for (final T point : cluster.getPoints()) { stat.increment(distance(point, center)); } varianceSum += stat.getResult(); } } return varianceSum; }
Example 2
Source File: ClusterEvaluator.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Computes the centroid for a cluster. * * @param cluster the cluster * @return the computed centroid for the cluster, * or {@code null} if the cluster does not contain any points */ protected Clusterable centroidOf(final Cluster<T> cluster) { final List<T> points = cluster.getPoints(); if (points.isEmpty()) { return null; } // in case the cluster is of type CentroidCluster, no need to compute the centroid if (cluster instanceof CentroidCluster) { return ((CentroidCluster<T>) cluster).getCenter(); } final int dimension = points.get(0).getPoint().length; final double[] centroid = new double[dimension]; for (final T p : points) { final double[] point = p.getPoint(); for (int i = 0; i < centroid.length; i++) { centroid[i] += point[i]; } } for (int i = 0; i < centroid.length; i++) { centroid[i] /= points.size(); } return new DoublePoint(centroid); }
Example 3
Source File: SumOfClusterVariances.java From astor with GNU General Public License v2.0 | 6 votes |
@Override public double score(final List<? extends Cluster<T>> clusters) { double varianceSum = 0.0; for (final Cluster<T> cluster : clusters) { if (!cluster.getPoints().isEmpty()) { final Clusterable center = centroidOf(cluster); // compute the distance variance of the current cluster final Variance stat = new Variance(); for (final T point : cluster.getPoints()) { stat.increment(distance(point, center)); } varianceSum += stat.getResult(); } } return varianceSum; }
Example 4
Source File: ClusterEvaluator.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Computes the centroid for a cluster. * * @param cluster the cluster * @return the computed centroid for the cluster, * or {@code null} if the cluster does not contain any points */ protected Clusterable centroidOf(final Cluster<T> cluster) { final List<T> points = cluster.getPoints(); if (points.isEmpty()) { return null; } // in case the cluster is of type CentroidCluster, no need to compute the centroid if (cluster instanceof CentroidCluster) { return ((CentroidCluster<T>) cluster).getCenter(); } final int dimension = points.get(0).getPoint().length; final double[] centroid = new double[dimension]; for (final T p : points) { final double[] point = p.getPoint(); for (int i = 0; i < centroid.length; i++) { centroid[i] += point[i]; } } for (int i = 0; i < centroid.length; i++) { centroid[i] /= points.size(); } return new DoublePoint(centroid); }
Example 5
Source File: ClusterLayer.java From arcgis-runtime-demo-java with Apache License 2.0 | 6 votes |
private Envelope getBoundingRectangle(Cluster<ClusterableLocation> cluster) { double xmin = cluster.getPoints().get(0).getPoint()[0]; double xmax = xmin; double ymin = cluster.getPoints().get(0).getPoint()[1]; double ymax = ymin; for (ClusterableLocation p : cluster.getPoints()) { if (p.getPoint()[0] < xmin) { xmin = p.getPoint()[0]; } if (p.getPoint()[0] > xmax) { xmax = p.getPoint()[0]; } if (p.getPoint()[1] < ymin) { ymin = p.getPoint()[1]; } if (p.getPoint()[1] > ymax) { ymax = p.getPoint()[1]; } } Envelope boundingRectangle = new Envelope(xmin, ymin, xmax, ymax); return boundingRectangle; }
Example 6
Source File: ClusterLayer.java From arcgis-runtime-demo-java with Apache License 2.0 | 5 votes |
private Point getCenter(Cluster<ClusterableLocation> cluster) { double sumx = 0; double sumy = 0; for (ClusterableLocation p : cluster.getPoints()) { sumx += p.getPoint()[0]; sumy += p.getPoint()[1]; } Point center = new Point(sumx / cluster.getPoints().size(), sumy/ cluster.getPoints().size()); return center; }
Example 7
Source File: ClusterLayer.java From arcgis-runtime-demo-java with Apache License 2.0 | 5 votes |
private Polygon getConvexHull(Cluster<ClusterableLocation> cluster) { List<ClusterableLocation> pointsList = cluster.getPoints(); ClusterableLocation[] pointsArray = pointsList.toArray(new ClusterableLocation[pointsList.size()]); ClusterableLocation[] pointsConvexHull = ConvexHullGenerator.generateHull(pointsArray); Polygon convexHull = new Polygon(); convexHull.startPath(pointsConvexHull[0].getPoint()[0], pointsConvexHull[0].getPoint()[1]); for (int i = 1; i < pointsConvexHull.length; i++) { ClusterableLocation p = pointsConvexHull[i]; convexHull.lineTo(p.getPoint()[0], p.getPoint()[1]); } convexHull.closePathWithLine(); return convexHull; }
Example 8
Source File: Stats.java From gama with GNU General Public License v3.0 | 4 votes |
@operator ( value = "kmeans", can_be_const = false, type = IType.LIST, category = { IOperatorCategory.STATISTICAL }, concept = { IConcept.STATISTIC, IConcept.CLUSTERING }) @doc ( value = "returns the list of clusters (list of instance indices) computed with the kmeans++ " + "algorithm from the first operand data according to the number of clusters to split" + " the data into (k) and the maximum number of iterations to run the algorithm for " + "(If negative, no maximum will be used) (maxIt). Usage: kmeans(data,k,maxit)", special_cases = "if the lengths of two vectors in the right-hand aren't equal, returns 0", examples = { @example ( value = "kmeans ([[2,4,5], [3,8,2], [1,1,3], [4,3,4]],2,10)", equals = "[[0,2,3],[1]]") }) public static IList<IList> KMeansPlusplusApache(final IScope scope, final IList data, final Integer k, final Integer maxIt) throws GamaRuntimeException { final MersenneTwister rand = new MersenneTwister(scope.getRandom().getSeed().longValue()); final List<DoublePoint> instances = new ArrayList<>(); for (int i = 0; i < data.size(); i++) { final IList d = (IList) data.get(i); final double point[] = new double[d.size()]; for (int j = 0; j < d.size(); j++) { point[j] = Cast.asFloat(scope, d.get(j)); } instances.add(new Instance(i, point)); } final KMeansPlusPlusClusterer<DoublePoint> kmeans = new KMeansPlusPlusClusterer<>(k, maxIt, new EuclideanDistance(), rand); final List<CentroidCluster<DoublePoint>> clusters = kmeans.cluster(instances); try (final Collector.AsList results = Collector.getList()) { for (final Cluster<DoublePoint> cl : clusters) { final IList clG = GamaListFactory.create(); for (final DoublePoint pt : cl.getPoints()) { clG.addValue(scope, ((Instance) pt).getId()); } results.add(clG); } return results.items(); } }