Search in sources :

Example 36 with ArffParser

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

the class RegressionTreeTest method test.

public void test(String dataset, String url, int response) {
    System.out.println(dataset);
    ArffParser parser = new ArffParser();
    parser.setResponseIndex(response);
    try {
        AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile(url));
        double[] datay = data.toArray(new double[data.size()]);
        double[][] datax = data.toArray(new double[data.size()][]);
        int n = datax.length;
        int k = 10;
        CrossValidation cv = new CrossValidation(n, k);
        double rss = 0.0;
        double ad = 0.0;
        for (int i = 0; i < k; i++) {
            double[][] trainx = Math.slice(datax, cv.train[i]);
            double[] trainy = Math.slice(datay, cv.train[i]);
            double[][] testx = Math.slice(datax, cv.test[i]);
            double[] testy = Math.slice(datay, cv.test[i]);
            RegressionTree tree = new RegressionTree(data.attributes(), trainx, trainy, 20);
            for (int j = 0; j < testx.length; j++) {
                double r = testy[j] - tree.predict(testx[j]);
                rss += r * r;
                ad += Math.abs(r);
            }
        }
        System.out.format("10-CV RMSE = %.4f \t AbsoluteDeviation = %.4f%n", Math.sqrt(rss / n), ad / n);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) CrossValidation(smile.validation.CrossValidation)

Example 37 with ArffParser

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

the class RegressionTreeTest method testCPU.

/**
     * Test of learn method, of class RegressionTree.
     */
@Test
public void testCPU() {
    System.out.println("CPU");
    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()][]);
        int n = datax.length;
        int m = 3 * n / 4;
        int[] index = Math.permutate(n);
        double[][] trainx = new double[m][];
        double[] trainy = new double[m];
        for (int i = 0; i < m; i++) {
            trainx[i] = datax[index[i]];
            trainy[i] = datay[index[i]];
        }
        double[][] testx = new double[n - m][];
        double[] testy = new double[n - m];
        for (int i = m; i < n; i++) {
            testx[i - m] = datax[index[i]];
            testy[i - m] = datay[index[i]];
        }
        RegressionTree tree = new RegressionTree(data.attributes(), trainx, trainy, 20);
        System.out.format("RMSE = %.4f%n", Validation.test(tree, testx, testy));
        double[] importance = tree.importance();
        index = QuickSort.sort(importance);
        for (int i = importance.length; i-- > 0; ) {
            System.out.format("%s importance is %.4f%n", data.attributes()[index[i]], importance[i]);
        }
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) Test(org.junit.Test)

Example 38 with ArffParser

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

the class ValidationTest method testCv_4args_2.

/**
     * Test of cv method, of class Validation.
     */
@Test
public void testCv_4args_2() {
    System.out.println("cv");
    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.cv(10, trainer, x, y);
        System.out.println("RMSE = " + 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 39 with ArffParser

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

the class ValidationTest method testTest_3args_2.

/**
     * Test of test method, of class Validation.
     */
@Test
public void testTest_3args_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);
        double rmse = Validation.test(rkhs, testx, testy);
        System.out.println("RMSE = " + rmse);
    } 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 40 with ArffParser

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

the class NumericAttributeFeatureTest method testROBUSTSTANDARDIZATION.

/**
     * Test of f method, of class NumericAttributeFeature.
     */
@Test
public void testROBUSTSTANDARDIZATION() {
    System.out.println("ROBUST STANDARDIZATION");
    ArffParser parser = new ArffParser();
    try {
        AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/abalone.arff"));
        double[][] x = data.toArray(new double[data.size()][]);
        NumericAttributeFeature naf = new NumericAttributeFeature(data.attributes(), x);
        Attribute[] attributes = naf.attributes();
        assertEquals(data.attributes().length - 1, attributes.length);
        for (int i = 0; i < x.length; i++) {
            double[] y = new double[attributes.length];
            for (int j = 0; j < y.length; j++) {
                y[j] = naf.f(x[i], j);
            }
        }
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) Attribute(smile.data.Attribute) 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