Clustering using Encog

Encog supports k-means clustering. Let's consider a very simple example, with the data shown in the following code block:

DATA = { { 28, 15, 22 }, { 16, 15, 32 }, { 32, 20, 44 }, { 1, 2, 3 }, { 3, 2, 1 } };

To make BasicMLDataSet from this data, a simple for loop is used, which will add data to the dataset:

BasicMLDataSet set = new BasicMLDataSet();for (final double[] element : DATA) {    set.add(new BasicMLData(element));}

Using the KMeansClustering function, let's clusters the dataset into two clusters, as follows:

KMeansClustering kmeans = new KMeansClustering(2, set);kmeans.iteration(100);// Display the clusterint i = 1;for (MLCluster cluster : kmeans.getClusters()) { System.out.println("*** Cluster " + (i++) + " ***"); ...

Get Machine Learning in Java - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.