Search in sources :

Example 6 with PlotCanvas

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

the class PPCADemo 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;
    }
    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);
    PPCA ppca = new PPCA(data, 2);
    y = ppca.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("PPCA");
    pane.add(plot);
    clock = System.currentTimeMillis();
    ppca = new PPCA(data, 3);
    System.out.format("Learn PPCA from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    y = ppca.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("PPCA");
    pane.add(plot);
    return pane;
}
Also used : JPanel(javax.swing.JPanel) GridLayout(java.awt.GridLayout) PPCA(smile.projection.PPCA) PCA(smile.projection.PCA) PPCA(smile.projection.PPCA) PlotCanvas(smile.plot.PlotCanvas)

Example 7 with PlotCanvas

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

the class RandomProjectionDemo 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;
    }
    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);
    RandomProjection rp = new RandomProjection(data[0].length, 2, sparseBox.isSelected());
    System.out.format("%d x %d Random Projection:\n", data[0].length, 3);
    double[][] projection = rp.getProjection();
    for (int i = 0; i < projection.length; i++) {
        for (int j = 0; j < projection[i].length; j++) {
            System.out.format("% .4f ", projection[i][j]);
        }
        System.out.println();
    }
    y = rp.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("Random Projection");
    pane.add(plot);
    rp = new RandomProjection(data[0].length, 3, sparseBox.isSelected());
    System.out.format("%d x %d Random Projection:\n", data[0].length, 3);
    projection = rp.getProjection();
    for (int i = 0; i < projection.length; i++) {
        for (int j = 0; j < projection[i].length; j++) {
            System.out.format("% .4f ", projection[i][j]);
        }
        System.out.println();
    }
    y = rp.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("Random Projection");
    pane.add(plot);
    return pane;
}
Also used : JPanel(javax.swing.JPanel) GridLayout(java.awt.GridLayout) RandomProjection(smile.projection.RandomProjection) PCA(smile.projection.PCA) PlotCanvas(smile.plot.PlotCanvas)

Example 8 with PlotCanvas

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

the class NeuralMapDemo method learn.

