Search in sources :

Example 16 with ArffParser

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

the class FeatureSetTest method testF.

/**
     * Test of f method, of class FeatureSet.
     */
@Test
public void testF() {
    System.out.println("f");
    try {
        ArffParser parser = new ArffParser();
        AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/abalone.arff"));
        double[][] x = data.toArray(new double[data.size()][]);
        FeatureSet<double[]> features = new FeatureSet<>();
        features.add(new Nominal2Binary(data.attributes()));
        features.add(new NumericAttributeFeature(data.attributes(), 0.05, 0.95, x));
        AttributeDataset dataset = features.f(data);
        assertEquals(data.size(), dataset.size());
        assertEquals(data.getName(), dataset.getName());
        assertEquals(data.getDescription(), dataset.getDescription());
        Attribute[] attributes = features.attributes();
        for (int i = 0; i < attributes.length; i++) {
            assertEquals(attributes[i].getName(), dataset.attributes()[i].getName());
            assertEquals(attributes[i].getType(), dataset.attributes()[i].getType());
        }
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) Attribute(smile.data.Attribute) Test(org.junit.Test)

Example 17 with ArffParser

use of smile.data.parser.ArffParser 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 18 with ArffParser

use of smile.data.parser.ArffParser 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 19 with ArffParser

use of smile.data.parser.ArffParser 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 20 with ArffParser

use of smile.data.parser.ArffParser 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)75 ArffParser (smile.data.parser.ArffParser)75 Test (org.junit.Test)71 LOOCV (smile.validation.LOOCV)18 CrossValidation (smile.validation.CrossValidation)17 EuclideanDistance (smile.math.distance.EuclideanDistance)14 ClassifierTrainer (smile.classification.ClassifierTrainer)12 GaussianKernel (smile.math.kernel.GaussianKernel)10 Attribute (smile.data.Attribute)8 RadialBasisFunction (smile.math.rbf.RadialBasisFunction)8 RBFNetwork (smile.regression.RBFNetwork)8 KMeans (smile.clustering.KMeans)6 IOException (java.io.IOException)3 DecisionTree (smile.classification.DecisionTree)2 NominalAttribute (smile.data.NominalAttribute)2 PolynomialKernel (smile.math.kernel.PolynomialKernel)2 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 LinearKernel (smile.math.kernel.LinearKernel)1 Distribution (smile.stat.distribution.Distribution)1