Search in sources :

Example 1 with PlotCanvas

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

the class ClassificationDemo method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    if ("startButton".equals(e.getActionCommand())) {
        Thread thread = new Thread(this);
        thread.start();
    } else if ("datasetBox".equals(e.getActionCommand())) {
        datasetIndex = datasetBox.getSelectedIndex();
        if (dataset[datasetIndex] == null) {
            DelimitedTextParser parser = new DelimitedTextParser();
            parser.setDelimiter("[\t ]+");
            parser.setResponseIndex(new NominalAttribute("class"), 0);
            try {
                dataset[datasetIndex] = parser.parse(datasetName[datasetIndex], smile.data.parser.IOUtils.getTestDataFile(datasource[datasetIndex]));
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Failed to load dataset.", "ERROR", JOptionPane.ERROR_MESSAGE);
                System.err.println(ex);
            }
        }
        double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
        int[] label = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
        if (data.length < 500) {
            pointLegend = 'o';
        } else {
            pointLegend = '.';
        }
        PlotCanvas canvas = ScatterPlot.plot(data, pointLegend);
        for (int i = 0; i < data.length; i++) {
            canvas.point(pointLegend, Palette.COLORS[label[i]], data[i]);
        }
        BorderLayout layout = (BorderLayout) getLayout();
        remove(layout.getLayoutComponent(BorderLayout.CENTER));
        add(canvas, BorderLayout.CENTER);
        validate();
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) NominalAttribute(smile.data.NominalAttribute) BorderLayout(java.awt.BorderLayout) PlotCanvas(smile.plot.PlotCanvas)

Example 2 with PlotCanvas

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

the class CLARANSDemo method learn.

