Search in sources :

Example 36 with NominalAttribute

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

the class LinearSearchSpeedTest method testUSPS.

/**
     * Test of nearest method, of class LinearSearch.
     */
@Test
public void testUSPS() {
    System.out.println("USPS");
    double[][] x = null;
    double[][] testx = null;
    long start = System.currentTimeMillis();
    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"));
        x = train.toArray(new double[train.size()][]);
        testx = test.toArray(new double[test.size()][]);
    } catch (Exception ex) {
        System.err.println(ex);
    }
    double time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("Loading USPS: %.2fs%n", time);
    LinearSearch<double[]> naive = new LinearSearch<>(x, new EuclideanDistance());
    start = System.currentTimeMillis();
    for (int i = 0; i < testx.length; i++) {
        naive.nearest(testx[i]);
    }
    time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("NN: %.2fs%n", time);
    start = System.currentTimeMillis();
    for (int i = 0; i < testx.length; i++) {
        naive.knn(testx[i], 10);
    }
    time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("10-NN: %.2fs%n", time);
    start = System.currentTimeMillis();
    List<Neighbor<double[], double[]>> n = new ArrayList<>();
    for (int i = 0; i < testx.length; i++) {
        naive.range(testx[i], 8.0, n);
        n.clear();
    }
    time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("Range: %.2fs%n", time);
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) ArrayList(java.util.ArrayList) EuclideanDistance(smile.math.distance.EuclideanDistance) NominalAttribute(smile.data.NominalAttribute) Test(org.junit.Test)

Example 37 with NominalAttribute

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

the class MPLSHSpeedTest method testUSPS.

/**
     * Test of nearest method, of class KDTree.
     */
@Test
public void testUSPS() {
    System.out.println("USPS");
    double[][] x = null;
    double[][] testx = null;
    long start = System.currentTimeMillis();
    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"));
        x = train.toArray(new double[train.size()][]);
        testx = test.toArray(new double[test.size()][]);
    } catch (Exception ex) {
        System.err.println(ex);
    }
    double time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("Loading USPS: %.2fs%n", time);
    start = System.currentTimeMillis();
    MPLSH<double[]> lsh = new MPLSH<>(256, 100, 3, 4.0);
    for (double[] xi : x) {
        lsh.put(xi, xi);
    }
    double[][] train = new double[500][];
    int[] index = Math.permutate(x.length);
    for (int i = 0; i < train.length; i++) {
        train[i] = x[index[i]];
    }
    LinearSearch<double[]> naive = new LinearSearch<>(x, new EuclideanDistance());
    lsh.learn(naive, train, 8.0);
    time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("Building LSH: %.2fs%n", time);
    start = System.currentTimeMillis();
    for (int i = 0; i < testx.length; i++) {
        lsh.nearest(testx[i]);
    }
    time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("NN: %.2fs%n", time);
    start = System.currentTimeMillis();
    for (int i = 0; i < testx.length; i++) {
        lsh.knn(testx[i], 10);
    }
    time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("10-NN: %.2fs%n", time);
    start = System.currentTimeMillis();
    List<Neighbor<double[], double[]>> n = new ArrayList<>();
    for (int i = 0; i < testx.length; i++) {
        lsh.range(testx[i], 8.0, n);
        n.clear();
    }
    time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("Range: %.2fs%n", time);
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) ArrayList(java.util.ArrayList) EuclideanDistance(smile.math.distance.EuclideanDistance) NominalAttribute(smile.data.NominalAttribute) Test(org.junit.Test)

Example 38 with NominalAttribute

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

the class DelimitedTextParserTest method testParse.

/**
     * Test of parse method, of class DelimitedTextParser.
     */
@Test
public void testParse() throws Exception {
    System.out.println("parse");
    try {
        DelimitedTextParser parser = new DelimitedTextParser();
        parser.setResponseIndex(new NominalAttribute("class"), 0);
        AttributeDataset usps = parser.parse("USPS Train", smile.data.parser.IOUtils.getTestDataFile("usps/zip.train"));
        double[][] x = usps.toArray(new double[usps.size()][]);
        int[] y = usps.toArray(new int[usps.size()]);
        assertEquals(Attribute.Type.NOMINAL, usps.response().getType());
        for (Attribute attribute : usps.attributes()) {
            assertEquals(Attribute.Type.NUMERIC, attribute.getType());
        }
        assertEquals(7291, usps.size());
        assertEquals(256, usps.attributes().length);
        assertEquals("6", usps.response().toString(y[0]));
        assertEquals("5", usps.response().toString(y[1]));
        assertEquals("4", usps.response().toString(y[2]));
        assertEquals(-1.0000, x[0][6], 1E-7);
        assertEquals(-0.6310, x[0][7], 1E-7);
        assertEquals(0.8620, x[0][8], 1E-7);
        assertEquals("1", usps.response().toString(y[7290]));
        assertEquals(-1.0000, x[7290][4], 1E-7);
        assertEquals(-0.1080, x[7290][5], 1E-7);
        assertEquals(1.0000, x[7290][6], 1E-7);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) NominalAttribute(smile.data.NominalAttribute) Attribute(smile.data.Attribute) Test(org.junit.Test)

Example 39 with NominalAttribute

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

the class ValidationTest method testTest_4args_1.

/**
     * Test of test method, of class Validation.
     */
@Test
public void testTest_4args_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);
        ClassificationMeasure[] measures = { new Accuracy() };
        double[] accuracy = Validation.test(lda, testx, testy, measures);
        System.out.println("accuracy = " + accuracy[0]);
        assertEquals(0.8724, accuracy[0], 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)

Example 40 with NominalAttribute

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

the class NeuralGasTest method testUSPS.

/**
     * Test of learn method, of class NeuralGas.
     */
@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()]);
        NeuralGas gas = new NeuralGas(x, 10);
        AdjustedRandIndex ari = new AdjustedRandIndex();
        RandIndex rand = new RandIndex();
        double r = rand.measure(y, gas.getClusterLabel());
        double r2 = ari.measure(y, gas.getClusterLabel());
        System.out.format("Training rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
        assertTrue(r > 0.88);
        assertTrue(r2 > 0.45);
        int[] p = new int[testx.length];
        for (int i = 0; i < testx.length; i++) {
            p[i] = gas.predict(testx[i]);
        }
        r = rand.measure(testy, p);
        r2 = ari.measure(testy, p);
        System.out.format("Testing rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
        assertTrue(r > 0.88);
        assertTrue(r2 > 0.45);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) RandIndex(smile.validation.RandIndex) AdjustedRandIndex(smile.validation.AdjustedRandIndex) AdjustedRandIndex(smile.validation.AdjustedRandIndex) 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