Example usage for org.apache.commons.math3.ml.clustering Cluster addPoint

List of usage examples for org.apache.commons.math3.ml.clustering Cluster addPoint

Introduction

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

Prototype

public void addPoint(final T point) 

Source Link

Document

Add a point to this cluster.

Usage

From source file:com.yahoo.egads.utilities.DBSCANClusterer.java

/**
 * Expands the cluster to include density-reachable items.
 *
 * @param cluster Cluster to expand/*w w  w. ja  v a2 s  .  c  o  m*/
 * @param point Point to add to cluster
 * @param neighbors List of neighbors
 * @param points the data set
 * @param visited the set of already visited points
 * @return the expanded cluster
 */
private Cluster<T> expandCluster(final Cluster<T> cluster, final T point, final List<T> neighbors,
        final Collection<T> points, final Map<Clusterable, PointStatus> visited) {
    cluster.addPoint(point);
    visited.put(point, PointStatus.PART_OF_CLUSTER);

    List<T> seeds = new ArrayList<T>(neighbors);
    int index = 0;
    while (index < seeds.size()) {
        final T current = seeds.get(index);
        PointStatus pStatus = visited.get(current);
        // only check non-visited points
        if (pStatus == null) {
            final List<T> currentNeighbors = getNeighbors(current, points);
            if (currentNeighbors.size() >= minPts) {
                seeds = merge(seeds, currentNeighbors);
            }
        }

        if (pStatus != PointStatus.PART_OF_CLUSTER) {
            visited.put(current, PointStatus.PART_OF_CLUSTER);
            cluster.addPoint(current);
        }

        index++;
    }
    return cluster;
}

From source file:com.yahoo.egads.utilities.DBSCANClusterer.java

/**
 * Performs DBSCAN cluster analysis.//from  w  w  w. ja v  a  2s  .  co  m
 *
 * @param points the points to cluster
 * @return the list of clusters
 * @throws NullArgumentException if the data points are null
 */
public List<Cluster<T>> cluster(final Collection<T> points) throws NullArgumentException {

    // sanity checks
    MathUtils.checkNotNull(points);

    final List<Cluster<T>> clusters = new ArrayList<Cluster<T>>();
    final List<Cluster<T>> anomalousClusters = new ArrayList<Cluster<T>>();
    final Cluster<T> anomalyCluster = new Cluster<T>();
    final Map<Clusterable, PointStatus> visited = new HashMap<Clusterable, PointStatus>();

    for (final T point : points) {
        if (visited.get(point) != null) {
            continue;
        }
        final List<T> neighbors = getNeighbors(point, points);
        if (neighbors.size() >= minPts) {
            // DBSCAN does not care about center points
            final Cluster<T> cluster = new Cluster<T>();
            clusters.add(expandCluster(cluster, point, neighbors, points, visited));
        } else {
            visited.put(point, PointStatus.NOISE);
            anomalyCluster.addPoint(point);
        }
    }
    anomalousClusters.add(anomalyCluster);

    return anomalousClusters;
}