Search in sources :

Example 11 with CrossValidation

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

the class NaiveBayesTest method testLearnMultinomial2.

/**
     * Test of learn method, of class SequenceNaiveBayes.
     */
@Test
public void testLearnMultinomial2() {
    System.out.println("online learn Multinomial");
    double[][] x = moviex;
    int[] y = moviey;
    int n = x.length;
    int k = 10;
    CrossValidation cv = new CrossValidation(n, k);
    int error = 0;
    int total = 0;
    for (int i = 0; i < k; i++) {
        double[][] trainx = Math.slice(x, cv.train[i]);
        int[] trainy = Math.slice(y, cv.train[i]);
        NaiveBayes bayes = new NaiveBayes(NaiveBayes.Model.MULTINOMIAL, 2, feature.length);
        for (int j = 0; j < trainx.length; j++) {
            bayes.learn(trainx[j], trainy[j]);
        }
        double[][] testx = Math.slice(x, cv.test[i]);
        int[] testy = Math.slice(y, cv.test[i]);
        for (int j = 0; j < testx.length; j++) {
            int label = bayes.predict(testx[j]);
            if (label != -1) {
                total++;
                if (testy[j] != label) {
                    error++;
                }
            }
        }
    }
    System.out.format("Multinomial error = %d of %d%n", error, total);
    assertTrue(error < 265);
}
Also used : CrossValidation(smile.validation.CrossValidation) Test(org.junit.Test)

Example 12 with CrossValidation

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

the class NaiveBayesTest method testLearnBernoulli.

/**
     * Test of learn method, of class SequenceNaiveBayes.
     */
@Test
public void testLearnBernoulli() {
    System.out.println("batch learn Bernoulli");
    double[][] x = moviex;
    int[] y = moviey;
    int n = x.length;
    int k = 10;
    CrossValidation cv = new CrossValidation(n, k);
    int error = 0;
    int total = 0;
    for (int i = 0; i < k; i++) {
        double[][] trainx = Math.slice(x, cv.train[i]);
        int[] trainy = Math.slice(y, cv.train[i]);
        NaiveBayes bayes = new NaiveBayes(NaiveBayes.Model.BERNOULLI, 2, feature.length);
        bayes.learn(trainx, trainy);
        double[][] testx = Math.slice(x, cv.test[i]);
        int[] testy = Math.slice(y, cv.test[i]);
        for (int j = 0; j < testx.length; j++) {
            int label = bayes.predict(testx[j]);
            if (label != -1) {
                total++;
                if (testy[j] != label) {
                    error++;
                }
            }
        }
    }
    System.out.format("Bernoulli error = %d of %d%n", error, total);
    assertTrue(error < 270);
}
Also used : CrossValidation(smile.validation.CrossValidation) Test(org.junit.Test)

Example 13 with CrossValidation

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

the class HMMPOSTaggerTest method testBrown.

/**
     * Test of learn method, of class HMMPOSTagger.
     */
@Test
public void testBrown() {
    System.out.println("BROWN");
    load("D:\\sourceforge\\corpora\\PennTreebank\\PennTreebank2\\TAGGED\\POS\\BROWN");
    String[][] x = sentences.toArray(new String[sentences.size()][]);
    PennTreebankPOS[][] y = labels.toArray(new PennTreebankPOS[labels.size()][]);
    int n = x.length;
    int k = 10;
    CrossValidation cv = new CrossValidation(n, k);
    int error = 0;
    int total = 0;
    for (int i = 0; i < k; i++) {
        String[][] trainx = Math.slice(x, cv.train[i]);
        PennTreebankPOS[][] trainy = Math.slice(y, cv.train[i]);
        String[][] testx = Math.slice(x, cv.test[i]);
        PennTreebankPOS[][] testy = Math.slice(y, cv.test[i]);
        HMMPOSTagger tagger = HMMPOSTagger.learn(trainx, trainy);
        for (int j = 0; j < testx.length; j++) {
            PennTreebankPOS[] label = tagger.tag(testx[j]);
            total += label.length;
            for (int l = 0; l < label.length; l++) {
                if (label[l] != testy[j][l]) {
                    error++;
                }
            }
        }
    }
    System.out.format("Error rate = %.2f as %d of %d\n", 100.0 * error / total, error, total);
}
Also used : CrossValidation(smile.validation.CrossValidation) Test(org.junit.Test)

Example 14 with CrossValidation

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

the class HMMPOSTaggerTest method testWSJ.

/**
     * Test of learn method, of class HMMPOSTagger.
     */
@Test
public void testWSJ() {
    System.out.println("WSJ");
    load("D:\\sourceforge\\corpora\\PennTreebank\\PennTreebank2\\TAGGED\\POS\\WSJ");
    String[][] x = sentences.toArray(new String[sentences.size()][]);
    PennTreebankPOS[][] y = labels.toArray(new PennTreebankPOS[labels.size()][]);
    int n = x.length;
    int k = 10;
    CrossValidation cv = new CrossValidation(n, k);
    int error = 0;
    int total = 0;
    for (int i = 0; i < k; i++) {
        String[][] trainx = Math.slice(x, cv.train[i]);
        PennTreebankPOS[][] trainy = Math.slice(y, cv.train[i]);
        String[][] testx = Math.slice(x, cv.test[i]);
        PennTreebankPOS[][] testy = Math.slice(y, cv.test[i]);
        HMMPOSTagger tagger = HMMPOSTagger.learn(trainx, trainy);
        for (int j = 0; j < testx.length; j++) {
            PennTreebankPOS[] label = tagger.tag(testx[j]);
            total += label.length;
            for (int l = 0; l < label.length; l++) {
                if (label[l] != testy[j][l]) {
                    error++;
                }
            }
        }
    }
    System.out.format("Error rate = %.2f as %d of %d\n", 100.0 * error / total, error, total);
}
Also used : CrossValidation(smile.validation.CrossValidation) Test(org.junit.Test)

Example 15 with CrossValidation

use of smile.validation.CrossValidation 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)

Aggregations

CrossValidation (smile.validation.CrossValidation)23 Test (org.junit.Test)20 AttributeDataset (smile.data.AttributeDataset)17 ArffParser (smile.data.parser.ArffParser)17 KMeans (smile.clustering.KMeans)6 GaussianKernel (smile.math.kernel.GaussianKernel)6 EuclideanDistance (smile.math.distance.EuclideanDistance)4 RadialBasisFunction (smile.math.rbf.RadialBasisFunction)4 PolynomialKernel (smile.math.kernel.PolynomialKernel)1