Search in sources :

Example 1 with SpectralClustering

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

the class SpectralClusteringDemo method learn.

@Override
public JComponent learn() {
    try {
        gaussianWidth = Double.parseDouble(gaussianWidthField.getText().trim());
        if (gaussianWidth <= 0) {
            JOptionPane.showMessageDialog(this, "Invalid Gaussian Width: " + gaussianWidth, "Error", JOptionPane.ERROR_MESSAGE);
            return null;
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Invalid Gaussian Width: " + gaussianWidthField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
        return null;
    }
    long clock = System.currentTimeMillis();
    SpectralClustering spectral = new SpectralClustering(dataset[datasetIndex], clusterNumber, gaussianWidth);
    System.out.format("Spectral Clustering clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
    PlotCanvas plot = ScatterPlot.plot(dataset[datasetIndex], pointLegend);
    for (int k = 0; k < spectral.getNumClusters(); k++) {
        double[][] cluster = new double[spectral.getClusterSize()[k]][];
        for (int i = 0, j = 0; i < dataset[datasetIndex].length; i++) {
            if (spectral.getClusterLabel()[i] == k) {
                cluster[j++] = dataset[datasetIndex][i];
            }
        }
        plot.points(cluster, pointLegend, Palette.COLORS[k % Palette.COLORS.length]);
    }
    return plot;
}
Also used : SpectralClustering(smile.clustering.SpectralClustering) PlotCanvas(smile.plot.PlotCanvas)

Aggregations

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