Search in sources :

Example 1 with KPCA

use of smile.projection.KPCA 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)1 JPanel (javax.swing.JPanel)1 GaussianKernel (smile.math.kernel.GaussianKernel)1 PlotCanvas (smile.plot.PlotCanvas)1 KPCA (smile.projection.KPCA)1 PCA (smile.projection.PCA)1