Search in sources :

Example 26 with DelimitedTextParser

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

the class NumericAttributeFeatureTest method testNORMALIZATION.

/**
     * Test of f method, of class NumericAttributeFeature.
     */
@Test
public void testNORMALIZATION() {
    System.out.println("NORMALIZATION");
    DelimitedTextParser parser = new DelimitedTextParser();
    parser.setResponseIndex(new NominalAttribute("class"), 0);
    try {
        AttributeDataset data = parser.parse("USPS Train", smile.data.parser.IOUtils.getTestDataFile("usps/zip.train"));
        double[][] x = data.toArray(new double[data.size()][]);
        double[] min = Math.colMin(x);
        double[] max = Math.colMax(x);
        NumericAttributeFeature naf = new NumericAttributeFeature(data.attributes(), NumericAttributeFeature.Scaling.NORMALIZATION, x);
        Attribute[] attributes = naf.attributes();
        assertEquals(256, attributes.length);
        for (int i = 0; i < x.length; i++) {
            double[] y = new double[attributes.length];
            for (int j = 0; j < y.length; j++) {
                y[j] = naf.f(x[i], j);
                assertEquals((x[i][j] - min[j]) / (max[j] - min[j]), y[j], 1E-7);
            }
        }
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) NominalAttribute(smile.data.NominalAttribute) Attribute(smile.data.Attribute) Test(org.junit.Test)

Example 27 with DelimitedTextParser

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

the class NumericAttributeFeatureTest method testLOGARITHM.

/**
     * Test of f method, of class NumericAttributeFeature.
     */
@Test
public void testLOGARITHM() {
    System.out.println("LOGARITHM");
    DelimitedTextParser parser = new DelimitedTextParser();
    parser.setResponseIndex(new NominalAttribute("class"), 0);
    try {
        AttributeDataset data = parser.parse("USPS Train", smile.data.parser.IOUtils.getTestDataFile("usps/zip.train"));
        double[][] x = data.toArray(new double[data.size()][]);
        for (int i = 0; i < x.length; i++) {
            for (int j = 0; j < x[i].length; j++) {
                x[i][j] += 2.0;
            }
        }
        NumericAttributeFeature naf = new NumericAttributeFeature(data.attributes(), NumericAttributeFeature.Scaling.LOGARITHM);
        Attribute[] attributes = naf.attributes();
        assertEquals(256, attributes.length);
        for (int i = 0; i < x.length; i++) {
            double[] y = new double[attributes.length];
            for (int j = 0; j < y.length; j++) {
                y[j] = naf.f(x[i], j);
                assertEquals(Math.log(x[i][j]), y[j], 1E-7);
            }
        }
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) NominalAttribute(smile.data.NominalAttribute) Attribute(smile.data.Attribute) Test(org.junit.Test)

Example 28 with DelimitedTextParser

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

the class SumSquaresRatioTest method testLearn.

/**
     * Test of learn method, of class SumSquaresRatio.
     */
@Test
public void testLearn() {
    System.out.println("USPS");
    try {
        DelimitedTextParser parser = new DelimitedTextParser();
        parser.setResponseIndex(new NominalAttribute("class"), 0);
        AttributeDataset train = parser.parse("USPS Train", smile.data.parser.IOUtils.getTestDataFile("usps/zip.train"));
        AttributeDataset test = parser.parse("USPS Test", smile.data.parser.IOUtils.getTestDataFile("usps/zip.test"));
        double[][] x = train.toArray(new double[train.size()][]);
        int[] y = train.toArray(new int[train.size()]);
        double[][] testx = test.toArray(new double[test.size()][]);
        int[] testy = test.toArray(new int[test.size()]);
        SumSquaresRatio ssr = new SumSquaresRatio();
        double[] score = ssr.rank(x, y);
        int[] index = QuickSort.sort(score);
        int p = 135;
        int n = x.length;
        double[][] xx = new double[n][p];
        for (int j = 0; j < p; j++) {
            for (int i = 0; i < n; i++) {
                xx[i][j] = x[i][index[255 - j]];
            }
        }
        int testn = testx.length;
        double[][] testxx = new double[testn][p];
        for (int j = 0; j < p; j++) {
            for (int i = 0; i < testn; i++) {
                testxx[i][j] = testx[i][index[255 - j]];
            }
        }
        LDA lda = new LDA(xx, y);
        int[] prediction = new int[testn];
        for (int i = 0; i < testn; i++) {
            prediction[i] = lda.predict(testxx[i]);
        }
        double accuracy = new Accuracy().measure(testy, prediction);
        System.out.format("SSR %.2f%%%n", 100 * accuracy);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) Accuracy(smile.validation.Accuracy) NominalAttribute(smile.data.NominalAttribute) LDA(smile.classification.LDA) Test(org.junit.Test)

Example 29 with DelimitedTextParser

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

the class VQDemo method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    if ("startButton".equals(e.getActionCommand())) {
        try {
            clusterNumber = Integer.parseInt(clusterNumberField.getText().trim());
            if (clusterNumber < 2) {
                JOptionPane.showMessageDialog(this, "Invalid K: " + clusterNumber, "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (clusterNumber > dataset[datasetIndex].length / 2) {
                JOptionPane.showMessageDialog(this, "Too large K: " + clusterNumber, "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Invalid K: " + clusterNumberField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        Thread thread = new Thread(this);
        thread.start();
    } else if ("datasetBox".equals(e.getActionCommand())) {
        datasetIndex = datasetBox.getSelectedIndex();
        if (dataset[datasetIndex] == null) {
            DelimitedTextParser parser = new DelimitedTextParser();
            parser.setDelimiter("[\t ]+");
            try {
                AttributeDataset data = parser.parse(datasetName[datasetIndex], smile.data.parser.IOUtils.getTestDataFile(datasource[datasetIndex]));
                dataset[datasetIndex] = data.toArray(new double[data.size()][]);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Failed to load dataset.", "ERROR", JOptionPane.ERROR_MESSAGE);
                System.err.println(ex);
            }
        }
        remove(canvas);
        if (dataset[datasetIndex].length < 500) {
            pointLegend = 'o';
        } else {
            pointLegend = '.';
        }
        canvas = ScatterPlot.plot(dataset[datasetIndex], pointLegend);
        add(canvas, BorderLayout.CENTER);
        validate();
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset)

Example 30 with DelimitedTextParser

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

the class ValidationTest method testTest_3args_1.

/**
     * Test of test method, of class Validation.
     */
@Test
public void testTest_3args_1() {
    System.out.println("test");
    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"));
        AttributeDataset test = parser.parse("USPS Test", smile.data.parser.IOUtils.getTestDataFile("usps/zip.test"));
        double[][] x = train.toArray(new double[train.size()][]);
        int[] y = train.toArray(new int[train.size()]);
        double[][] testx = test.toArray(new double[test.size()][]);
        int[] testy = test.toArray(new int[test.size()]);
        LDA lda = new LDA(x, y);
        double accuracy = Validation.test(lda, testx, testy);
        System.out.println("accuracy = " + accuracy);
        assertEquals(0.8724, accuracy, 1E-4);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) LDA(smile.classification.LDA) Test(org.junit.Test)

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