org.apache.commons.math3.ml.clustering.Cluster Java Examples

The following examples show how to use org.apache.commons.math3.ml.clustering.Cluster. 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: MyTest2.java    From ACManager with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void test6() throws Exception {
    Clusterer<DoublePoint> clusterer = new KMeansPlusPlusClusterer<DoublePoint>(3);
    List<DoublePoint> list = new ArrayList<>();

    list.add(new DoublePoint(new double[]{1}));
    list.add(new DoublePoint(new double[]{1.5}));
    list.add(new DoublePoint(new double[]{1.8}));
    list.add(new DoublePoint(new double[]{3.5}));
    list.add(new DoublePoint(new double[]{3.6}));
    list.add(new DoublePoint(new double[]{4}));
    list.add(new DoublePoint(new double[]{4.2}));
    System.out.println(list);

    List<? extends Cluster<DoublePoint>> res = clusterer.cluster(list);
    System.out.println("!!!");
    System.out.println(res.size());
    for (Cluster<DoublePoint> re : res) {
        System.out.println(re.getPoints());
    }
}
 
Example #2
Source File: SumOfClusterVariances.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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 #3
Source File: ClusterLayer.java    From arcgis-runtime-demo-java with Apache License 2.0 6 votes vote down vote up
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 #4
Source File: ClusterEvaluator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 vote down vote up
private void add(List<Cluster<ClusterableLocation>> clusters) {
  int max = getMax(clusters);
  for (Cluster<ClusterableLocation> cluster : clusters) {
    Geometry geometry = null;
    Symbol symbol = null;
    Color color = cluster.getPoints().size() == max ? Color.RED : Color.GRAY;
    ClusterType thisClusterType = cluster.getPoints().size() < 2 ? ClusterType.POINT : clusterType;
    switch (thisClusterType) {
      default:
      case POINT:
        geometry = getCenter(cluster);
        symbol = createPointSymbol(color, cluster.getPoints().size());
        break;
      case BOUNDING_RECTANGLE:
        geometry = getBoundingRectangle(cluster);
        symbol = createPolygonSymbol(color, cluster.getPoints().size());
        break;
      case CONVEX_HULL:
        geometry = getConvexHull(cluster);
        symbol = createPolygonSymbol(color, cluster.getPoints().size());
        break;
    };
    addGraphic(new Graphic(geometry, symbol));
  }
}
 
Example #6
Source File: SumOfClusterVariances.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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 #7
Source File: ClusterEvaluator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 #8
Source File: ClusterLayer.java    From arcgis-runtime-demo-java with Apache License 2.0 5 votes vote down vote up
private int getMax(List<Cluster<ClusterableLocation>> clusters) {
  int max = Integer.MIN_VALUE;
  for (Cluster<ClusterableLocation> cluster : clusters) {
    if (cluster.getPoints().size() > max) {
      max = cluster.getPoints().size();
    }
  }
  return max;
}
 
Example #9
Source File: DBSCANClusterer.java    From egads with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Expands the cluster to include density-reachable items.
 *
 * @param cluster Cluster to expand
 * @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;
}
 
Example #10
Source File: ClusterLayer.java    From arcgis-runtime-demo-java with Apache License 2.0 5 votes vote down vote up
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 #11
Source File: ClusterLayer.java    From arcgis-runtime-demo-java with Apache License 2.0 5 votes vote down vote up
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 #12
Source File: DbscanEvaluator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public ClusterTuple(@SuppressWarnings({"rawtypes"})Map fields,
                    List<Cluster<ClusterPoint>> clusters,
                    List<String> columnLabels) {
  super(fields);
  this.clusters = clusters;
  this.columnLabels = columnLabels;
}
 
Example #13
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public ClusterPlot(final List<? extends Cluster<DoublePoint>> clusters, long duration) {
    this.clusters = clusters;
    this.duration = duration;
}
 
Example #14
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public ClusterPlot(final List<? extends Cluster<DoublePoint>> clusters, long duration) {
    this.clusters = clusters;
    this.duration = duration;
}
 
Example #15
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public ClusterPlot(final List<? extends Cluster<DoublePoint>> clusters, long duration) {
    this.clusters = clusters;
    this.duration = duration;
}
 
Example #16
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public ClusterPlot(final List<? extends Cluster<DoublePoint>> clusters, long duration) {
    this.clusters = clusters;
    this.duration = duration;
}
 
Example #17
Source File: Stats.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
@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();
	}
}
 
Example #18
Source File: DbscanEvaluator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public List<Cluster<ClusterPoint>> getClusters() {
  return this.clusters;
}
 
Example #19
Source File: ClusterEvaluator.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Computes the evaluation score for the given list of clusters.
 * @param clusters the clusters to evaluate
 * @return the computed score
 */
public abstract double score(List<? extends Cluster<T>> clusters);
 
Example #20
Source File: ClusterEvaluator.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Computes the evaluation score for the given list of clusters.
 * @param clusters the clusters to evaluate
 * @return the computed score
 */
public abstract double score(List<? extends Cluster<T>> clusters);