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

The following examples show how to use org.apache.commons.math3.ml.clustering.CentroidCluster. 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: GMeansTest.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Creates a cluster and checks if output is generated without exceptions.
 * 
 * @throws Exception if the test fails
 */
@Test
public void createClusters() throws Exception {
	Random rand = new Random(SEED);
	
	ArrayList<DoublePoint> data = new ArrayList<>(DATA_POINT_NUMBER);
	
	for (int i = 0; i < DATA_POINT_NUMBER; i++) {
		data.add(new DoublePoint( new int[] {rand.nextInt(500), rand.nextInt(500)}));
	}
	
	// create Cluster
	GMeans<DoublePoint> cluster = new GMeans<>(data);
	
	List<CentroidCluster<DoublePoint>> result = cluster.cluster();
	
	assertNotNull("GMeans created no result!", result);
	
	assertFalse("GMeans created no clusters!", result.size() == 0);
	
	for (CentroidCluster<DoublePoint> centroidCluster : result) {
		assertFalse("A Gmeans cluster is empty!", centroidCluster.getPoints().size() == 0);
	}
}
 
Example #2
Source File: GetCentroidsEvaluator.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Object doWork(Object value) throws IOException {
  if(!(value instanceof KmeansEvaluator.ClusterTuple)){
    throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting a clustering result",toExpression(constructingFactory), value.getClass().getSimpleName()));
  } else {
    KmeansEvaluator.ClusterTuple clusterTuple = (KmeansEvaluator.ClusterTuple)value;
    List<CentroidCluster<KmeansEvaluator.ClusterPoint>> clusters = clusterTuple.getClusters();
    double[][] data = new double[clusters.size()][];
    for(int i=0; i<clusters.size(); i++) {
      CentroidCluster<KmeansEvaluator.ClusterPoint> centroidCluster = clusters.get(i);
      Clusterable clusterable = centroidCluster.getCenter();
      data[i] = clusterable.getPoint();
    }
    Matrix centroids = new Matrix(data);
    centroids.setColumnLabels(clusterTuple.getColumnLabels());
    return centroids;
  }
}
 
Example #3
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 #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: ClusterSampling.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
public IAlgorithmEvent doAlgorithmStep() throws AlgorithmTimeoutedException, InterruptedException, AlgorithmExecutionCanceledException {
	if (this.currentCluster < this.clusterResults.size()) {
		CentroidCluster<I> cluster = this.clusterResults.get(this.currentCluster++);
		boolean same = true;
		int n = cluster.getPoints().size();
		for (int i = 1; i < n; i++) {
			if (i % 1000 == 0) {
				this.checkAndConductTermination();
			}
			if (!cluster.getPoints().get(i - 1).getLabel().equals(cluster.getPoints().get(i).getLabel())) {
				same = false;
				break;
			}
		}
		if (same) {
			I near = cluster.getPoints().get(0);
			double dist = Double.MAX_VALUE;
			for (I p : cluster.getPoints()) {
				double newDist = this.distanceMeassure.compute(p.getPoint(), cluster.getCenter().getPoint());
				if (newDist < dist) {
					near = p;
					dist = newDist;
				}
			}
			this.sample.add(near);
		} else {
			// find a solution to not sample all points here
			for (int i = 0; i < cluster.getPoints().size(); i++) {
				this.sample.add(cluster.getPoints().get(i));
			}
		}
		return new SampleElementAddedEvent(this);
	} else {
		return this.terminate();
	}

}
 
Example #6
Source File: GMeansTest.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates random datapoints and clusters. Then creates a UI to visualize the clusters. Not a Unit test for obvious reasons. 
 * 
 * @param args Nothing to see here
 */
public static void main(String[] args) {
	Random rand = new Random(SEED);
	
	// generate random points
	ArrayList<DoublePoint> data = new ArrayList<>(DATA_POINT_NUMBER);
	for (int i = 0; i < DATA_POINT_NUMBER; i++) {
		data.add(new DoublePoint( new int[] {rand.nextInt(500), rand.nextInt(500)}));
	}
	
	// create Cluster and results
	GMeans<DoublePoint> cluster = new GMeans<>(data);
	List<CentroidCluster<DoublePoint>> result = cluster.cluster();
	
	
	// create Window
	JFrame frame = new JFrame("Simple Result UI");
	
	@SuppressWarnings("serial")
	Canvas c = new Canvas() {
		@Override
		public void paint(Graphics g) {
			// paint points colored by cluster
			for (CentroidCluster<DoublePoint> centroidCluster : result) {
				g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)));
				for (DoublePoint point : centroidCluster.getPoints()) {
					g.fillOval((int)point.getPoint()[0]-2, (int)point.getPoint()[1]-2, 4, 4);
				}
			}
			
		}
	};
	c.setSize(500, 500);
	
	frame.getContentPane().add(c);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setSize(500, 500);
	frame.setVisible(true);
	
}
 
Example #7
Source File: KmeansEvaluator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public ClusterTuple(@SuppressWarnings({"rawtypes"})Map fields,
                    List<CentroidCluster<ClusterPoint>> clusters,
                    List<String> columnLabels) {
  super(fields);
  this.clusters = clusters;
  this.columnLabels = columnLabels;
}
 