@Override
public JComponent learn() {
    try {
        T = Double.parseDouble(TNumberField.getText().trim());
        if (T <= 0) {
            JOptionPane.showMessageDialog(this, "Invalid T: " + T, "Error", JOptionPane.ERROR_MESSAGE);
            return null;
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Invalid T: " + TNumberField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
        return null;
    }
    long clock = System.currentTimeMillis();
    NeuralMap cortex = new NeuralMap(2, T, 0.05, 0.0006, 5, 3);
    for (int i = 0; i < 5; i++) {
        for (double[] x : dataset[datasetIndex]) {
            cortex.update(x);
        }
    }
    cortex.purge(16);
    cortex.partition(clusterNumber, 16);
    System.out.format("Cortex clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
    int[] y = new int[dataset[datasetIndex].length];
    int[] clusterSize = new int[clusterNumber];
    for (int i = 0; i < dataset[datasetIndex].length; i++) {
        y[i] = cortex.predict(dataset[datasetIndex][i]);
        if (y[i] != Clustering.OUTLIER) {
            clusterSize[y[i]]++;
        }
    }
    List<NeuralMap.Neuron> nodes = cortex.neurons();
    double[][] x = new double[nodes.size()][];
    for (int i = 0; i < x.length; i++) {
        x[i] = nodes.get(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 (y[i] == k) {
                    cluster[j++] = dataset[datasetIndex][i];
                }
            }
            plot.points(cluster, pointLegend, Palette.COLORS[k % Palette.COLORS.length]);
        }
    }
    for (int i = 0; i < nodes.size(); i++) {
        NeuralMap.Neuron neuron = nodes.get(i);
        for (NeuralMap.Neuron neighbor : neuron.neighbors) {
            plot.line(neuron.w, neighbor.w);
        }
    }
    plot.points(x, '@');
    return plot;
}
Also used : NeuralMap(smile.vq.NeuralMap) PlotCanvas(smile.plot.PlotCanvas)

Example 9 with PlotCanvas

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

the class RNNSearchDemo method run.

@Override
public void run() {
    startButton.setEnabled(false);
    logNSlider.setEnabled(false);
    dimensionSlider.setEnabled(false);
    radiusField.setEnabled(false);
    logN = logNSlider.getValue();
    dimension = dimensionSlider.getValue();
    System.out.println("Generating dataset...");
    int n = (int) Math.pow(10, logN);
    double[][] data = new double[n][];
    for (int i = 0; i < n; i++) {
        data[i] = new double[dimension];
        for (int j = 0; j < dimension; j++) {
            data[i][j] = Math.random();
        }
    }
    int[] perm = Math.permutate(n);
    System.out.println("Building searching data structure...");
    long time = System.currentTimeMillis();
    LinearSearch<double[]> naive = new LinearSearch<>(data, new EuclideanDistance());
    int naiveBuild = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    KDTree<double[]> kdtree = new KDTree<>(data, data);
    int kdtreeBuild = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    CoverTree<double[]> cover = new CoverTree<>(data, new EuclideanDistance());
    int coverBuild = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    LSH<double[]> lsh = new LSH<>(dimension, 5, (int) Math.log2(dimension), 4 * radius, 1017881);
    for (int i = 0; i < n; i++) {
        lsh.put(data[i], data[i]);
    }
    int lshBuild = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    MPLSH<double[]> mplsh = new MPLSH<>(dimension, 3, (int) Math.log2(n), 4 * radius, 1017881);
    for (int i = 0; i < n; i++) {
        mplsh.put(data[i], data[i]);
    }
    double[][] train = new double[1000][];
    for (int i = 0; i < train.length; i++) {
        train[i] = data[perm[i]];
    }
    mplsh.learn(kdtree, train, radius);
    int mplshBuild = (int) (System.currentTimeMillis() - time);
    System.out.println("Perform 1000 searches...");
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        ArrayList<Neighbor<double[], double[]>> neighbors = new ArrayList<>();
        naive.range(data[perm[i]], radius, neighbors);
    }
    int naiveSearch = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        ArrayList<Neighbor<double[], double[]>> neighbors = new ArrayList<>();
        kdtree.range(data[perm[i]], radius, neighbors);
    }
    int kdtreeSearch = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        ArrayList<Neighbor<double[], double[]>> neighbors = new ArrayList<>();
        cover.range(data[perm[i]], radius, neighbors);
    }
    int coverSearch = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        ArrayList<Neighbor<double[], double[]>> neighbors = new ArrayList<>();
        lsh.range(data[perm[i]], radius, neighbors);
    }
    int lshSearch = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        ArrayList<Neighbor<double[], double[]>> neighbors = new ArrayList<>();
        mplsh.range(data[perm[i]], radius, neighbors, 0.95, 10);
    }
    int mplshSearch = (int) (System.currentTimeMillis() - time);
    canvas.removeAll();
    double[] buildTime = { naiveBuild, kdtreeBuild, coverBuild, lshBuild, mplshBuild };
    PlotCanvas build = BarPlot.plot(buildTime, label);
    build.setTitle("Build Time");
    canvas.add(build);
    double[] searchTime = { naiveSearch, kdtreeSearch, coverSearch, lshSearch, mplshSearch };
    PlotCanvas search = BarPlot.plot(searchTime, label);
    search.setTitle("Search Time");
    canvas.add(search);
    validate();
    startButton.setEnabled(true);
    logNSlider.setEnabled(true);
    dimensionSlider.setEnabled(true);
    radiusField.setEnabled(false);
}
Also used : MPLSH(smile.neighbor.MPLSH) LSH(smile.neighbor.LSH) CoverTree(smile.neighbor.CoverTree) ArrayList(java.util.ArrayList) Neighbor(smile.neighbor.Neighbor) EuclideanDistance(smile.math.distance.EuclideanDistance) KDTree(smile.neighbor.KDTree) MPLSH(smile.neighbor.MPLSH) LinearSearch(smile.neighbor.LinearSearch) PlotCanvas(smile.plot.PlotCanvas)

