Search in sources :

Example 1 with GHA

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

Aggregations

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