Search in sources :

Example 1 with ArffParser

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

the class ValidationTest method testCv_5args_1.

/**
     * Test of cv method, of class Validation.
     */
@Test
public void testCv_5args_1() {
    System.out.println("cv");
    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()]);
        ClassifierTrainer<double[]> trainer = new LDA.Trainer();
        ClassificationMeasure[] measures = { new Accuracy() };
        double[] results = Validation.cv(10, trainer, x, y, measures);
        for (int i = 0; i < measures.length; i++) {
            System.out.println(measures[i] + " = " + results[i]);
        }
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) ClassifierTrainer(smile.classification.ClassifierTrainer) Test(org.junit.Test)

Example 2 with ArffParser

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

the class ValidationTest method testCv_4args_1.

/**
     * Test of cv method, of class Validation.
     */
@Test
public void testCv_4args_1() {
    System.out.println("cv");
    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()]);
        ClassifierTrainer<double[]> trainer = new LDA.Trainer();
        double accuracy = Validation.cv(10, trainer, x, y);
        System.out.println("10-fold CV accuracy = " + accuracy);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) ClassifierTrainer(smile.classification.ClassifierTrainer) Test(org.junit.Test)

Example 3 with ArffParser

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

the class ValidationTest method testTest_4args_2.

/**
     * Test of test method, of class Validation.
     */
@Test
public void testTest_4args_2() {
    System.out.println("test");
    ArffParser parser = new ArffParser();
    parser.setResponseIndex(6);
    try {
        AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/cpu.arff"));
        double[] datay = data.toArray(new double[data.size()]);
        double[][] datax = data.toArray(new double[data.size()][]);
        Math.standardize(datax);
        int n = datax.length;
        int m = 3 * n / 4;
        double[][] x = new double[m][];
        double[] y = new double[m];
        double[][] testx = new double[n - m][];
        double[] testy = new double[n - m];
        int[] index = Math.permutate(n);
        for (int i = 0; i < m; i++) {
            x[i] = datax[index[i]];
            y[i] = datay[index[i]];
        }
        for (int i = m; i < n; i++) {
            testx[i - m] = datax[index[i]];
            testy[i - m] = datay[index[i]];
        }
        double[][] centers = new double[20][];
        RadialBasisFunction[] rbf = SmileUtils.learnGaussianRadialBasis(x, centers, 2);
        RBFNetwork<double[]> rkhs = new RBFNetwork<>(x, y, new EuclideanDistance(), rbf, centers);
        RegressionMeasure[] measures = { new RMSE(), new AbsoluteDeviation() };
        double[] results = Validation.test(rkhs, testx, testy, measures);
        System.out.println("RMSE = " + results[0]);
        System.out.println("Absolute Deviation = " + results[1]);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : RadialBasisFunction(smile.math.rbf.RadialBasisFunction) AttributeDataset(smile.data.AttributeDataset) RBFNetwork(smile.regression.RBFNetwork) EuclideanDistance(smile.math.distance.EuclideanDistance) ArffParser(smile.data.parser.ArffParser) Test(org.junit.Test)

Example 4 with ArffParser

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

the class ValidationTest method testLoocv_4args_2.

/**
     * Test of loocv method, of class Validation.
     */
@Test
public void testLoocv_4args_2() {
    System.out.println("loocv");
    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);
        RegressionMeasure[] measures = { new RMSE(), new AbsoluteDeviation() };
        double[] results = Validation.loocv(trainer, x, y, measures);
        System.out.println("RMSE = " + results[0]);
        System.out.println("Absolute Deviation = " + results[1]);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : AttributeDataset(smile.data.AttributeDataset) ClassifierTrainer(smile.classification.ClassifierTrainer) RBFNetwork(smile.regression.RBFNetwork) EuclideanDistance(smile.math.distance.EuclideanDistance) ArffParser(smile.data.parser.ArffParser) Test(org.junit.Test)

Example 5 with ArffParser

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

the class ValidationTest method testBootstrap_4args_1.

/**
     * Test of bootstrap method, of class Validation.
     */
@Test
public void testBootstrap_4args_1() {
    System.out.println("bootstrap");
    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()]);
        ClassifierTrainer<double[]> trainer = new LDA.Trainer();
        double[] accuracy = Validation.bootstrap(100, trainer, x, y);
        System.out.println("100-fold bootstrap accuracy average = " + Math.mean(accuracy));
        System.out.println("100-fold bootstrap accuracy std.dev = " + Math.sd(accuracy));
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) ClassifierTrainer(smile.classification.ClassifierTrainer) 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