Search in sources :

Example 31 with PlotCanvas

use of smile.plot.PlotCanvas in project smile by haifengl.

the class DBScanDemo method learn.

@Override
public JComponent learn() {
    try {
        minPts = Integer.parseInt(minPtsField.getText().trim());
        if (minPts < 1) {
            JOptionPane.showMessageDialog(this, "Invalid MinPts: " + minPts, "Error", JOptionPane.ERROR_MESSAGE);
            return null;
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Invalid MinPts: " + minPtsField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
        return null;
    }
    try {
        range = Double.parseDouble(rangeField.getText().trim());
        if (range <= 0) {
            JOptionPane.showMessageDialog(this, "Invalid Range: " + range, "Error", JOptionPane.ERROR_MESSAGE);
            return null;
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Invalid range: " + rangeField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
        return null;
    }
    long clock = System.currentTimeMillis();
    DBScan<double[]> dbscan = new DBScan<>(dataset[datasetIndex], new EuclideanDistance(), minPts, range);
    System.out.format("DBSCAN clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
    JPanel pane = new JPanel(new GridLayout(1, 2));
    PlotCanvas plot = ScatterPlot.plot(dataset[datasetIndex], pointLegend);
    for (int k = 0; k < dbscan.getNumClusters(); k++) {
        double[][] cluster = new double[dbscan.getClusterSize()[k]][];
        for (int i = 0, j = 0; i < dataset[datasetIndex].length; i++) {
            if (dbscan.getClusterLabel()[i] == k) {
                cluster[j++] = dataset[datasetIndex][i];
            }
        }
        plot.points(cluster, pointLegend, Palette.COLORS[k % Palette.COLORS.length]);
    }
    pane.add(plot);
    return pane;
}
Also used : EuclideanDistance(smile.math.distance.EuclideanDistance) JPanel(javax.swing.JPanel) GridLayout(java.awt.GridLayout) DBScan(smile.clustering.DBScan) PlotCanvas(smile.plot.PlotCanvas)

Example 32 with PlotCanvas

use of smile.plot.PlotCanvas in project smile by haifengl.

the class DENCLUEDemo method learn.

@Override
public JComponent learn() {
    try {
        k = Integer.parseInt(kField.getText().trim());
        if (k < 1) {
            JOptionPane.showMessageDialog(this, "Invalid K: " + k, "Error", JOptionPane.ERROR_MESSAGE);
            return null;
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Invalid K: " + kField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
        return null;
    }
    try {
        sigma = Double.parseDouble(sigmaField.getText().trim());
        if (sigma <= 0) {
            JOptionPane.showMessageDialog(this, "Invalid Sigma: " + sigma, "Error", JOptionPane.ERROR_MESSAGE);
            return null;
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Invalid Sigma: " + sigmaField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
        return null;
    }
    long clock = System.currentTimeMillis();
    DENCLUE denclue = new DENCLUE(dataset[datasetIndex], sigma, k);
    System.out.format("DENCLUE clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
    JPanel pane = new JPanel(new GridLayout(1, 2));
    PlotCanvas plot = ScatterPlot.plot(dataset[datasetIndex], pointLegend);
    for (int l = 0; l < denclue.getNumClusters(); l++) {
        double[][] cluster = new double[denclue.getClusterSize()[l]][];
        for (int i = 0, j = 0; i < dataset[datasetIndex].length; i++) {
            if (denclue.getClusterLabel()[i] == l) {
                cluster[j++] = dataset[datasetIndex][i];
            }
        }
        plot.points(cluster, pointLegend, Palette.COLORS[l % Palette.COLORS.length]);
    }
    plot.points(denclue.getDensityAttractors(), '@');
    pane.add(plot);
    return pane;
}
Also used : JPanel(javax.swing.JPanel) GridLayout(java.awt.GridLayout) DENCLUE(smile.clustering.DENCLUE) PlotCanvas(smile.plot.PlotCanvas)

Example 33 with PlotCanvas

use of smile.plot.PlotCanvas in project smile by haifengl.

the class KPCADemo method learn.

@Override
public JComponent learn() {
    JPanel pane = new JPanel(new GridLayout(2, 2));
    double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
    String[] names = dataset[datasetIndex].toArray(new String[dataset[datasetIndex].size()]);
    if (names[0] == null) {
        names = null;
    }
    if (gamma[datasetIndex] == 0.0) {
        int n = 0;
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < i; j++, n++) {
                gamma[datasetIndex] += Math.squaredDistance(data[i], data[j]);
            }
        }
        gamma[datasetIndex] = Math.sqrt(gamma[datasetIndex] / n) / 4;
    } else {
        try {
            gamma[datasetIndex] = Double.parseDouble(gammaNumberField.getText().trim());
            if (gamma[datasetIndex] <= 0) {
                JOptionPane.showMessageDialog(this, "Invalid parameter: " + gamma[datasetIndex], "Error", JOptionPane.ERROR_MESSAGE);
                return null;
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Invalid parameter: " + gammaNumberField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
            return null;
        }
    }
    gammaNumberField.setText(String.format("%.4f", gamma[datasetIndex]));
    long clock = System.currentTimeMillis();
    PCA pca = new PCA(data, true);
    System.out.format("Learn PCA from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    pca.setProjection(2);
    double[][] y = pca.project(data);
    PlotCanvas plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
    if (names != null) {
        plot.points(y, names);
    } else if (dataset[datasetIndex].response() != null) {
        int[] labels = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
        for (int i = 0; i < y.length; i++) {
            plot.point(pointLegend, Palette.COLORS[labels[i]], y[i]);
        }
    } else {
        plot.points(y, pointLegend);
    }
    plot.setTitle("PCA");
    pane.add(plot);
    pca.setProjection(3);
    y = pca.project(data);
    plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
    if (names != null) {
        plot.points(y, names);
    } else if (dataset[datasetIndex].response() != null) {
        int[] labels = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
        for (int i = 0; i < y.length; i++) {
            plot.point(pointLegend, Palette.COLORS[labels[i]], y[i]);
        }
    } else {
        plot.points(y, pointLegend);
    }
    plot.setTitle("PCA");
    pane.add(plot);
    KPCA<double[]> kpca = new KPCA<>(data, new GaussianKernel(gamma[datasetIndex]), 2);
    y = kpca.getCoordinates();
    plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
    if (names != null) {
        plot.points(y, names);
    } else if (dataset[datasetIndex].response() != null) {
        int[] labels = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
        for (int i = 0; i < y.length; i++) {
            plot.point(pointLegend, Palette.COLORS[labels[i]], y[i]);
        }
    } else {
        plot.points(y, pointLegend);
    }
    plot.setTitle("KPCA");
    pane.add(plot);
    clock = System.currentTimeMillis();
    kpca = new KPCA<>(data, new GaussianKernel(gamma[datasetIndex]), 3);
    System.out.format("Learn KPCA from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    y = kpca.getCoordinates();
    plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
    if (names != null) {
        plot.points(y, names);
    } else if (dataset[datasetIndex].response() != null) {
        int[] labels = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
        for (int i = 0; i < y.length; i++) {
            plot.point(pointLegend, Palette.COLORS[labels[i]], y[i]);
        }
    } else {
        plot.points(y, pointLegend);
    }
    plot.setTitle("KPCA");
    pane.add(plot);
    return pane;
}
Also used : KPCA(smile.projection.KPCA) JPanel(javax.swing.JPanel) KPCA(smile.projection.KPCA) PCA(smile.projection.PCA) GridLayout(java.awt.GridLayout) GaussianKernel(smile.math.kernel.GaussianKernel) PlotCanvas(smile.plot.PlotCanvas)

Example 34 with PlotCanvas

use of smile.plot.PlotCanvas 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)

Example 35 with PlotCanvas

use of smile.plot.PlotCanvas 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

PlotCanvas (smile.plot.PlotCanvas)36 GridLayout (java.awt.GridLayout)16 JPanel (javax.swing.JPanel)15 EuclideanDistance (smile.math.distance.EuclideanDistance)6 ArrayList (java.util.ArrayList)4 PCA (smile.projection.PCA)4 BorderLayout (java.awt.BorderLayout)3 Attribute (smile.data.Attribute)3 Graph (smile.graph.Graph)3 CoverTree (smile.neighbor.CoverTree)3 KDTree (smile.neighbor.KDTree)3 LSH (smile.neighbor.LSH)3 LinearSearch (smile.neighbor.LinearSearch)3 MPLSH (smile.neighbor.MPLSH)3 Neighbor (smile.neighbor.Neighbor)3 JFrame (javax.swing.JFrame)2 NominalAttribute (smile.data.NominalAttribute)2 DelimitedTextParser (smile.data.parser.DelimitedTextParser)2 IsotonicMDS (smile.mds.IsotonicMDS)2 MDS (smile.mds.MDS)2