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 cluster
int i = 1;
for (MLCluster cluster : kmeans.getClusters()) {
System.out.println("*** Cluster " + (i++) + " ***");
final MLDataSet ds = cluster.createDataSet();
final MLDataPair pair = BasicMLDataPair.createPair(ds.getInputSize(), ds.getIdealSize());
for (int j = 0; j < ds.getRecordCount(); j++) {
ds.getRecord(j, pair);
System.out.println(Arrays.toString(pair.getInputArray()));
}
}

This will generate the following output:

*** Cluster 1 ***
[16.0, 15.0, 32.0]
[1.0, 2.0, 3.0]
[3.0, 2.0, 1.0]
*** Cluster 2 ***
[28.0, 15.0, 22.0]
*** Cluster 3 ***
[32.0, 20.0, 44.0]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.96.94