Search in sources :

Example 1 with PCA

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

Example 2 with PCA

use of smile.projection.PCA 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 3 with PCA

use of smile.projection.PCA 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 4 with PCA

use of smile.projection.PCA in project smile by haifengl.

the class GHADemo method learn.

@Override
public JComponent learn() {
    JPanel pane = new JPanel(new GridLayout(2, 2));
    double[][] data = Math.clone(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);
    clock = System.currentTimeMillis();
    GHA gha = new GHA(data[0].length, 2, 0.00001);
    for (int iter = 1; iter <= 500; iter++) {
        double error = 0.0;
        for (int i = 0; i < data.length; i++) {
            error += gha.learn(data[i]);
        }
        error /= data.length;
        if (iter % 100 == 0) {
            System.out.format("Iter %3d, Error = %.5g\n", iter, error);
        }
    }
    System.out.format("Learn GHA from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    y = gha.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("GHA");
    pane.add(plot);
    clock = System.currentTimeMillis();
    gha = new GHA(data[0].length, 3, 0.00001);
    for (int iter = 1; iter <= 500; iter++) {
        double error = 0.0;
        for (int i = 0; i < data.length; i++) {
            error += gha.learn(data[i]);
        }
        error /= data.length;
        if (iter % 100 == 0) {
            System.out.format("Iter %3d, Error = %.5g\n", iter, error);
        }
    }
    System.out.format("Learn GHA from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    y = gha.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("GHA");
    pane.add(plot);
    return pane;
}
Also used : JPanel(javax.swing.JPanel) GHA(smile.projection.GHA) GridLayout(java.awt.GridLayout) PCA(smile.projection.PCA) PlotCanvas(smile.plot.PlotCanvas)

Example 5 with PCA

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

Aggregations

GridLayout (java.awt.GridLayout)5 JPanel (javax.swing.JPanel)5 PlotCanvas (smile.plot.PlotCanvas)5 PCA (smile.projection.PCA)5 GaussianKernel (smile.math.kernel.GaussianKernel)1 GHA (smile.projection.GHA)1 KPCA (smile.projection.KPCA)1 PPCA (smile.projection.PPCA)1 RandomProjection (smile.projection.RandomProjection)1