Search in sources :

Example 21 with CrossValidation

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

the class RBFNetworkTest method testCPU.

/**
     * Test of learn method, of class RBFNetwork.
     */
@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()][]);
        Math.standardize(datax);
        int n = datax.length;
        int k = 10;
        CrossValidation cv = new CrossValidation(n, k);
        double rss = 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]);
            double[][] centers = new double[20][];
            RadialBasisFunction[] basis = SmileUtils.learnGaussianRadialBasis(trainx, centers, 5.0);
            RBFNetwork<double[]> rbf = new RBFNetwork<>(trainx, trainy, new EuclideanDistance(), basis, centers);
            for (int j = 0; j < testx.length; j++) {
                double r = testy[j] - rbf.predict(testx[j]);
                rss += r * r;
            }
        }
        System.out.println("10-CV MSE = " + rss / n);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : RadialBasisFunction(smile.math.rbf.RadialBasisFunction) AttributeDataset(smile.data.AttributeDataset) EuclideanDistance(smile.math.distance.EuclideanDistance) ArffParser(smile.data.parser.ArffParser) CrossValidation(smile.validation.CrossValidation) Test(org.junit.Test)

Example 22 with CrossValidation

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

the class RBFNetworkTest method test2DPlanes.

/**
     * Test of learn method, of class RBFNetwork.
     */
@Test
public void test2DPlanes() {
    System.out.println("2dplanes");
    ArffParser parser = new ArffParser();
    parser.setResponseIndex(10);
    try {
        AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/2dplanes.arff"));
        double[] datay = data.toArray(new double[data.size()]);
        double[][] datax = data.toArray(new double[data.size()][]);
        //Math.normalize(datax);
        int n = datax.length;
        int k = 10;
        CrossValidation cv = new CrossValidation(n, k);
        double rss = 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]);
            double[][] centers = new double[20][];
            RadialBasisFunction[] basis = SmileUtils.learnGaussianRadialBasis(trainx, centers, 5.0);
            RBFNetwork<double[]> rbf = new RBFNetwork<>(trainx, trainy, new EuclideanDistance(), basis, centers);
            for (int j = 0; j < testx.length; j++) {
                double r = testy[j] - rbf.predict(testx[j]);
                rss += r * r;
            }
        }
        System.out.println("10-CV MSE = " + rss / n);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : RadialBasisFunction(smile.math.rbf.RadialBasisFunction) AttributeDataset(smile.data.AttributeDataset) EuclideanDistance(smile.math.distance.EuclideanDistance) ArffParser(smile.data.parser.ArffParser) CrossValidation(smile.validation.CrossValidation) Test(org.junit.Test)

Example 23 with CrossValidation

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

the class NaiveBayesTest method testLearnMultinomial.

/**
     * Test of learn method, of class SequenceNaiveBayes.
     */
@Test
public void testLearnMultinomial() {
    System.out.println("batch 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);
        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("Multinomial error = %d of %d%n", error, total);
    assertTrue(error < 265);
}
Also used : CrossValidation(smile.validation.CrossValidation) Test(org.junit.Test)

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