Search in sources :

Example 6 with DelimitedTextParser

use of smile.data.parser.DelimitedTextParser in project smile by haifengl.

the class LDADemo method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    if ("startButton".equals(e.getActionCommand())) {
        datasetIndex = datasetBox.getSelectedIndex();
        if (dataset[datasetIndex] == null) {
            DelimitedTextParser parser = new DelimitedTextParser();
            parser.setDelimiter("[\t]+");
            if (datasetIndex == 0) {
                parser.setColumnNames(true);
            }
            if (datasetIndex == 0) {
                parser.setResponseIndex(new NominalAttribute("class"), 4);
            }
            if (datasetIndex == 1) {
                parser.setResponseIndex(new NominalAttribute("class"), 16);
            }
            try {
                dataset[datasetIndex] = parser.parse(datasetName[datasetIndex], smile.data.parser.IOUtils.getTestDataFile(datasource[datasetIndex]));
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Failed to load dataset.", "ERROR", JOptionPane.ERROR_MESSAGE);
                System.out.println(ex);
            }
        }
        if (dataset[datasetIndex].size() < 500) {
            pointLegend = 'o';
        } else {
            pointLegend = '.';
        }
        Thread thread = new Thread(this);
        thread.start();
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) NominalAttribute(smile.data.NominalAttribute)

Example 7 with DelimitedTextParser

use of smile.data.parser.DelimitedTextParser in project smile by haifengl.

the class ProjectionDemo method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    if ("startButton".equals(e.getActionCommand())) {
        datasetIndex = datasetBox.getSelectedIndex();
        if (dataset[datasetIndex] == null) {
            DelimitedTextParser parser = new DelimitedTextParser();
            parser.setDelimiter("[\t]+");
            if (datasetIndex < 5 && datasetIndex != 3) {
                parser.setColumnNames(true);
            }
            if (datasetIndex == 1) {
                parser.setRowNames(true);
            }
            if (datasetIndex == 0) {
                parser.setResponseIndex(new NominalAttribute("class"), 4);
            }
            if (datasetIndex == 3) {
                parser.setResponseIndex(new NominalAttribute("class"), 16);
            }
            if (datasetIndex >= 5) {
                parser.setResponseIndex(new NominalAttribute("class"), 4);
            }
            try {
                dataset[datasetIndex] = parser.parse(datasetName[datasetIndex], smile.data.parser.IOUtils.getTestDataFile(datasource[datasetIndex]));
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Failed to load dataset.", "ERROR", JOptionPane.ERROR_MESSAGE);
                System.out.println(ex);
            }
        }
        if (dataset[datasetIndex].size() < 500) {
            pointLegend = 'o';
        } else {
            pointLegend = '.';
        }
        Thread thread = new Thread(this);
        thread.start();
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) NominalAttribute(smile.data.NominalAttribute)

Example 8 with DelimitedTextParser

use of smile.data.parser.DelimitedTextParser 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 9 with DelimitedTextParser

use of smile.data.parser.DelimitedTextParser in project smile by haifengl.

the class IsotonicMDSDemo method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    if ("startButton".equals(e.getActionCommand())) {
        datasetIndex = datasetBox.getSelectedIndex();
        if (dataset[datasetIndex] == null) {
            DelimitedTextParser parser = new DelimitedTextParser();
            parser.setDelimiter("[\t]+");
            parser.setRowNames(true);
            parser.setColumnNames(true);
            if (datasetIndex == 2 || datasetIndex == 3) {
                parser.setRowNames(false);
            }
            try {
                dataset[datasetIndex] = parser.parse(datasetName[datasetIndex], smile.data.parser.IOUtils.getTestDataFile(datasource[datasetIndex]));
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Failed to load dataset.", "ERROR", JOptionPane.ERROR_MESSAGE);
                System.err.println(ex);
            }
        }
        Thread thread = new Thread(this);
        thread.start();
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser)

Example 10 with DelimitedTextParser

use of smile.data.parser.DelimitedTextParser in project smile by haifengl.

the class SammonMappingDemo method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    if ("startButton".equals(e.getActionCommand())) {
        datasetIndex = datasetBox.getSelectedIndex();
        if (dataset[datasetIndex] == null) {
            DelimitedTextParser parser = new DelimitedTextParser();
            parser.setDelimiter("[\t]+");
            parser.setRowNames(true);
            parser.setColumnNames(true);
            if (datasetIndex == 2 || datasetIndex == 3) {
                parser.setRowNames(false);
            }
            try {
                dataset[datasetIndex] = parser.parse(datasetName[datasetIndex], smile.data.parser.IOUtils.getTestDataFile(datasource[datasetIndex]));
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Failed to load dataset.", "ERROR", JOptionPane.ERROR_MESSAGE);
                System.err.println(ex);
            }
        }
        Thread thread = new Thread(this);
        thread.start();
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser)

Aggregations

DelimitedTextParser (smile.data.parser.DelimitedTextParser)55 NominalAttribute (smile.data.NominalAttribute)49 AttributeDataset (smile.data.AttributeDataset)48 Test (org.junit.Test)45 AdjustedRandIndex (smile.validation.AdjustedRandIndex)14 RandIndex (smile.validation.RandIndex)14 Attribute (smile.data.Attribute)8 EuclideanDistance (smile.math.distance.EuclideanDistance)5 ArrayList (java.util.ArrayList)4 LDA (smile.classification.LDA)3 PlotCanvas (smile.plot.PlotCanvas)2 Accuracy (smile.validation.Accuracy)2 BorderLayout (java.awt.BorderLayout)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 JFrame (javax.swing.JFrame)1 ClassifierTrainer (smile.classification.ClassifierTrainer)1 CompleteLinkage (smile.clustering.linkage.CompleteLinkage)1 SingleLinkage (smile.clustering.linkage.SingleLinkage)1 UPGMALinkage (smile.clustering.linkage.UPGMALinkage)1