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

The following examples show how to use org.apache.commons.math3.ml.clustering.DoublePoint. 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: 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 #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: KMeansPlusPlus.java    From Java-Data-Analysis with MIT License 5 votes vote down vote up
private static List<DoublePoint> load(double[][] data) {
    List<DoublePoint> points = new ArrayList(M);
    for (double[] pair : data) {
        points.add(new DoublePoint(pair));            
    }
    return points;
}
 
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: ModifiedISACgMeans.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * inilizes toClusterPoints with the points that are to Cluster and are
 * normalized metafeatures
 *
 * @param toClusterPoints
 * @param instances
 */
public ModifiedISACgMeans(final List<double[]> toClusterPoints, final List<ProblemInstance<Instance>> instances) {
	super(toClusterPoints.stream().map(DoublePoint::new).collect(Collectors.toList()));
	this.pointToInstance = new HashMap<>();
	for (int i = 0; i < instances.size(); i++) {
		this.pointToInstance.put(toClusterPoints.get(i), instances.get(i));
	}
	this.gmeansCluster = new ArrayList<>();

}
 
Example #8
Source File: KMeans.java    From Java-Data-Analysis with MIT License 5 votes vote down vote up
private static List<DoublePoint> load(double[][] data) {
    List<DoublePoint> points = new ArrayList(M);
    for (double[] pair : data) {
        points.add(new DoublePoint(pair));            
    }
    return points;
}
 
Example #9
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public static List<DoublePoint> normalize(final List<Vector2D> input, double minX, double maxX, double minY, double maxY) {
    double rangeX = maxX - minX;
    double rangeY = maxY - minY;
    List<DoublePoint> points = new ArrayList<DoublePoint>();
    for (Vector2D p : input) {
        double[] arr = p.toArray();
        arr[0] = (arr[0] - minX) / rangeX * 2 - 1;
        arr[1] = (arr[1] - minY) / rangeY * 2 - 1;
        points.add(new DoublePoint(arr));
    }
    return points;
}
 
Example #10
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public static List<DoublePoint> normalize(final List<Vector2D> input, double minX, double maxX, double minY, double maxY) {
    double rangeX = maxX - minX;
    double rangeY = maxY - minY;
    List<DoublePoint> points = new ArrayList<DoublePoint>();
    for (Vector2D p : input) {
        double[] arr = p.toArray();
        arr[0] = (arr[0] - minX) / rangeX * 2 - 1;
        arr[1] = (arr[1] - minY) / rangeY * 2 - 1;
        points.add(new DoublePoint(arr));
    }
    return points;
}
 
Example #11
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public static List<DoublePoint> normalize(final List<Vector2D> input, double minX, double maxX, double minY, double maxY) {
    double rangeX = maxX - minX;
    double rangeY = maxY - minY;
    List<DoublePoint> points = new ArrayList<DoublePoint>();
    for (Vector2D p : input) {
        double[] arr = p.toArray();
        arr[0] = (arr[0] - minX) / rangeX * 2 - 1;
        arr[1] = (arr[1] - minY) / rangeY * 2 - 1;
        points.add(new DoublePoint(arr));
    }
    return points;
}
 
Example #12
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public static List<DoublePoint> normalize(final List<Vector2D> input, double minX, double maxX, double minY, double maxY) {
    double rangeX = maxX - minX;
    double rangeY = maxY - minY;
    List<DoublePoint> points = new ArrayList<DoublePoint>();
    for (Vector2D p : input) {
        double[] arr = p.toArray();
        arr[0] = (arr[0] - minX) / rangeX * 2 - 1;
        arr[1] = (arr[1] - minY) / rangeY * 2 - 1;
        points.add(new DoublePoint(arr));
    }
    return points;
}
 
Example #13
Source File: SumOfClusterVariancesTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Before
public void setUp() {
    evaluator = new SumOfClusterVariances<DoublePoint>(new EuclideanDistance());
}
 
Example #14
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private Clusterable transform(Clusterable point, int width, int height) {
    double[] arr = point.getPoint();
    return new DoublePoint(new double[] { PAD + (arr[0] + 1) / 2.0 * (width - 2 * PAD),
                                          height - PAD - (arr[1] + 1) / 2.0 * (height - 2 * PAD) });
}
 
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
private Clusterable transform(Clusterable point, int width, int height) {
    double[] arr = point.getPoint();
    return new DoublePoint(new double[] { PAD + (arr[0] + 1) / 2.0 * (width - 2 * PAD),
                                          height - PAD - (arr[1] + 1) / 2.0 * (height - 2 * PAD) });
}
 
Example #17
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 #18
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private Clusterable transform(Clusterable point, int width, int height) {
    double[] arr = point.getPoint();
    return new DoublePoint(new double[] { PAD + (arr[0] + 1) / 2.0 * (width - 2 * PAD),
                                          height - PAD - (arr[1] + 1) / 2.0 * (height - 2 * PAD) });
}
 
Example #19
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 #20
Source File: SumOfClusterVariancesTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Before
public void setUp() {
    evaluator = new SumOfClusterVariances<DoublePoint>(new EuclideanDistance());
}
 
Example #21
Source File: ClusterAlgorithmComparison.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private Clusterable transform(Clusterable point, int width, int height) {
    double[] arr = point.getPoint();
    return new DoublePoint(new double[] { PAD + (arr[0] + 1) / 2.0 * (width - 2 * PAD),
                                          height - PAD - (arr[1] + 1) / 2.0 * (height - 2 * PAD) });
}
 
Example #22
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 #23
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();
	}
}