Example usage for org.apache.commons.math3.ml.clustering DoublePoint getPoint

List of usage examples for org.apache.commons.math3.ml.clustering DoublePoint getPoint

Introduction

In this page you can find the example usage for org.apache.commons.math3.ml.clustering DoublePoint getPoint.

Prototype

public double[] getPoint() 

Source Link

Usage

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];
        coords[1] += p.getPoint()[1];//from ww w .  j  a v  a  2  s  .  c om
    }

    coords[0] /= clPoints.size();
    coords[1] /= clPoints.size();

    return new DoublePoint(coords);
}

From source file:bigdataproject.KMeansKFinder.java

private double[] getCentroid(List cluster) {
    double size = (double) cluster.size();
    double x = 0.0, y = 0.0;
    for (Object p : cluster) {
        DoublePoint point = (DoublePoint) p;
        double[] pointDouble = point.getPoint();
        x += pointDouble[0];//w w  w. jav  a  2s. co m
        y += pointDouble[1];
    }
    double[] centroid = new double[2];
    centroid[0] = x / size;
    centroid[1] = y / size;
    return centroid;
}

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  ww.  j  ava2  s.  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();
            }//from  w  w w .j  a v a2  s  .c o m
            clusters.put(index++, clusterMatrix);
        }
    }
}

From source file:edu.nyu.vida.data_polygamy.ctdata.TopologicalIndex.java

public double getThreshold(Feature[] f) {

    KMeansPlusPlusClusterer<DoublePoint> kmeans = new KMeansPlusPlusClusterer<DoublePoint>(2, 1000);
    ArrayList<DoublePoint> pts = new ArrayList<DoublePoint>();

    if (f.length < 2) {
        return f[0].wt * 0.4;
    }/*ww w . j av a2  s  . co  m*/
    for (int i = 0; i < f.length; i++) {
        DoublePoint dpt = new DoublePoint(new double[] { f[i].wt });
        pts.add(dpt);
    }
    List<CentroidCluster<DoublePoint>> clusters = kmeans.cluster(pts);

    double maxp = 0;
    double minp = 0;
    int ct = 0;
    for (CentroidCluster<DoublePoint> c : clusters) {
        double mp = 0;
        double mnp = Double.MAX_VALUE;
        for (DoublePoint dpt : c.getPoints()) {
            double[] pt = dpt.getPoint();
            mp = Math.max(mp, pt[0]);
            mnp = Math.min(mnp, pt[0]);
        }
        if (mp > maxp) {
            maxp = mp;
            minp = mnp;
        }
        ct++;
    }
    if (ct > 2) {
        Utilities.er("Can there be > 2 clusters?");
    }
    return minp;
}

From source file:org.esa.s2tbx.s2msi.idepix.operators.cloudshadow.MyClustering.java

/**
 * Runs the K-means++ clustering algorithm.
 *
 * @param points the points to cluster//ww  w .  j a  v a 2  s.  com
 * @return a list of clusters containing the points
 * @throws MathIllegalArgumentException if the data points are null or the number
 *                                      of clusters is larger than the number of data points
 * @throws ConvergenceException         if an empty cluster is encountered and the
 *                                      {@link #emptyStrategy} is set to {@code ERROR}
 */
@Override
public List<CentroidCluster<T>> cluster(final Collection<T> points)
        throws MathIllegalArgumentException, ConvergenceException {

    // sanity checks
    MathUtils.checkNotNull(points);

    // number of clusters has to be smaller or equal the number of data points
    if (points.size() < k) {
        throw new NumberIsTooSmallException(points.size(), k, false);
    }
    // todo adaption for more bands is required
    double darkestPoint = Double.MAX_VALUE;
    int darkestPointIndex = -1;
    for (int index = 0; index < points.size(); index++) {
        DoublePoint p = ((ArrayList<DoublePoint>) points).get(index);
        double value = p.getPoint()[0];
        if (value < darkestPoint) {
            darkestPoint = value;
            darkestPointIndex = index;
        }
    }
    /*
    int array [][] = {{11, 0},{34, 1},{8, 2}};
            
     java.util.Arrays.sort(array, new java.util.Comparator<double[]>() {
     public int compare(double[] a, double[] b) {
         return b[0] - a[0];
     }
    });
            
    array [][] = {{8, 2}, {11, 0}, {34, 1}};
    */

    // create the initial clusters
    List<CentroidCluster<T>> clusters = chooseInitialCenters(points, darkestPointIndex);

    // create an array containing the latest assignment of a point to a cluster
    // no need to initialize the array, as it will be filled with the first assignment
    int[] assignments = new int[points.size()];
    assignPointsToClusters(clusters, points, assignments);

    // iterate through updating the centers until we're done
    final int max = (maxIterations < 0) ? Integer.MAX_VALUE : maxIterations;
    for (int count = 0; count < max; count++) {
        boolean emptyCluster = false;
        List<CentroidCluster<T>> newClusters = new ArrayList<>();
        for (final CentroidCluster<T> cluster : clusters) {
            final Clusterable newCenter;
            if (cluster.getPoints().isEmpty()) {
                switch (emptyStrategy) {
                case LARGEST_VARIANCE:
                    newCenter = getPointFromLargestVarianceCluster(clusters);
                    break;
                case LARGEST_POINTS_NUMBER:
                    newCenter = getPointFromLargestNumberCluster(clusters);
                    break;
                case FARTHEST_POINT:
                    newCenter = getFarthestPoint(clusters);
                    break;
                default:
                    throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS);
                }
                emptyCluster = true;
            } else {
                newCenter = centroidOf(cluster.getPoints(), cluster.getCenter().getPoint().length);
            }
            newClusters.add(new CentroidCluster<>(newCenter));
        }
        int changes = assignPointsToClusters(newClusters, points, assignments);
        clusters = newClusters;

        // if there were no more changes in the point-to-cluster assignment
        // and there are no empty clusters left, return the current clusters
        if (changes == 0 && !emptyCluster) {
            return clusters;
        }
    }
    return clusters;
}