Example 10 with PlotCanvas

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

the class HexmapDemo method main.

public static void main(String[] args) {
    DelimitedTextParser parser = new DelimitedTextParser();
    parser.setResponseIndex(new NominalAttribute("class"), 0);
    try {
        AttributeDataset train = parser.parse("USPS Train", smile.data.parser.IOUtils.getTestDataFile("usps/zip.train"));
        double[][] x = train.toArray(new double[train.size()][]);
        int[] y = train.toArray(new int[train.size()]);
        int m = 20;
        int n = 20;
        SOM som = new SOM(x, m, n);
        String[][] labels = new String[m][n];
        int[] neurons = new int[x.length];
        for (int i = 0; i < x.length; i++) {
            neurons[i] = som.predict(x[i]);
        }
        int[] count = new int[10];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                Arrays.fill(count, 0);
                for (int k = 0; k < neurons.length; k++) {
                    if (neurons[k] == i * n + j) {
                        count[y[k]]++;
                    }
                }
                int sum = Math.sum(count);
                if (sum == 0.0) {
                    labels[i][j] = "no samples";
                } else {
                    labels[i][j] = String.format("<table border=\"1\"><tr><td>Total</td><td align=\"right\">%d</td></tr>", sum);
                    for (int l = 0; l < count.length; l++) {
                        if (count[l] > 0) {
                            labels[i][j] += String.format("<tr><td>class %d</td><td align=\"right\">%.1f%%</td></tr>", l, 100.0 * count[l] / sum);
                        }
                    }
                    labels[i][j] += "</table>";
                }
            }
        }
        double[][] umatrix = som.umatrix();
        double[][][] map = som.map();
        double[][] proximity = new double[m * n][m * n];
        for (int i = 0; i < m * n; i++) {
            for (int j = 0; j < m * n; j++) {
                proximity[i][j] = Math.distance(map[i / n][i % n], map[j / n][j % n]);
            }
        }
        MDS mds = new MDS(proximity, 3);
        double[][] coords = mds.getCoordinates();
        double[][][] mdsgrid = new double[m][n][];
        for (int i = 0; i < m * n; i++) {
            mdsgrid[i / n][i % n] = mds.getCoordinates()[i];
        }
        SammonMapping sammon = new SammonMapping(proximity, coords);
        double[][][] sammongrid = new double[m][n][];
        for (int i = 0; i < m * n; i++) {
            sammongrid[i / n][i % n] = sammon.getCoordinates()[i];
        }
        IsotonicMDS isomds = new IsotonicMDS(proximity, coords);
        double[][][] isomdsgrid = new double[m][n][];
        for (int i = 0; i < m * n; i++) {
            isomdsgrid[i / n][i % n] = isomds.getCoordinates()[i];
        }
        JFrame frame = new JFrame("Hexmap");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.add(Hexmap.plot(labels, umatrix));
        PlotCanvas canvas = Surface.plot(mdsgrid);
        canvas.setTitle("MDS");
        frame.add(canvas);
        canvas = Surface.plot(isomdsgrid);
        canvas.setTitle("Isotonic MDS");
        frame.add(canvas);
        canvas = Surface.plot(sammongrid);
        canvas.setTitle("Sammon Mapping");
        frame.add(canvas);
        frame.setVisible(true);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) SammonMapping(smile.mds.SammonMapping) SOM(smile.vq.SOM) NominalAttribute(smile.data.NominalAttribute) IsotonicMDS(smile.mds.IsotonicMDS) JFrame(javax.swing.JFrame) MDS(smile.mds.MDS) IsotonicMDS(smile.mds.IsotonicMDS) 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