Search in sources :

Example 1 with MDS

use of smile.mds.MDS in project smile by haifengl.

the class HexmapDemo method main.

public static void main(String[] args) {
    DelimitedTextParser parser = new DelimitedTextParser();
    parser.setResponseIndex(new NominalAttribute("class"), 0);
    try {
        AttributeDataset train = parser.parse("USPS Train", smile.data.parser.IOUtils.getTestDataFile("usps/zip.train"));
        double[][] x = train.toArray(new double[train.size()][]);
        int[] y = train.toArray(new int[train.size()]);
        int m = 20;
        int n = 20;
        SOM som = new SOM(x, m, n);
        String[][] labels = new String[m][n];
        int[] neurons = new int[x.length];
        for (int i = 0; i < x.length; i++) {
            neurons[i] = som.predict(x[i]);
        }
        int[] count = new int[10];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                Arrays.fill(count, 0);
                for (int k = 0; k < neurons.length; k++) {
                    if (neurons[k] == i * n + j) {
                        count[y[k]]++;
                    }
                }
                int sum = Math.sum(count);
                if (sum == 0.0) {
                    labels[i][j] = "no samples";
                } else {
                    labels[i][j] = String.format("<table border=\"1\"><tr><td>Total</td><td align=\"right\">%d</td></tr>", sum);
                    for (int l = 0; l < count.length; l++) {
                        if (count[l] > 0) {
                            labels[i][j] += String.format("<tr><td>class %d</td><td align=\"right\">%.1f%%</td></tr>", l, 100.0 * count[l] / sum);
                        }
                    }
                    labels[i][j] += "</table>";
                }
            }
        }
        double[][] umatrix = som.umatrix();
        double[][][] map = som.map();
        double[][] proximity = new double[m * n][m * n];
        for (int i = 0; i < m * n; i++) {
            for (int j = 0; j < m * n; j++) {
                proximity[i][j] = Math.distance(map[i / n][i % n], map[j / n][j % n]);
            }
        }
        MDS mds = new MDS(proximity, 3);
        double[][] coords = mds.getCoordinates();
        double[][][] mdsgrid = new double[m][n][];
        for (int i = 0; i < m * n; i++) {
            mdsgrid[i / n][i % n] = mds.getCoordinates()[i];
        }
        SammonMapping sammon = new SammonMapping(proximity, coords);
        double[][][] sammongrid = new double[m][n][];
        for (int i = 0; i < m * n; i++) {
            sammongrid[i / n][i % n] = sammon.getCoordinates()[i];
        }
        IsotonicMDS isomds = new IsotonicMDS(proximity, coords);
        double[][][] isomdsgrid = new double[m][n][];
        for (int i = 0; i < m * n; i++) {
            isomdsgrid[i / n][i % n] = isomds.getCoordinates()[i];
        }
        JFrame frame = new JFrame("Hexmap");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.add(Hexmap.plot(labels, umatrix));
        PlotCanvas canvas = Surface.plot(mdsgrid);
        canvas.setTitle("MDS");
        frame.add(canvas);
        canvas = Surface.plot(isomdsgrid);
        canvas.setTitle("Isotonic MDS");
        frame.add(canvas);
        canvas = Surface.plot(sammongrid);
        canvas.setTitle("Sammon Mapping");
        frame.add(canvas);
        frame.setVisible(true);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) SammonMapping(smile.mds.SammonMapping) SOM(smile.vq.SOM) NominalAttribute(smile.data.NominalAttribute) IsotonicMDS(smile.mds.IsotonicMDS) JFrame(javax.swing.JFrame) MDS(smile.mds.MDS) IsotonicMDS(smile.mds.IsotonicMDS) PlotCanvas(smile.plot.PlotCanvas)

Example 2 with MDS

use of smile.mds.MDS in project smile by haifengl.

the class MDSDemo method learn.

/**
     * Execute the MDS algorithm and return a swing JComponent representing
     * the clusters.
     */
public JComponent learn() {
    JPanel pane = new JPanel(new GridLayout(1, 2));
    double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
    String[] labels = dataset[datasetIndex].toArray(new String[dataset[datasetIndex].size()]);
    if (labels[0] == null) {
        Attribute[] attr = dataset[datasetIndex].attributes();
        labels = new String[attr.length];
        for (int i = 0; i < labels.length; i++) {
            labels[i] = attr[i].getName();
        }
    }
    long clock = System.currentTimeMillis();
    MDS mds = new MDS(data, 2);
    System.out.format("Learn MDS (k=2) from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    PlotCanvas plot = ScatterPlot.plot(mds.getCoordinates(), labels);
    plot.setTitle("MDS (k = 2)");
    pane.add(plot);
    clock = System.currentTimeMillis();
    mds = new MDS(data, 3);
    System.out.format("Learn MDS (k=3) from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    plot = ScatterPlot.plot(mds.getCoordinates(), labels);
    plot.setTitle("MDS (k = 3)");
    pane.add(plot);
    return pane;
}
Also used : JPanel(javax.swing.JPanel) GridLayout(java.awt.GridLayout) Attribute(smile.data.Attribute) MDS(smile.mds.MDS) PlotCanvas(smile.plot.PlotCanvas)

Aggregations

MDS (smile.mds.MDS)2 PlotCanvas (smile.plot.PlotCanvas)2 GridLayout (java.awt.GridLayout)1 JFrame (javax.swing.JFrame)1 JPanel (javax.swing.JPanel)1 Attribute (smile.data.Attribute)1 AttributeDataset (smile.data.AttributeDataset)1 NominalAttribute (smile.data.NominalAttribute)1 DelimitedTextParser (smile.data.parser.DelimitedTextParser)1 IsotonicMDS (smile.mds.IsotonicMDS)1 SammonMapping (smile.mds.SammonMapping)1 SOM (smile.vq.SOM)1