use of smile.clustering.GMeans in project smile by haifengl.
the class GMeansDemo method learn.
@Override
public JComponent learn() {
try {
maxClusterNumber = Integer.parseInt(maxClusterNumberField.getText().trim());
if (maxClusterNumber < 2) {
JOptionPane.showMessageDialog(this, "Invalid Max K: " + maxClusterNumber, "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Invalid Max K: " + maxClusterNumberField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
long clock = System.currentTimeMillis();
GMeans gmeans = new GMeans(dataset[datasetIndex], maxClusterNumber);
System.out.format("G-Means clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
PlotCanvas plot = ScatterPlot.plot(gmeans.centroids(), '@');
for (int k = 0; k < gmeans.getNumClusters(); k++) {
if (gmeans.getClusterSize()[k] > 0) {
double[][] cluster = new double[gmeans.getClusterSize()[k]][];
for (int i = 0, j = 0; i < dataset[datasetIndex].length; i++) {
if (gmeans.getClusterLabel()[i] == k) {
cluster[j++] = dataset[datasetIndex][i];
}
}
plot.points(cluster, pointLegend, Palette.COLORS[k % Palette.COLORS.length]);
}
}
plot.points(gmeans.centroids(), '@');
return plot;
}
Aggregations