Search in sources :

Example 21 with LOOCV

use of smile.validation.LOOCV in project smile by haifengl.

the class RBFNetworkTest method testLearn.

/**
     * Test of learn method, of class RBFNetwork.
     */
@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]);
            double[][] centers = new double[10][];
            RadialBasisFunction[] basis = SmileUtils.learnGaussianRadialBasis(trainx, centers, 5.0);
            RBFNetwork<double[]> rbf = new RBFNetwork<>(trainx, trainy, new EuclideanDistance(), basis, centers);
            if (y[loocv.test[i]] != rbf.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("RBF network error = " + error);
        assertTrue(error <= 6);
    } 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) LOOCV(smile.validation.LOOCV) Test(org.junit.Test)

Example 22 with LOOCV

use of smile.validation.LOOCV in project smile by haifengl.

the class RandomForestTest method testWeather.

/**
     * Test of learn method, of class RandomForest.
     */
@Test
public void testWeather() {
    System.out.println("Weather");
    ArffParser arffParser = new ArffParser();
    arffParser.setResponseIndex(4);
    try {
        AttributeDataset weather = arffParser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/weather.nominal.arff"));
        double[][] x = weather.toArray(new double[weather.size()][]);
        int[] y = weather.toArray(new int[weather.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(weather.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 <= 7);
    } 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 23 with LOOCV

use of smile.validation.LOOCV in project smile by haifengl.

the class AdaBoostTest method testWeather.

/**
     * Test of learn method, of class AdaBoost.
     */
@Test
public void testWeather() {
    System.out.println("Weather");
    ArffParser arffParser = new ArffParser();
    arffParser.setResponseIndex(4);
    try {
        AttributeDataset weather = arffParser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/weather.nominal.arff"));
        double[][] x = weather.toArray(new double[weather.size()][]);
        int[] y = weather.toArray(new int[weather.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]);
            AdaBoost forest = new AdaBoost(weather.attributes(), trainx, trainy, 200, 4);
            if (y[loocv.test[i]] != forest.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("AdaBoost error = " + error);
        assertEquals(3, 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 24 with LOOCV

use of smile.validation.LOOCV in project smile by haifengl.

the class LogisticRegressionTest method testIris2.

/**
     * Test of learn method, of class LogisticRegression.
     */
@Test
public void testIris2() {
    System.out.println("Iris binary");
    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()]);
        for (int i = 0; i < y.length; i++) {
            if (y[i] == 2) {
                y[i] = 1;
            } else {
                y[i] = 0;
            }
        }
        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]);
            LogisticRegression logit = new LogisticRegression(trainx, trainy);
            if (y[loocv.test[i]] != logit.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("Logistic Regression error = " + error);
        assertEquals(3, 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 25 with LOOCV

use of smile.validation.LOOCV in project smile by haifengl.

the class LogisticRegressionTest method testIris.

/**
     * Test of learn method, of class LogisticRegression.
     */
@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]);
            LogisticRegression logit = new LogisticRegression(trainx, trainy);
            if (y[loocv.test[i]] != logit.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("Logistic Regression error = " + error);
        assertEquals(3, 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)

Aggregations

Test (org.junit.Test)26 LOOCV (smile.validation.LOOCV)26 AttributeDataset (smile.data.AttributeDataset)18 ArffParser (smile.data.parser.ArffParser)18 EuclideanDistance (smile.math.distance.EuclideanDistance)2 RadialBasisFunction (smile.math.rbf.RadialBasisFunction)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 GaussianKernel (smile.math.kernel.GaussianKernel)1 Distribution (smile.stat.distribution.Distribution)1 GaussianMixture (smile.stat.distribution.GaussianMixture)1