Search in sources :

Example 26 with ArffParser

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

the class RandomForestTest 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]);
            RandomForest forest = new RandomForest(data.attributes(), trainx, trainy, 200, n, 5, trainx[0].length / 3);
            System.out.format("OOB error rate = %.4f%n", forest.error());
            for (int j = 0; j < testx.length; j++) {
                double r = testy[j] - forest.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 27 with ArffParser

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

the class NaiveBayesTest method testPredict.

/**
     * Test of predict method, of class NaiveBayes.
     */
@Test
public void testPredict() {
    System.out.println("predict");
    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 l = 0; l < n; l++) {
            double[][] trainx = Math.slice(x, loocv.train[l]);
            int[] trainy = Math.slice(y, loocv.train[l]);
            int p = trainx[0].length;
            int k = Math.max(trainy) + 1;
            double[] priori = new double[k];
            Distribution[][] condprob = new Distribution[k][p];
            for (int i = 0; i < k; i++) {
                priori[i] = 1.0 / k;
                for (int j = 0; j < p; j++) {
                    ArrayList<Double> axi = new ArrayList<>();
                    for (int m = 0; m < trainx.length; m++) {
                        if (trainy[m] == i) {
                            axi.add(trainx[m][j]);
                        }
                    }
                    double[] xi = new double[axi.size()];
                    for (int m = 0; m < xi.length; m++) {
                        xi[m] = axi.get(m);
                    }
                    condprob[i][j] = new GaussianMixture(xi, 3);
                }
            }
            NaiveBayes bayes = new NaiveBayes(priori, condprob);
            if (y[loocv.test[l]] != bayes.predict(x[loocv.test[l]]))
                error++;
        }
        System.out.format("Iris error rate = %.2f%%%n", 100.0 * error / x.length);
        assertEquals(5, error);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : AttributeDataset(smile.data.AttributeDataset) ArrayList(java.util.ArrayList) GaussianMixture(smile.stat.distribution.GaussianMixture) LOOCV(smile.validation.LOOCV) IOException(java.io.IOException) ArffParser(smile.data.parser.ArffParser) Distribution(smile.stat.distribution.Distribution) Test(org.junit.Test)

Example 28 with ArffParser

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

the class NeuralNetworkTest method testIris2.

/**
     * Test of learn method, of class NeuralNetwork.
     */
@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;
        int p = x[0].length;
        double[] mu = Math.colMean(x);
        double[] sd = Math.colSd(x);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < p; j++) {
                x[i][j] = (x[i][j] - mu[j]) / sd[j];
            }
        }
        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]);
            NeuralNetwork net = new NeuralNetwork(NeuralNetwork.ErrorFunction.CROSS_ENTROPY, NeuralNetwork.ActivationFunction.LOGISTIC_SIGMOID, x[0].length, 10, 1);
            for (int j = 0; j < 30; j++) {
                net.learn(trainx, trainy);
            }
            if (y[loocv.test[i]] != net.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("Neural network error = " + error);
        assertTrue(error <= 8);
    } 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 29 with ArffParser

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

the class NeuralNetworkTest method testSegmentLMS.

/**
     * Test of learn method, of class NeuralNetwork.
     */
@Test
public void testSegmentLMS() {
    System.out.println("Segment LMS");
    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]);
        int p = x[0].length;
        double[] mu = Math.colMin(x);
        double[] sd = Math.colMax(x);
        for (int i = 0; i < x.length; i++) {
            for (int j = 0; j < p; j++) {
                x[i][j] = (x[i][j] - mu[j]) / sd[j];
            }
        }
        for (int i = 0; i < testx.length; i++) {
            for (int j = 0; j < p; j++) {
                testx[i][j] = (testx[i][j] - mu[j]) / sd[j];
            }
        }
        NeuralNetwork net = new NeuralNetwork(NeuralNetwork.ErrorFunction.LEAST_MEAN_SQUARES, NeuralNetwork.ActivationFunction.LOGISTIC_SIGMOID, x[0].length, 30, Math.max(y) + 1);
        for (int j = 0; j < 30; j++) {
            net.learn(x, y);
        }
        int error = 0;
        for (int i = 0; i < testx.length; i++) {
            if (net.predict(testx[i]) != testy[i]) {
                error++;
            }
        }
        System.out.format("Segment error rate = %.2f%%%n", 100.0 * error / testx.length);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) Test(org.junit.Test)

Example 30 with ArffParser

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

the class DecisionTreeTest method testIris.

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

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