Search in sources :

Example 16 with PlotCanvas

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

the class GrowingNeuralGasDemo method learn.

@Override
public JComponent learn() {
    long clock = System.currentTimeMillis();
    GrowingNeuralGas gas = new GrowingNeuralGas(2, 0.05, 0.0006, 88, 200, 0.5, 0.9995);
    for (int loop = 0; loop < 25; loop++) {
        for (double[] x : dataset[datasetIndex]) {
            gas.update(x);
        }
    }
    gas.partition(clusterNumber);
    System.out.format("Growing Neural Gas clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
    int[] membership = new int[dataset[datasetIndex].length];
    int[] clusterSize = new int[clusterNumber];
    for (int i = 0; i < dataset[datasetIndex].length; i++) {
        membership[i] = gas.predict(dataset[datasetIndex][i]);
        clusterSize[membership[i]]++;
    }
    GrowingNeuralGas.Neuron[] neurons = gas.neurons();
    double[][] x = new double[neurons.length][];
    for (int i = 0; i < x.length; i++) x[i] = neurons[i].w;
    PlotCanvas plot = ScatterPlot.plot(x, '@');
    for (int k = 0; k < clusterNumber; k++) {
        if (clusterSize[k] > 0) {
            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]);
        }
    }
    for (int i = 0; i < neurons.length; i++) {
        for (int j = 0; j < neurons[i].neighbors.length; j++) {
            plot.line(neurons[i].w, neurons[i].neighbors[j].w);
        }
    }
    plot.points(x, '@');
    return plot;
}
Also used : GrowingNeuralGas(smile.vq.GrowingNeuralGas) PlotCanvas(smile.plot.PlotCanvas)

Example 17 with PlotCanvas

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

the class ClassificationDemo method run.

@Override
public void run() {
    startButton.setEnabled(false);
    datasetBox.setEnabled(false);
    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]);
    }
    double[] lower = canvas.getLowerBounds();
    double[] upper = canvas.getUpperBounds();
    double[] x = new double[50];
    double step = (upper[0] - lower[0]) / x.length;
    for (int i = 0; i < x.length; i++) {
        x[i] = lower[0] + step * (i + 1);
    }
    double[] y = new double[50];
    step = (upper[1] - lower[1]) / y.length;
    for (int i = 0; i < y.length; i++) {
        y[i] = lower[1] + step * (i + 1);
    }
    try {
        double[][] f = learn(x, y);
        if (f != null) {
            for (int i = 0; i < y.length; i++) {
                for (int j = 0; j < x.length; j++) {
                    double[] p = { x[j], y[i] };
                    canvas.point('.', Palette.COLORS[(int) f[i][j]], p);
                }
            }
            double[] levels = { 0.5 };
            Contour contour = new Contour(x, y, f, levels);
            contour.showLevelValue(false);
            canvas.add(contour);
            BorderLayout layout = (BorderLayout) getLayout();
            remove(layout.getLayoutComponent(BorderLayout.CENTER));
            add(canvas, BorderLayout.CENTER);
            validate();
        }
    } catch (Exception ex) {
        System.err.println(ex);
    }
    startButton.setEnabled(true);
    datasetBox.setEnabled(true);
}
Also used : BorderLayout(java.awt.BorderLayout) Contour(smile.plot.Contour) PlotCanvas(smile.plot.PlotCanvas)

Example 18 with PlotCanvas

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

the class ClassificationDemo method ancestorAdded.

@Override
public void ancestorAdded(AncestorEvent event) {
    if (datasetBox.getSelectedIndex() != datasetIndex) {
        datasetBox.setSelectedIndex(datasetIndex);
        double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
        int[] label = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
        PlotCanvas canvas = ScatterPlot.plot(data, 'o');
        for (int i = 0; i < data.length; i++) {
            canvas.point('o', Palette.COLORS[label[i]], data[i]);
        }
        BorderLayout layout = (BorderLayout) getLayout();
        remove(layout.getLayoutComponent(BorderLayout.CENTER));
        add(canvas, BorderLayout.CENTER);
        validate();
    }
}
Also used : BorderLayout(java.awt.BorderLayout) PlotCanvas(smile.plot.PlotCanvas)

Example 19 with PlotCanvas

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

the class IsotonicMDSDemo method learn.

/**
     * Execute the MDS algorithm and return a swing JComponent representing
     * the clusters.
     */
public JComponent learn() {
    JPanel pane = new JPanel(new GridLayout(1, 2));
    double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
    String[] labels = dataset[datasetIndex].toArray(new String[dataset[datasetIndex].size()]);
    if (labels[0] == null) {
        Attribute[] attr = dataset[datasetIndex].attributes();
        labels = new String[attr.length];
        for (int i = 0; i < labels.length; i++) {
            labels[i] = attr[i].getName();
        }
    }
    long clock = System.currentTimeMillis();
    IsotonicMDS isomds = new IsotonicMDS(data, 2);
    System.out.format("Learn Kruskal's Nonmetric MDS (k=2) from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    PlotCanvas plot = ScatterPlot.plot(isomds.getCoordinates(), labels);
    plot.setTitle("Kruskal's Nonmetric MDS (k = 2)");
    pane.add(plot);
    clock = System.currentTimeMillis();
    isomds = new IsotonicMDS(data, 3);
    System.out.format("Learn Kruskal's Nonmetric MDS (k=3) from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    plot = ScatterPlot.plot(isomds.getCoordinates(), labels);
    plot.setTitle("Kruskal's Nonmetric MDS (k = 3)");
    pane.add(plot);
    return pane;
}
Also used : JPanel(javax.swing.JPanel) GridLayout(java.awt.GridLayout) IsotonicMDS(smile.mds.IsotonicMDS) Attribute(smile.data.Attribute) PlotCanvas(smile.plot.PlotCanvas)

Example 20 with PlotCanvas

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

the class MDSDemo method learn.

/**
     * Execute the MDS algorithm and return a swing JComponent representing
     * the clusters.
     */
public JComponent learn() {
    JPanel pane = new JPanel(new GridLayout(1, 2));
    double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
    String[] labels = dataset[datasetIndex].toArray(new String[dataset[datasetIndex].size()]);
    if (labels[0] == null) {
        Attribute[] attr = dataset[datasetIndex].attributes();
        labels = new String[attr.length];
        for (int i = 0; i < labels.length; i++) {
            labels[i] = attr[i].getName();
        }
    }
    long clock = System.currentTimeMillis();
    MDS mds = new MDS(data, 2);
    System.out.format("Learn MDS (k=2) from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    PlotCanvas plot = ScatterPlot.plot(mds.getCoordinates(), labels);
    plot.setTitle("MDS (k = 2)");
    pane.add(plot);
    clock = System.currentTimeMillis();
    mds = new MDS(data, 3);
    System.out.format("Learn MDS (k=3) from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    plot = ScatterPlot.plot(mds.getCoordinates(), labels);
    plot.setTitle("MDS (k = 3)");
    pane.add(plot);
    return pane;
}
Also used : JPanel(javax.swing.JPanel) GridLayout(java.awt.GridLayout) Attribute(smile.data.Attribute) MDS(smile.mds.MDS) 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