Search in sources :

Example 36 with DelimitedTextParser

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

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

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

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

Example 40 with DelimitedTextParser

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

the class DeterministicAnnealingTest method testUSPS.

/**
     * Test of learn method, of class DeterministicAnnealing.
     */
@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()]);
        DeterministicAnnealing annealing = new DeterministicAnnealing(x, 10, 0.8);
        AdjustedRandIndex ari = new AdjustedRandIndex();
        RandIndex rand = new RandIndex();
        double r = rand.measure(y, annealing.getClusterLabel());
        double r2 = ari.measure(y, annealing.getClusterLabel());
        System.out.format("Training rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
        assertTrue(r > 0.75);
        assertTrue(r2 > 0.25);
        int[] p = new int[testx.length];
        for (int i = 0; i < testx.length; i++) {
            p[i] = annealing.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.75);
        assertTrue(r2 > 0.3);
    } 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

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