Example usage for org.apache.commons.math.exception.util LocalizedFormats EMPTY_CLUSTER_IN_K_MEANS

List of usage examples for org.apache.commons.math.exception.util LocalizedFormats EMPTY_CLUSTER_IN_K_MEANS

Introduction

In this page you can find the example usage for org.apache.commons.math.exception.util LocalizedFormats EMPTY_CLUSTER_IN_K_MEANS.

Prototype

LocalizedFormats EMPTY_CLUSTER_IN_K_MEANS

To view the source code for org.apache.commons.math.exception.util LocalizedFormats EMPTY_CLUSTER_IN_K_MEANS.

Click Source Link

Usage

From source file:org.basketball.MyKMeansPlusPlusClusterer.java

/**
 * Runs the K-means++ clustering algorithm.
 *
 * @param points the points to cluster//from   w ww . java  2s.  c  om
 * @param k the number of clusters to split the data into
 * @param maxIterations the maximum number of iterations to run the algorithm
 *     for.  If negative, no maximum will be used
 * @return a list of clusters containing the points
 */
public List<Cluster<T>> cluster(final Collection<T> points, final int k, final int maxIterations) {
    // create the initial clusters
    List<Cluster<T>> clusters = chooseInitialCenters(points, k, random);
    assignPointsToClusters(clusters, points);

    // 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 clusteringChanged = false;
        List<Cluster<T>> newClusters = new ArrayList<Cluster<T>>();
        for (final Cluster<T> cluster : clusters) {
            final T 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;
                case IGNORE:
                    continue;
                default:
                    throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS);
                }
                clusteringChanged = true;
            } else {
                newCenter = cluster.getCenter().centroidOf(cluster.getPoints());
                if (!newCenter.equals(cluster.getCenter())) {
                    clusteringChanged = true;
                }
            }
            newClusters.add(new Cluster<T>(newCenter));
        }
        if (!clusteringChanged) {
            return clusters;
        }
        assignPointsToClusters(newClusters, points);
        clusters = newClusters;
    }
    return clusters;
}

From source file:org.basketball.MyKMeansPlusPlusClusterer.java

/**
 * Get a random point from the {@link Cluster} with the largest distance variance.
 *
 * @param clusters the {@link Cluster}s to search
 * @return a random point from the selected cluster
 *//* ww  w  .ja v a 2s. c  o m*/
private T getPointFromLargestVarianceCluster(final Collection<Cluster<T>> clusters) {

    double maxVariance = Double.NEGATIVE_INFINITY;
    Cluster<T> selected = null;
    for (final Cluster<T> cluster : clusters) {
        if (!cluster.getPoints().isEmpty()) {

            // compute the distance variance of the current cluster
            final T center = cluster.getCenter();
            final Variance stat = new Variance();
            for (final T point : cluster.getPoints()) {
                stat.increment(point.distanceFrom(center));
            }
            final double variance = stat.getResult();

            // select the cluster with the largest variance
            if (variance > maxVariance) {
                maxVariance = variance;
                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:org.basketball.MyKMeansPlusPlusClusterer.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
 *///from  w w  w  . j a v a 2  s.  co  m
private T getPointFromLargestNumberCluster(final Collection<Cluster<T>> clusters) {

    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:org.basketball.MyKMeansPlusPlusClusterer.java

/**
 * Get the point farthest to its cluster center
 *
 * @param clusters the {@link Cluster}s to search
 * @return point farthest to its cluster center
 *///from www . ja v  a2  s.c  o  m
private T getFarthestPoint(final Collection<Cluster<T>> clusters) {

    double maxDistance = Double.NEGATIVE_INFINITY;
    Cluster<T> selectedCluster = null;
    int selectedPoint = -1;
    for (final Cluster<T> cluster : clusters) {

        // get the farthest point
        final T center = cluster.getCenter();
        final List<T> points = cluster.getPoints();
        for (int i = 0; i < points.size(); ++i) {
            final double distance = points.get(i).distanceFrom(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);

}