@Override
public JComponent learn() {
    try {
        numLocal = Integer.parseInt(numLocalField.getText().trim());
        if (numLocal < 5) {
            JOptionPane.showMessageDialog(this, "Toll smal NumLocal: " + numLocal, ERROR, JOptionPane.ERROR_MESSAGE);
            return null;
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Invalid NumLocal: " + numLocalField.getText(), ERROR, JOptionPane.ERROR_MESSAGE);
        return null;
    }
    try {
        maxNeighbor = Integer.parseInt(maxNeighborField.getText().trim());
        if (maxNeighbor < 5) {
            JOptionPane.showMessageDialog(this, "Too small MaxNeighbor: " + maxNeighbor, ERROR, JOptionPane.ERROR_MESSAGE);
            return null;
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Invalid MaxNeighbor: " + maxNeighborField.getText(), ERROR, JOptionPane.ERROR_MESSAGE);
        return null;
    }
    long clock = System.currentTimeMillis();
    CLARANS<double[]> clarans = new CLARANS<>(dataset[datasetIndex], new EuclideanDistance(), clusterNumber, maxNeighbor, numLocal);
    System.out.format("CLARANS clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
    PlotCanvas plot = ScatterPlot.plot(clarans.medoids(), '@');
    for (int k = 0; k < clusterNumber; k++) {
        if (clarans.getClusterSize()[k] > 0) {
            double[][] cluster = new double[clarans.getClusterSize()[k]][];
            for (int i = 0, j = 0; i < dataset[datasetIndex].length; i++) {
                if (clarans.getClusterLabel()[i] == k) {
                    cluster[j++] = dataset[datasetIndex][i];
                }
            }
            plot.points(cluster, pointLegend, Palette.COLORS[k % Palette.COLORS.length]);
        }
    }
    plot.points(clarans.medoids(), '@');
    return plot;
}
Also used : CLARANS(smile.clustering.CLARANS) EuclideanDistance(smile.math.distance.EuclideanDistance) PlotCanvas(smile.plot.PlotCanvas)

Example 3 with PlotCanvas

use of smile.plot.PlotCanvas 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;
}
Also used : DeterministicAnnealing(smile.clustering.DeterministicAnnealing) PlotCanvas(smile.plot.PlotCanvas)

Example 4 with PlotCanvas

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

the class SOMDemo method learn.

@Override
public JComponent learn() {
    try {
        width = Integer.parseInt(widthField.getText().trim());
        if (width < 1) {
            JOptionPane.showMessageDialog(this, "Invalid width: " + width, "Error", JOptionPane.ERROR_MESSAGE);
            return null;
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Invalid width: " + widthField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
        return null;
    }
    try {
        height = Integer.parseInt(heightField.getText().trim());
        if (height < 1) {
            JOptionPane.showMessageDialog(this, "Invalid height: " + height, "Error", JOptionPane.ERROR_MESSAGE);
            return null;
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Invalid height: " + heightField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
        return null;
    }
    long clock = System.currentTimeMillis();
    SOM som = new SOM(dataset[datasetIndex], width, height);
    System.out.format("SOM clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
    JPanel pane = new JPanel(new GridLayout(2, 3));
    PlotCanvas plot = ScatterPlot.plot(dataset[datasetIndex], pointLegend);
    plot.grid(som.map());
    plot.setTitle("SOM Grid");
    pane.add(plot);
    int[] membership = som.partition(clusterNumber);
    int[] clusterSize = new int[clusterNumber];
    for (int i = 0; i < membership.length; i++) {
        clusterSize[membership[i]]++;
    }
    plot = ScatterPlot.plot(dataset[datasetIndex], pointLegend);
    plot.setTitle("Hierarchical Clustering");
    for (int k = 0; k < clusterNumber; k++) {
        double[][] cluster = new double[clusterSize[k]][];
        for (int i = 0, j = 0; i < dataset[datasetIndex].length; i++) {
            if (membership[i] == k) {
                cluster[j++] = dataset[datasetIndex][i];
            }
        }
        plot.points(cluster, pointLegend, Palette.COLORS[k % Palette.COLORS.length]);
    }
    pane.add(plot);
    double[][] umatrix = som.umatrix();
    double[] umatrix1 = new double[umatrix.length * umatrix[0].length];
    for (int i = 0, k = 0; i < umatrix.length; i++) {
        for (int j = 0; j < umatrix[i].length; j++, k++) umatrix1[k] = umatrix[i][j];
    }
    plot = Histogram.plot(null, umatrix1, 20);
    plot.setTitle("U-Matrix Histogram");
    pane.add(plot);
    GaussianMixture mixture = new GaussianMixture(umatrix1);
    double w = (Math.max(umatrix1) - Math.min(umatrix1)) / 24;
    double[][] p = new double[50][2];
    for (int i = 0; i < p.length; i++) {
        p[i][0] = Math.min(umatrix1) + i * w;
        p[i][1] = mixture.p(p[i][0]) * w;
    }
    plot.line(p, Color.RED);
    plot = Hexmap.plot(umatrix, Palette.jet(256));
    plot.setTitle("U-Matrix");
    pane.add(plot);
    /*
        double[][] x = new double[height][width];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                x[i][j] = som.getMap()[i][j][0];
            }
        }
        plot = PlotCanvas.hexmap(x, Palette.jet(256));
        plot.setTitle("X");
        pane.add(plot);

        double[][] y = new double[height][width];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                y[i][j] = som.getMap()[i][j][1];
            }
        }
        plot = PlotCanvas.hexmap(y, Palette.jet(256));
        plot.setTitle("Y");
        pane.add(plot);
*/
    return pane;
}
Also used : SOM(smile.vq.SOM) JPanel(javax.swing.JPanel) GridLayout(java.awt.GridLayout) GaussianMixture(smile.stat.distribution.GaussianMixture) PlotCanvas(smile.plot.PlotCanvas)

Example 5 with PlotCanvas

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

the class PCADemo method learn.

@Override
public JComponent learn() {
    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;
    }
    boolean cor = corBox.getSelectedIndex() != 0;
    long clock = System.currentTimeMillis();
    PCA pca = new PCA(data, cor);
    System.out.format("Learn PCA from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    JPanel pane = new JPanel(new GridLayout(1, 2));
    PlotCanvas scree = PlotCanvas.screeplot(pca);
    scree.setTitle("Variance");
    pane.add(scree);
    pca.setProjection(3);
    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("Scatter Plot");
    pane.add(plot);
    return pane;
}
Also used : JPanel(javax.swing.JPanel) GridLayout(java.awt.GridLayout) PCA(smile.projection.PCA) 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