List of usage examples for org.apache.commons.math3.ml.clustering Cluster getPoints
public List<T> getPoints()
From source file:Data.Utilities.java
public static DoublePoint getCentroid(Cluster<DoublePoint> dp) { double[] coords = new double[2]; List<DoublePoint> clPoints = dp.getPoints(); for (DoublePoint p : clPoints) { coords[0] += p.getPoint()[0];// w w w . j a v a2s .co m coords[1] += p.getPoint()[1]; } coords[0] /= clPoints.size(); coords[1] /= clPoints.size(); return new DoublePoint(coords); }
From source file:DBClust.java
public static void Ident(double[] eps, int[] minPts, int[] pixelSize, boolean[] doCluster) { ArrayList<Particle> InpParticle = TableIO.Load(); // Get current table data. ij.measure.ResultsTable tab = Analyzer.getResultsTable(); double width = tab.getValue("width", 0); double height = tab.getValue("height", 0); tab.reset();//from ww w.j a v a 2 s . c o m // tab.incrementCounter(); for (int Ch = 1; Ch <= InpParticle.get(InpParticle.size() - 1).channel; Ch++) { if (doCluster[Ch - 1]) { List<DoublePoint> points = new ArrayList<DoublePoint>(); for (int i = 0; i < InpParticle.size(); i++) { double[] p = new double[2]; if (InpParticle.get(i).include == 1 && InpParticle.get(i).channel == Ch) { p[0] = InpParticle.get(i).x; p[1] = InpParticle.get(i).y; points.add(new DoublePoint(p)); } } DBSCANClusterer<DoublePoint> DB = new DBSCANClusterer<DoublePoint>(eps[Ch - 1], minPts[Ch - 1]); List<Cluster<DoublePoint>> cluster = DB.cluster(points); int ClustIdx = 1; int[] IndexList = new int[InpParticle.size()]; for (Cluster<DoublePoint> c : cluster) { for (int j = 0; j < c.getPoints().size(); j++) { DoublePoint p = c.getPoints().get(j); double[] Coord = p.getPoint(); for (int i = 0; i < InpParticle.size(); i++) { Particle tempParticle = InpParticle.get(i); if (tempParticle.x == Coord[0] && tempParticle.y == Coord[1]) { IndexList[i] = ClustIdx; } } } ClustIdx++; } boolean first = true; for (int i = 0; i < InpParticle.size(); i++) { if (InpParticle.get(i).channel == Ch) { tab.incrementCounter(); tab.addValue("Cluster", IndexList[i]); tab.addValue("x0", InpParticle.get(i).x); tab.addValue("y0", InpParticle.get(i).y); tab.addValue("z0", InpParticle.get(i).z); tab.addValue("frame", InpParticle.get(i).frame); tab.addValue("channel", InpParticle.get(i).channel); tab.addValue("sigma_x", InpParticle.get(i).sigma_x); tab.addValue("sigma_y", InpParticle.get(i).sigma_y); tab.addValue("precision_x", InpParticle.get(i).precision_x); tab.addValue("precision_y", InpParticle.get(i).precision_y); tab.addValue("precision_z", InpParticle.get(i).precision_z); tab.addValue("r_square", InpParticle.get(i).r_square); tab.addValue("photons", InpParticle.get(i).photons); if (IndexList[i] > 0) tab.addValue("include", 1); else tab.addValue("include", 0); if (first) { first = false; tab.addValue("width", width); tab.addValue("height", height); } } } } } // Channel loop. tab.show("Results"); RenderIm.run(doCluster, pixelSize, false); }
From source file:bigdataproject.KMeansKFinder.java
public int find(double epsilon) { double oldAvDist = 0.0; for (int k = 2; k < numSamples; k++) { KMeansPlusPlusClusterer kmeans = new KMeansPlusPlusClusterer(k, 1000, new EuclideanDistance()); List<Cluster<DoublePoint>> clusterList = kmeans.cluster(list); double[] avDistances = new double[k]; int index = 0; for (Cluster<DoublePoint> c : clusterList) { List cluster = c.getPoints(); int size = cluster.size(); double[] centroid = getCentroid(cluster); double distanceSum = 0.0; for (Object p : cluster) { DoublePoint point = (DoublePoint) p; double[] pointDouble = point.getPoint(); EuclideanDistance dist = new EuclideanDistance(); distanceSum += dist.compute(centroid, pointDouble); }/* w w w . j av a2s .c o m*/ avDistances[index] = distanceSum / size; index++; } double avDistSum = 0.0; for (int i = 0; i < avDistances.length; i++) { avDistSum += avDistances[i]; } double newAvDist = avDistSum / avDistances.length; double difference = Math.abs(newAvDist - oldAvDist); if (difference >= epsilon) { oldAvDist = newAvDist; } else return k - 1; } return 0; }
From source file:bigdataproject.ScatterPlot.java
private void listToHashMap() { if (list != null) { int index = 0; clusters = new HashMap<>(); for (Cluster<DoublePoint> c : list) { List cluster = c.getPoints(); int size = cluster.size(); double[][] clusterMatrix = new double[size][2]; int i = 0; for (Object p : cluster) { DoublePoint point = (DoublePoint) p; clusterMatrix[i++] = point.getPoint(); }/* w ww. ja v a 2s. c om*/ clusters.put(index++, clusterMatrix); } } }
From source file:msi.gaml.operators.Stats.java
@operator(value = "dbscan", 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 dbscan (density-based spatial clustering of applications with noise) algorithm from the first operand data according to the maximum radius of the neighborhood to be considered (eps) and the minimum number of points needed for a cluster (minPts). Usage: dbscan(data,eps,minPoints)", special_cases = "if the lengths of two vectors in the right-hand aren't equal, returns 0", examples = { @example(value = "dbscan ([[2,4,5], [3,8,2], [1,1,3], [4,3,4]],10,2)", equals = "[]") }) public static IList<GamaList> DBscanApache(final IScope scope, final GamaList data, final Double eps, final Integer minPts) throws GamaRuntimeException { final DBSCANClusterer<DoublePoint> dbscan = new DBSCANClusterer(eps, minPts); final List<DoublePoint> instances = new ArrayList<>(); for (int i = 0; i < data.size(); i++) { final GamaList d = (GamaList) 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)); }// w w w. jav a 2 s. c o m instances.add(new Instance(i, point)); } final List<Cluster<DoublePoint>> clusters = dbscan.cluster(instances); final GamaList results = (GamaList) GamaListFactory.create(); for (final Cluster<DoublePoint> cl : clusters) { final GamaList clG = (GamaList) GamaListFactory.create(); for (final DoublePoint pt : cl.getPoints()) { clG.addValue(scope, ((Instance) pt).getId()); } results.addValue(scope, clG); } return results; }
From source file:msi.gaml.operators.Stats.java
@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)", isExecutable = false) }) public static GamaList<GamaList> KMeansPlusplusApache(final IScope scope, final GamaList 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 GamaList d = (GamaList) 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)); }/* w w w . j av a2 s.co m*/ 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); final GamaList results = (GamaList) GamaListFactory.create(); for (final Cluster<DoublePoint> cl : clusters) { final GamaList clG = (GamaList) GamaListFactory.create(); for (final DoublePoint pt : cl.getPoints()) { clG.addValue(scope, ((Instance) pt).getId()); } results.addValue(scope, clG); } return results; }
From source file:com.yahoo.egads.models.adm.DBScanModel.java
@Override public IntervalSequence detect(DataSequence observedSeries, DataSequence expectedSeries) throws Exception { IntervalSequence output = new IntervalSequence(); int n = observedSeries.size(); long unixTime = System.currentTimeMillis() / 1000L; // Get an array of thresholds. Float[] thresholdErrors = new Float[aes.getErrorToIndex().size()]; for (Map.Entry<String, Float> entry : this.threshold.entrySet()) { thresholdErrors[aes.getErrorToIndex().get(entry.getKey())] = Math.abs(entry.getValue()); }/*from ww w. j a v a 2 s .co m*/ // Compute the time-series of errors. HashMap<String, ArrayList<Float>> allErrors = aes.initAnomalyErrors(observedSeries, expectedSeries); List<IdentifiedDoublePoint> points = new ArrayList<IdentifiedDoublePoint>(); for (int i = 0; i < n; i++) { double[] d = new double[(aes.getIndexToError().keySet()).size()]; for (int e = 0; e < (aes.getIndexToError().keySet()).size(); e++) { d[e] = allErrors.get(aes.getIndexToError().get(e)).get(i); } points.add(new IdentifiedDoublePoint(d, i)); } List<Cluster<IdentifiedDoublePoint>> cluster = dbscan.cluster(points); for (Cluster<IdentifiedDoublePoint> c : cluster) { for (IdentifiedDoublePoint p : c.getPoints()) { int i = p.getId(); Float[] errors = aes.computeErrorMetrics(expectedSeries.get(p.getId()).value, observedSeries.get(p.getId()).value); logger.debug("TS:" + observedSeries.get(i).time + ",E:" + arrayF2S(errors) + ",TE:" + arrayF2S(thresholdErrors) + ",OV:" + observedSeries.get(i).value + ",EV:" + expectedSeries.get(i).value); if (observedSeries.get(p.getId()).value != expectedSeries.get(p.getId()).value && ((((unixTime - observedSeries.get(p.getId()).time) / 3600) < maxHrsAgo) || (maxHrsAgo == 0 && p.getId() == (n - 1)))) { output.add(new Interval(observedSeries.get(p.getId()).time, p.getId(), errors, thresholdErrors, observedSeries.get(p.getId()).value, expectedSeries.get(p.getId()).value)); } } } return output; }
From source file:com.graphhopper.jsprit.core.algorithm.ruin.DBSCANClusterer.java
private List<Job> getJobList(Cluster<LocationWrapper> c) { List<Job> l_ = new ArrayList<Job>(); if (c == null) return l_; for (LocationWrapper lw : c.getPoints()) { l_.add(lw.getJob());/*from www .ja v a2 s . c om*/ } return l_; }
From source file:Clustering.technique.KMeansPlusPlusClusterer.java
/** * Get a random point from the {@link Cluster} with the largest number of points * * @param clusters the {@link Cluster}s to search * @return a random point from the selected cluster * @throws ConvergenceException if clusters are all empty *//*from w w w. j av a 2 s. co m*/ private T getPointFromLargestNumberCluster(final Collection<? extends Cluster<T>> clusters) throws ConvergenceException { int maxNumber = 0; Cluster<T> selected = null; for (final Cluster<T> cluster : clusters) { // get the number of points of the current cluster final int number = cluster.getPoints().size(); // select the cluster with the largest number of points if (number > maxNumber) { maxNumber = number; selected = cluster; } } // did we find at least one non-empty cluster ? if (selected == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } // extract a random point from the cluster final List<T> selectedPoints = selected.getPoints(); return selectedPoints.remove(random.nextInt(selectedPoints.size())); }
From source file:Clustering.technique.KMeansPlusPlusClusterer.java
/** * Get the point farthest to its cluster center * * @param clusters the {@link Cluster}s to search * @return point farthest to its cluster center * @throws ConvergenceException if clusters are all empty *//* w w w. j a va2 s . c o m*/ private T getFarthestPoint(final Collection<CentroidCluster<T>> clusters) throws ConvergenceException { double maxDistance = Double.NEGATIVE_INFINITY; Cluster<T> selectedCluster = null; int selectedPoint = -1; for (final CentroidCluster<T> cluster : clusters) { // get the farthest point final Clusterable center = cluster.getCenter(); final List<T> points = cluster.getPoints(); for (int i = 0; i < points.size(); ++i) { final double distance = distance(points.get(i), center); if (distance > maxDistance) { maxDistance = distance; selectedCluster = cluster; selectedPoint = i; } } } // did we find at least one non-empty cluster ? if (selectedCluster == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } return selectedCluster.getPoints().remove(selectedPoint); }