Search in sources :

Example 1 with Contour

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

the class ClassificationDemo method run.

@Override
public void run() {
    startButton.setEnabled(false);
    datasetBox.setEnabled(false);
    double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
    int[] label = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
    if (data.length < 500) {
        pointLegend = 'o';
    } else {
        pointLegend = '.';
    }
    PlotCanvas canvas = ScatterPlot.plot(data, pointLegend);
    for (int i = 0; i < data.length; i++) {
        canvas.point(pointLegend, Palette.COLORS[label[i]], data[i]);
    }
    double[] lower = canvas.getLowerBounds();
    double[] upper = canvas.getUpperBounds();
    double[] x = new double[50];
    double step = (upper[0] - lower[0]) / x.length;
    for (int i = 0; i < x.length; i++) {
        x[i] = lower[0] + step * (i + 1);
    }
    double[] y = new double[50];
    step = (upper[1] - lower[1]) / y.length;
    for (int i = 0; i < y.length; i++) {
        y[i] = lower[1] + step * (i + 1);
    }
    try {
        double[][] f = learn(x, y);
        if (f != null) {
            for (int i = 0; i < y.length; i++) {
                for (int j = 0; j < x.length; j++) {
                    double[] p = { x[j], y[i] };
                    canvas.point('.', Palette.COLORS[(int) f[i][j]], p);
                }
            }
            double[] levels = { 0.5 };
            Contour contour = new Contour(x, y, f, levels);
            contour.showLevelValue(false);
            canvas.add(contour);
            BorderLayout layout = (BorderLayout) getLayout();
            remove(layout.getLayoutComponent(BorderLayout.CENTER));
            add(canvas, BorderLayout.CENTER);
            validate();
        }
    } catch (Exception ex) {
        System.err.println(ex);
    }
    startButton.setEnabled(true);
    datasetBox.setEnabled(true);
}
Also used : BorderLayout(java.awt.BorderLayout) Contour(smile.plot.Contour) PlotCanvas(smile.plot.PlotCanvas)

Aggregations

BorderLayout (java.awt.BorderLayout)1 Contour (smile.plot.Contour)1 PlotCanvas (smile.plot.PlotCanvas)1