From source file:uk.ac.diamond.scisoft.ncd.core.data.stats.ClusterOutlierRemoval.java

@Override
public Dataset getStatsData() {
    if (referenceData != null) {
        int[] shape = referenceData.getShape();
        IndexIterator itr = referenceData.getIterator(true);
        Map<DoublePoint, Integer> clusterInputMap = new HashMap<DoublePoint, Integer>();
        while (itr.hasNext()) {
            int[] pos = itr.getPos();
            int idx = AbstractDataset.getFlat1DIndex(shape, pos);
            double val = referenceData.getDouble(pos);
            clusterInputMap.put(new DoublePoint(new double[] { val }), idx);
        }//from   w w  w .j  a  va  2  s  .co m

        DBSCANClusterer<DoublePoint> clusterer = new DBSCANClusterer<DoublePoint>(dbSCANClustererEpsilon,
                dbSCANClustererMinPoints);
        List<Cluster<DoublePoint>> clusterResults = clusterer.cluster(clusterInputMap.keySet());
        // output the clusters
        List<Double> dev = new ArrayList<Double>(clusterResults.size());
        for (int i = 0; i < clusterResults.size(); i++) {
            System.out.println("Cluster " + i);
            List<DoublePoint> points = clusterResults.get(i).getPoints();
            int size = points.size();
            if (size == 1) {
                dev.add(Double.MAX_VALUE);
                continue;
            }
            double[] values = new double[size];
            for (int j = 0; j < values.length; j++) {
                DoublePoint point = points.get(j);
                values[j] = point.getPoint()[0];
            }
            dev.add(StatUtils.variance(values));
            System.out.println("Number of points : " + Integer.toString(size));
            System.out.println("Variance : " + Double.toString(dev.get(i)));
            System.out.println();
        }

        Dataset result = DatasetFactory.zeros(shape, Dataset.INT);
        //int selectIndex = dev.indexOf(Collections.min(dev));
        for (int selectIndex = 0; selectIndex < clusterResults.size(); selectIndex++) {
            Cluster<DoublePoint> selectCluster = clusterResults.get(selectIndex);
            for (DoublePoint point : selectCluster.getPoints()) {
                Integer idx = clusterInputMap.get(point);
                result.set(selectIndex, AbstractDataset.getNDPositionFromShape(idx, shape));
            }
        }
        return result;
    }
    return null;
}

From source file:VQVAD.VQVADModel.java

protected DoublePoint mergeValues(DoublePoint mine, DoublePoint other, double lambda) {
    final double[] myValues = mine.getPoint();
    final double[] otherValues = other.getPoint();
    final double[] mergedValues = new double[myValues.length];

    for (int i = 0; i < mergedValues.length; i++) {
        mergedValues[i] = lambda * otherValues[i] + (1 - lambda) * myValues[i];
    }/* w  w  w.ja  v  a  2  s .c  o m*/

    return new DoublePoint(mergedValues);
}

From source file:VQVAD.VQVADModel.java

public double minDistance(DoublePoint[] centers, double[] input) {
    EuclideanDistance d = new EuclideanDistance();
    double minDistance = Double.POSITIVE_INFINITY;

    for (DoublePoint c : centers) {
        double v = d.compute(c.getPoint(), input);

        v = v * v;// w  ww. j ava2s  .c  o  m

        if (v < minDistance) {
            minDistance = v;
        }
    }

    return minDistance;
}