Example #8
Source File: KmeansEvaluator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public ClusterTuple(@SuppressWarnings({"rawtypes"})Map fields,
                    List<CentroidCluster<ClusterPoint>> clusters,
                    List<String> columnLabels,
                    Matrix membershipMatrix) {
  super(fields);
  this.clusters = clusters;
  this.columnLabels = columnLabels;
  this.membershipMatrix = membershipMatrix;
}
 
Example #9
Source File: GetClusterEvaluator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Object doWork(Object value1, Object value2) throws IOException {
  if(!(value1 instanceof KmeansEvaluator.ClusterTuple)){
    throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for value, expecting a cluster result.",toExpression(constructingFactory), value1.getClass().getSimpleName()));
  } else {

    KmeansEvaluator.ClusterTuple clusterTuple = (KmeansEvaluator.ClusterTuple)value1;
    List<CentroidCluster<KmeansEvaluator.ClusterPoint>> clusters = clusterTuple.getClusters();

    Number index = (Number)value2;
    @SuppressWarnings({"rawtypes"})
    CentroidCluster cluster = clusters.get(index.intValue());
    @SuppressWarnings({"rawtypes"})
    List points = cluster.getPoints();
    List<String> rowLabels = new ArrayList<>();
    double[][] data = new double[points.size()][];

    for(int i=0; i<points.size(); i++) {
      KmeansEvaluator.ClusterPoint p = (KmeansEvaluator.ClusterPoint)points.get(i);
      data[i] = p.getPoint();
      rowLabels.add(p.getId());
    }

    Matrix matrix = new Matrix(data);
    matrix.setRowLabels(rowLabels);
    matrix.setColumnLabels(clusterTuple.getColumnLabels());
    return matrix;
  }
}
 
Example #10
Source File: ClusterSampling.java    From AILibs with GNU Affero General Public License v3.0 4 votes vote down vote up
public List<CentroidCluster<I>> getClusterResults() {
	return this.clusterResults;
}
 
Example #11
Source File: ClusterSampling.java    From AILibs with GNU Affero General Public License v3.0 4 votes vote down vote up
public void setClusterResults(final List<CentroidCluster<I>> clusterResults) {
	this.clusterResults = clusterResults;
}
 
Example #12
Source File: ClusterStratiAssigner.java    From AILibs with GNU Affero General Public License v3.0 4 votes vote down vote up
public List<CentroidCluster<Clusterable>> getClusters() {
	return this.clusters;
}
 
Example #13
Source File: ClusterStratiAssigner.java    From AILibs with GNU Affero General Public License v3.0 4 votes vote down vote up
protected void setClusters(final List<CentroidCluster<Clusterable>> clusters) {
	this.clusters = clusters;
}
 
Example #14
Source File: KmeansEvaluator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public List<CentroidCluster<ClusterPoint>> getClusters() {
  return this.clusters;
}
 
Example #15
Source File: FuzzyKmeansEvaluator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings({"unchecked"})
public Object doWork(Object value1, Object value2) throws IOException {


  Matrix matrix = null;
  int k = 0;


  if(value1 instanceof Matrix) {
    matrix = (Matrix)value1;
  } else {
    throw new IOException("The first parameter for fuzzyKmeans should be the observation matrix.");
  }

  if(value2 instanceof Number) {
    k = ((Number)value2).intValue();
  } else {
    throw new IOException("The second parameter for fuzzyKmeans should be k.");
  }

  @SuppressWarnings({"rawtypes"})
  FuzzyKMeansClusterer<KmeansEvaluator.ClusterPoint> kmeans = new FuzzyKMeansClusterer(k,
                                                                                       fuzziness,
                                                                                       maxIterations,
                                                                                       new EuclideanDistance());
  List<KmeansEvaluator.ClusterPoint> points = new ArrayList<>();
  double[][] data = matrix.getData();

  List<String> ids = matrix.getRowLabels();

  for(int i=0; i<data.length; i++) {
    double[] vec = data[i];
    points.add(new KmeansEvaluator.ClusterPoint(ids.get(i), vec));
  }

  @SuppressWarnings({"rawtypes"})
  Map fields = new HashMap();

  fields.put("k", k);
  fields.put("fuzziness", fuzziness);
  fields.put("distance", "euclidean");
  fields.put("maxIterations", maxIterations);

  List<CentroidCluster<KmeansEvaluator.ClusterPoint>> clusters = kmeans.cluster(points);
  RealMatrix realMatrix = kmeans.getMembershipMatrix();
  double[][] mmData = realMatrix.getData();
  Matrix mmMatrix = new Matrix(mmData);
  mmMatrix.setRowLabels(matrix.getRowLabels());
  List<String> clusterCols = new ArrayList<>();
  for(int i=0; i<clusters.size(); i++) {
    clusterCols.add("cluster"+ ZplotStream.pad(Integer.toString(i), clusters.size()));
  }
  mmMatrix.setRowLabels(clusterCols);
  return new KmeansEvaluator.ClusterTuple(fields, clusters, matrix.getColumnLabels(),mmMatrix);
}
 
Example #16
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();
	}
}