Search in sources :

Example 1 with RandomProjection

use of smile.projection.RandomProjection 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)

Aggregations

GridLayout (java.awt.GridLayout)1 JPanel (javax.swing.JPanel)1 PlotCanvas (smile.plot.PlotCanvas)1 PCA (smile.projection.PCA)1 RandomProjection (smile.projection.RandomProjection)1