Search in sources :

Example 36 with PlotCanvas

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

the class LDADemo method learn.

/**
     * Execute the projection algorithm and return a swing JComponent representing
     * the clusters.
     */
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;
    }
    int[] label = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
    int min = Math.min(label);
    for (int i = 0; i < label.length; i++) {
        label[i] -= min;
    }
    long clock = System.currentTimeMillis();
    FLD lda = new FLD(data, label, Math.unique(label).length > 3 ? 3 : 2);
    System.out.format("Learn LDA from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    double[][] y = lda.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("Linear Discriminant Analysis");
    return plot;
}
Also used : FLD(smile.classification.FLD) PlotCanvas(smile.plot.PlotCanvas)

Aggregations

PlotCanvas (smile.plot.PlotCanvas)36 GridLayout (java.awt.GridLayout)16 JPanel (javax.swing.JPanel)15 EuclideanDistance (smile.math.distance.EuclideanDistance)6 ArrayList (java.util.ArrayList)4 PCA (smile.projection.PCA)4 BorderLayout (java.awt.BorderLayout)3 Attribute (smile.data.Attribute)3 Graph (smile.graph.Graph)3 CoverTree (smile.neighbor.CoverTree)3 KDTree (smile.neighbor.KDTree)3 LSH (smile.neighbor.LSH)3 LinearSearch (smile.neighbor.LinearSearch)3 MPLSH (smile.neighbor.MPLSH)3 Neighbor (smile.neighbor.Neighbor)3 JFrame (javax.swing.JFrame)2 NominalAttribute (smile.data.NominalAttribute)2 DelimitedTextParser (smile.data.parser.DelimitedTextParser)2 IsotonicMDS (smile.mds.IsotonicMDS)2 MDS (smile.mds.MDS)2