Search in sources :

Example 1 with XMeans

use of smile.clustering.XMeans in project smile by haifengl.

the class XMeansDemo 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();
    XMeans xmeans = new XMeans(dataset[datasetIndex], maxClusterNumber);
    System.out.format("X-Means clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
    PlotCanvas plot = ScatterPlot.plot(xmeans.centroids(), '@');
    for (int k = 0; k < xmeans.getNumClusters(); k++) {
        if (xmeans.getClusterSize()[k] > 0) {
            double[][] cluster = new double[xmeans.getClusterSize()[k]][];
            for (int i = 0, j = 0; i < dataset[datasetIndex].length; i++) {
                if (xmeans.getClusterLabel()[i] == k) {
                    cluster[j++] = dataset[datasetIndex][i];
                }
            }
            plot.points(cluster, pointLegend, Palette.COLORS[k % Palette.COLORS.length]);
        }
    }
    plot.points(xmeans.centroids(), '@');
    return plot;
}
Also used : XMeans(smile.clustering.XMeans) PlotCanvas(smile.plot.PlotCanvas)

Aggregations

XMeans (smile.clustering.XMeans)1 PlotCanvas (smile.plot.PlotCanvas)1