use of smile.clustering.DeterministicAnnealing in project smile by haifengl.
the class DeterministicAnnealingDemo method learn.
@Override
public JComponent learn() {
try {
alpha = Double.parseDouble(alphaField.getText().trim());
if (alpha <= 0 || alpha >= 1) {
JOptionPane.showMessageDialog(this, "Invalid alpha: " + alpha, "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Invalid K: " + alphaField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
long clock = System.currentTimeMillis();
DeterministicAnnealing annealing = new DeterministicAnnealing(dataset[datasetIndex], clusterNumber, alpha);
System.out.format("Deterministic Annealing clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
PlotCanvas plot = ScatterPlot.plot(annealing.centroids(), '@');
for (int k = 0; k < clusterNumber; k++) {
if (annealing.getClusterSize()[k] > 0) {
double[][] cluster = new double[annealing.getClusterSize()[k]][];
for (int i = 0, j = 0; i < dataset[datasetIndex].length; i++) {
if (annealing.getClusterLabel()[i] == k) {
cluster[j++] = dataset[datasetIndex][i];
}
}
plot.points(cluster, pointLegend, Palette.COLORS[k % Palette.COLORS.length]);
}
}
plot.points(annealing.centroids(), '@');
return plot;
}
Aggregations