Search in sources :

Example 36 with AttributeDataset

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

the class QDATest method testLearn.

/**
     * Test of learn method, of class QDA.
     */
@Test
public void testLearn() {
    System.out.println("learn");
    ArffParser arffParser = new ArffParser();
    arffParser.setResponseIndex(4);
    try {
        AttributeDataset iris = arffParser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/iris.arff"));
        double[][] x = iris.toArray(new double[iris.size()][]);
        int[] y = iris.toArray(new int[iris.size()]);
        int n = x.length;
        LOOCV loocv = new LOOCV(n);
        int error = 0;
        double[] posteriori = new double[3];
        for (int i = 0; i < n; i++) {
            double[][] trainx = Math.slice(x, loocv.train[i]);
            int[] trainy = Math.slice(y, loocv.train[i]);
            QDA qda = new QDA(trainx, trainy);
            if (y[loocv.test[i]] != qda.predict(x[loocv.test[i]], posteriori))
                error++;
        //System.out.println(posteriori[0]+"\t"+posteriori[1]+"\t"+posteriori[2]);
        }
        System.out.println("QDA error = " + error);
        assertEquals(4, error);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) LOOCV(smile.validation.LOOCV) Test(org.junit.Test)

Example 37 with AttributeDataset

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

the class RBFNetworkTest method testSegment.

/**
     * Test of learn method, of class RBFNetwork.
     */
@Test
public void testSegment() {
    System.out.println("Segment");
    ArffParser parser = new ArffParser();
    parser.setResponseIndex(19);
    try {
        AttributeDataset train = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/segment-challenge.arff"));
        AttributeDataset test = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/segment-test.arff"));
        double[][] x = train.toArray(new double[0][]);
        int[] y = train.toArray(new int[0]);
        double[][] testx = test.toArray(new double[0][]);
        int[] testy = test.toArray(new int[0]);
        double[][] centers = new double[100][];
        RadialBasisFunction[] basis = SmileUtils.learnGaussianRadialBasis(x, centers, 5.0);
        RBFNetwork<double[]> rbf = new RBFNetwork<>(x, y, new EuclideanDistance(), basis, centers);
        int error = 0;
        for (int i = 0; i < testx.length; i++) {
            if (rbf.predict(testx[i]) != testy[i]) {
                error++;
            }
        }
        System.out.format("Segment error rate = %.2f%%%n", 100.0 * error / testx.length);
        assertTrue(error <= 210);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : RadialBasisFunction(smile.math.rbf.RadialBasisFunction) EuclideanDistance(smile.math.distance.EuclideanDistance) ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) Test(org.junit.Test)

Example 38 with AttributeDataset

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

the class RDATest method testLearn.

/**
     * Test of learn method, of class RDA.
     */
@Test
public void testLearn() {
    System.out.println("learn");
    ArffParser arffParser = new ArffParser();
    arffParser.setResponseIndex(4);
    try {
        AttributeDataset iris = arffParser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/iris.arff"));
        double[][] x = iris.toArray(new double[iris.size()][]);
        int[] y = iris.toArray(new int[iris.size()]);
        int n = x.length;
        LOOCV loocv = new LOOCV(n);
        int error = 0;
        for (int i = 0; i < n; i++) {
            double[][] trainx = Math.slice(x, loocv.train[i]);
            int[] trainy = Math.slice(y, loocv.train[i]);
            RDA rda = new RDA(trainx, trainy, 0.0);
            if (y[loocv.test[i]] != rda.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("RDA (0.0) error = " + error);
        assertEquals(22, error);
        error = 0;
        for (int i = 0; i < n; i++) {
            double[][] trainx = Math.slice(x, loocv.train[i]);
            int[] trainy = Math.slice(y, loocv.train[i]);
            RDA rda = new RDA(trainx, trainy, 0.1);
            if (y[loocv.test[i]] != rda.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("RDA (0.1) error = " + error);
        assertEquals(24, error);
        error = 0;
        for (int i = 0; i < n; i++) {
            double[][] trainx = Math.slice(x, loocv.train[i]);
            int[] trainy = Math.slice(y, loocv.train[i]);
            RDA rda = new RDA(trainx, trainy, 0.2);
            if (y[loocv.test[i]] != rda.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("RDA (0.2) error = " + error);
        assertEquals(20, error);
        error = 0;
        for (int i = 0; i < n; i++) {
            double[][] trainx = Math.slice(x, loocv.train[i]);
            int[] trainy = Math.slice(y, loocv.train[i]);
            RDA rda = new RDA(trainx, trainy, 0.3);
            if (y[loocv.test[i]] != rda.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("RDA (0.3) error = " + error);
        assertEquals(19, error);
        error = 0;
        for (int i = 0; i < n; i++) {
            double[][] trainx = Math.slice(x, loocv.train[i]);
            int[] trainy = Math.slice(y, loocv.train[i]);
            RDA rda = new RDA(trainx, trainy, 0.4);
            if (y[loocv.test[i]] != rda.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("RDA (0.4) error = " + error);
        assertEquals(16, error);
        error = 0;
        for (int i = 0; i < n; i++) {
            double[][] trainx = Math.slice(x, loocv.train[i]);
            int[] trainy = Math.slice(y, loocv.train[i]);
            RDA rda = new RDA(trainx, trainy, 0.5);
            if (y[loocv.test[i]] != rda.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("RDA (0.5) error = " + error);
        assertEquals(12, error);
        error = 0;
        for (int i = 0; i < n; i++) {
            double[][] trainx = Math.slice(x, loocv.train[i]);
            int[] trainy = Math.slice(y, loocv.train[i]);
            RDA rda = new RDA(trainx, trainy, 0.6);
            if (y[loocv.test[i]] != rda.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("RDA (0.6) error = " + error);
        assertEquals(11, error);
        error = 0;
        for (int i = 0; i < n; i++) {
            double[][] trainx = Math.slice(x, loocv.train[i]);
            int[] trainy = Math.slice(y, loocv.train[i]);
            RDA rda = new RDA(trainx, trainy, 0.7);
            if (y[loocv.test[i]] != rda.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("RDA (0.7) error = " + error);
        assertEquals(9, error);
        error = 0;
        double[] posteriori = new double[3];
        for (int i = 0; i < n; i++) {
            double[][] trainx = Math.slice(x, loocv.train[i]);
            int[] trainy = Math.slice(y, loocv.train[i]);
            RDA rda = new RDA(trainx, trainy, 0.8);
            if (y[loocv.test[i]] != rda.predict(x[loocv.test[i]], posteriori))
                error++;
        //System.out.println(posteriori[0]+"\t"+posteriori[1]+"\t"+posteriori[2]);
        }
        System.out.println("RDA (0.8) error = " + error);
        assertEquals(6, error);
        error = 0;
        for (int i = 0; i < n; i++) {
            double[][] trainx = Math.slice(x, loocv.train[i]);
            int[] trainy = Math.slice(y, loocv.train[i]);
            RDA rda = new RDA(trainx, trainy, 0.9);
            if (y[loocv.test[i]] != rda.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("RDA (0.9) error = " + error);
        assertEquals(3, error);
        error = 0;
        for (int i = 0; i < n; i++) {
            double[][] trainx = Math.slice(x, loocv.train[i]);
            int[] trainy = Math.slice(y, loocv.train[i]);
            RDA rda = new RDA(trainx, trainy, 1.0);
            if (y[loocv.test[i]] != rda.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("RDA (1.0) error = " + error);
        assertEquals(4, error);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) LOOCV(smile.validation.LOOCV) Test(org.junit.Test)

Example 39 with AttributeDataset

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

the class RandomForestTest method testUSPSNominal.

/**
     * Test of learn method, of class RandomForest.
     */
@Test
public void testUSPSNominal() {
    System.out.println("USPS nominal");
    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()]);
        for (double[] xi : x) {
            for (int i = 0; i < xi.length; i++) {
                xi[i] = Math.round(255 * (xi[i] + 1) / 2);
            }
        }
        for (double[] xi : testx) {
            for (int i = 0; i < xi.length; i++) {
                xi[i] = Math.round(255 * (xi[i] + 1) / 2);
            }
        }
        Attribute[] attributes = new Attribute[256];
        String[] values = new String[attributes.length];
        for (int i = 0; i < attributes.length; i++) {
            values[i] = String.valueOf(i);
        }
        for (int i = 0; i < attributes.length; i++) {
            attributes[i] = new NominalAttribute("V" + i, values);
        }
        RandomForest forest = new RandomForest(attributes, 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);
        double[] accuracy = forest.test(testx, testy);
        for (int i = 1; i <= accuracy.length; i++) {
            System.out.format("%d trees accuracy = %.2f%%%n", i, 100.0 * accuracy[i - 1]);
        }
        double[] importance = forest.importance();
        int[] index = QuickSort.sort(importance);
        for (int i = importance.length; i-- > 0; ) {
            System.out.format("%s importance is %.4f%n", train.attributes()[index[i]], importance[i]);
        }
        assertTrue(error <= 150);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) Attribute(smile.data.Attribute) NominalAttribute(smile.data.NominalAttribute) NominalAttribute(smile.data.NominalAttribute) Test(org.junit.Test)

Example 40 with AttributeDataset

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

the class RandomForestTest method testIris.

/**
     * Test of learn method, of class RandomForest.
     */
@Test
public void testIris() {
    System.out.println("Iris");
    ArffParser arffParser = new ArffParser();
    arffParser.setResponseIndex(4);
    try {
        AttributeDataset iris = arffParser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/iris.arff"));
        double[][] x = iris.toArray(new double[iris.size()][]);
        int[] y = iris.toArray(new int[iris.size()]);
        int n = x.length;
        LOOCV loocv = new LOOCV(n);
        int error = 0;
        for (int i = 0; i < n; i++) {
            double[][] trainx = Math.slice(x, loocv.train[i]);
            int[] trainy = Math.slice(y, loocv.train[i]);
            RandomForest forest = new RandomForest(iris.attributes(), trainx, trainy, 100);
            if (y[loocv.test[i]] != forest.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("Random Forest error = " + error);
        assertTrue(error <= 9);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) LOOCV(smile.validation.LOOCV) Test(org.junit.Test)

Aggregations

AttributeDataset (smile.data.AttributeDataset)140 Test (org.junit.Test)125 ArffParser (smile.data.parser.ArffParser)75 NominalAttribute (smile.data.NominalAttribute)50 DelimitedTextParser (smile.data.parser.DelimitedTextParser)48 Attribute (smile.data.Attribute)29 EuclideanDistance (smile.math.distance.EuclideanDistance)19 LOOCV (smile.validation.LOOCV)18 CrossValidation (smile.validation.CrossValidation)17 AdjustedRandIndex (smile.validation.AdjustedRandIndex)14 RandIndex (smile.validation.RandIndex)14 ClassifierTrainer (smile.classification.ClassifierTrainer)13 GaussianKernel (smile.math.kernel.GaussianKernel)11 IOException (java.io.IOException)10 RadialBasisFunction (smile.math.rbf.RadialBasisFunction)9 RBFNetwork (smile.regression.RBFNetwork)8 ArrayList (java.util.ArrayList)6 KMeans (smile.clustering.KMeans)6 Datum (smile.data.Datum)6 NumericAttribute (smile.data.NumericAttribute)6