Search in sources :

Example 56 with ArffParser

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

the class ValidationTest method testBootstrap_4args_2.

/**
     * Test of bootstrap method, of class Validation.
     */
@Test
public void testBootstrap_4args_2() {
    System.out.println("bootstrap");
    ArffParser parser = new ArffParser();
    parser.setResponseIndex(6);
    try {
        AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/cpu.arff"));
        double[] y = data.toArray(new double[data.size()]);
        double[][] x = data.toArray(new double[data.size()][]);
        Math.standardize(x);
        RBFNetwork.Trainer<double[]> trainer = new RBFNetwork.Trainer<>(new EuclideanDistance());
        trainer.setNumCenters(20);
        double[] rmse = Validation.bootstrap(100, trainer, x, y);
        System.out.println("100-fold bootstrap RMSE average = " + Math.mean(rmse));
        System.out.println("100-fold bootstrap RMSE std.dev = " + Math.sd(rmse));
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : EuclideanDistance(smile.math.distance.EuclideanDistance) ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) ClassifierTrainer(smile.classification.ClassifierTrainer) RBFNetwork(smile.regression.RBFNetwork) Test(org.junit.Test)

Example 57 with ArffParser

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

the class DecisionTreeTest method testWeather.

/**
     * Test of learn method, of class DecisionTree.
     */
@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]);
            DecisionTree tree = new DecisionTree(weather.attributes(), trainx, trainy, 3);
            if (y[loocv.test[i]] != tree.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("Decision Tree error = " + error);
        assertEquals(5, 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 58 with ArffParser

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

the class GradientTreeBoostTest method testIris2.

/**
     * Test of predict method, of class GradientTreeBoost.
     */
@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]);
            GradientTreeBoost boost = new GradientTreeBoost(iris.attributes(), trainx, trainy, 100);
            if (y[loocv.test[i]] != boost.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("Gradient Tree Boost error = " + error);
    //assertEquals(6, 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 59 with ArffParser

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

the class GradientTreeBoostTest method testIris.

/**
     * Test of learn method, of class GradientTreeBoost.
     */
@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]);
            GradientTreeBoost boost = new GradientTreeBoost(iris.attributes(), trainx, trainy, 100);
            if (y[loocv.test[i]] != boost.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("Gradient Tree Boost error = " + error);
    //assertEquals(6, 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 60 with ArffParser

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

the class KNNTest method testLearn_3args.

/**
     * Test of learn method, of class KNN.
     */
@Test
public void testLearn_3args() {
    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[0][]);
        int[] y = iris.toArray(new int[0]);
        KNN<double[]> knn = KNN.learn(x, y, 1);
        int error = 0;
        for (int i = 0; i < x.length; i++) {
            if (knn.predict(x[i]) != y[i]) {
                error++;
            }
        }
        System.out.println("1-nn error = " + error);
        assertEquals(6, error);
        knn = KNN.learn(x, y, 3);
        error = 0;
        for (int i = 0; i < x.length; i++) {
            if (knn.predict(x[i]) != y[i]) {
                error++;
            }
        }
        System.out.println("3-nn error = " + error);
        assertEquals(6, error);
        knn = KNN.learn(x, y, 5);
        error = 0;
        for (int i = 0; i < x.length; i++) {
            if (knn.predict(x[i]) != y[i]) {
                error++;
            }
        }
        System.out.println("5-nn error = " + error);
        assertEquals(5, error);
        knn = KNN.learn(x, y, 7);
        error = 0;
        for (int i = 0; i < x.length; i++) {
            if (knn.predict(x[i]) != y[i]) {
                error++;
            }
        }
        System.out.println("7-nn error = " + error);
        assertEquals(5, error);
        knn = KNN.learn(x, y, 9);
        error = 0;
        for (int i = 0; i < x.length; i++) {
            if (knn.predict(x[i]) != y[i]) {
                error++;
            }
        }
        System.out.println("9-nn error = " + error);
        assertEquals(5, error);
        knn = KNN.learn(x, y, 11);
        error = 0;
        for (int i = 0; i < x.length; i++) {
            if (knn.predict(x[i]) != y[i]) {
                error++;
            }
        }
        System.out.println("11-nn error = " + error);
        assertEquals(4, error);
        knn = KNN.learn(x, y, 13);
        error = 0;
        for (int i = 0; i < x.length; i++) {
            if (knn.predict(x[i]) != y[i]) {
                error++;
            }
        }
        System.out.println("13-nn error = " + error);
        assertEquals(5, error);
        knn = KNN.learn(x, y, 15);
        error = 0;
        for (int i = 0; i < x.length; i++) {
            if (knn.predict(x[i]) != y[i]) {
                error++;
            }
        }
        System.out.println("15-nn error = " + error);
        assertEquals(4, error);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) IOException(java.io.IOException) ParseException(java.text.ParseException) 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