Search in sources :

Example 46 with NominalAttribute

use of smile.data.NominalAttribute in project smile by haifengl.

the class LDATest method testUSPS.

/**
     * Test of learn method, of class LDA.
     */
@Test
public void testUSPS() {
    System.out.println("USPS");
    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);
        int error = 0;
        for (int i = 0; i < testx.length; i++) {
            if (lda.predict(testx[i]) != testy[i]) {
                error++;
            }
        }
        System.out.format("USPS error rate = %.2f%%%n", 100.0 * error / testx.length);
        assertEquals(256, error);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) Test(org.junit.Test)

Example 47 with NominalAttribute

use of smile.data.NominalAttribute in project smile by haifengl.

the class LogisticRegressionTest method testUSPS.

/**
     * Test of learn method, of class LogisticRegression.
     */
@Test
public void testUSPS() {
    System.out.println("USPS");
    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()]);
        LogisticRegression logit = new LogisticRegression(x, y, 0.3, 1E-3, 1000);
        int error = 0;
        for (int i = 0; i < testx.length; i++) {
            if (logit.predict(testx[i]) != testy[i]) {
                error++;
            }
        }
        System.out.format("USPS error rate = %.2f%%%n", 100.0 * error / testx.length);
        assertEquals(188, error);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) Test(org.junit.Test)

Example 48 with NominalAttribute

use of smile.data.NominalAttribute in project smile by haifengl.

the class RBFNetworkTest method testUSPS.

/**
     * Test of learn method, of class RBFNetwork.
     */
@Test
public void testUSPS() {
    System.out.println("USPS");
    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()]);
        double[][] centers = new double[200][];
        RadialBasisFunction basis = SmileUtils.learnGaussianRadialBasis(x, centers);
        RBFNetwork<double[]> rbf = new RBFNetwork<>(x, y, new EuclideanDistance(), new GaussianRadialBasis(8.0), centers);
        int error = 0;
        for (int i = 0; i < testx.length; i++) {
            if (rbf.predict(testx[i]) != testy[i]) {
                error++;
            }
        }
        System.out.format("USPS error rate = %.2f%%%n", 100.0 * error / testx.length);
        assertTrue(error <= 150);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) RadialBasisFunction(smile.math.rbf.RadialBasisFunction) EuclideanDistance(smile.math.distance.EuclideanDistance) AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) GaussianRadialBasis(smile.math.rbf.GaussianRadialBasis) Test(org.junit.Test)

Example 49 with NominalAttribute

use of smile.data.NominalAttribute in project smile by haifengl.

the class RDATest method testUSPS.

/**
     * Test of learn method, of class RDA.
     */
@Test
public void testUSPS() {
    System.out.println("USPS");
    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()]);
        RDA rda = new RDA(x, y, 0.7);
        int error = 0;
        for (int i = 0; i < testx.length; i++) {
            if (rda.predict(testx[i]) != testy[i]) {
                error++;
            }
        }
        System.out.format("USPS error rate = %.2f%%%n", 100.0 * error / testx.length);
        assertEquals(235, error);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) Test(org.junit.Test)

Example 50 with NominalAttribute

use of smile.data.NominalAttribute in project smile by haifengl.

the class RandomForestTest method testUSPS.

/**
     * Test of learn method, of class RandomForest.
     */
@Test
public void testUSPS() {
    System.out.println("USPS");
    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()]);
        RandomForest forest = new RandomForest(x, y, 200);
        int error = 0;
        for (int i = 0; i < testx.length; i++) {
            if (forest.predict(testx[i]) != testy[i]) {
                error++;
            }
        }
        System.out.println(error);
        System.out.format("USPS OOB error rate = %.2f%%%n", 100.0 * forest.error());
        System.out.format("USPS error rate = %.2f%%%n", 100.0 * error / testx.length);
        assertTrue(error <= 140);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) Test(org.junit.Test)

Aggregations

NominalAttribute (smile.data.NominalAttribute)54 DelimitedTextParser (smile.data.parser.DelimitedTextParser)49 AttributeDataset (smile.data.AttributeDataset)48 Test (org.junit.Test)46 AdjustedRandIndex (smile.validation.AdjustedRandIndex)14 RandIndex (smile.validation.RandIndex)14 Attribute (smile.data.Attribute)12 ArrayList (java.util.ArrayList)7 EuclideanDistance (smile.math.distance.EuclideanDistance)5 IOException (java.io.IOException)4 BufferedReader (java.io.BufferedReader)3 ParseException (java.text.ParseException)3 LDA (smile.classification.LDA)3 InputStreamReader (java.io.InputStreamReader)2 DateAttribute (smile.data.DateAttribute)2 NumericAttribute (smile.data.NumericAttribute)2 StringAttribute (smile.data.StringAttribute)2 PlotCanvas (smile.plot.PlotCanvas)2 Accuracy (smile.validation.Accuracy)2 BorderLayout (java.awt.BorderLayout)1