Search in sources :

Example 1 with CrossValidation

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

the class RidgeRegressionTest method testCPU.

/**
     * Test of learn method, of class LinearRegression.
     */
@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[][] datax = data.toArray(new double[data.size()][]);
        double[] datay = data.toArray(new double[data.size()]);
        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]);
            RidgeRegression ridge = new RidgeRegression(trainx, trainy, 10.0);
            for (int j = 0; j < testx.length; j++) {
                double r = testy[j] - ridge.predict(testx[j]);
                rss += r * r;
            }
        }
        System.out.println("10-CV MSE = " + rss / n);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) CrossValidation(smile.validation.CrossValidation) Test(org.junit.Test)

Example 2 with CrossValidation

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

the class SVRTest method testCPU.

/**
     * Test of learn method, of class SVR.
     */
@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]);
            SVR<double[]> svr = new SVR<>(trainx, trainy, new PolynomialKernel(3, 1.0, 1.0), 0.1, 1.0);
            for (int j = 0; j < testx.length; j++) {
                double r = testy[j] - svr.predict(testx[j]);
                rss += r * r;
            }
        }
        System.out.println("10-CV RMSE = " + Math.sqrt(rss / n));
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : PolynomialKernel(smile.math.kernel.PolynomialKernel) ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) CrossValidation(smile.validation.CrossValidation) Test(org.junit.Test)

Example 3 with CrossValidation

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

the class GaussianProcessRegressionTest method testKin8nm.

/**
     * Test of learn method, of class GaussianProcessRegression.
     */
@Test
public void testKin8nm() {
    System.out.println("kin8nm");
    ArffParser parser = new ArffParser();
    parser.setResponseIndex(8);
    try {
        AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/kin8nm.arff"));
        double[] y = data.toArray(new double[data.size()]);
        double[][] x = data.toArray(new double[data.size()][]);
        int[] perm = Math.permutate(x.length);
        double[][] datax = new double[4000][];
        double[] datay = new double[datax.length];
        for (int i = 0; i < datax.length; i++) {
            datax[i] = x[perm[i]];
            datay[i] = y[perm[i]];
        }
        int n = datax.length;
        int k = 10;
        CrossValidation cv = new CrossValidation(n, k);
        double rss = 0.0;
        double sparseRSS30 = 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]);
            GaussianProcessRegression<double[]> rkhs = new GaussianProcessRegression<>(trainx, trainy, new GaussianKernel(34.97), 0.1);
            KMeans kmeans = new KMeans(trainx, 30, 10);
            double[][] centers = kmeans.centroids();
            double r0 = 0.0;
            for (int l = 0; l < centers.length; l++) {
                for (int j = 0; j < l; j++) {
                    r0 += Math.distance(centers[l], centers[j]);
                }
            }
            r0 /= (2 * centers.length);
            System.out.println("Kernel width = " + r0);
            GaussianProcessRegression<double[]> sparse30 = new GaussianProcessRegression<>(trainx, trainy, centers, new GaussianKernel(r0), 0.1);
            for (int j = 0; j < testx.length; j++) {
                double r = testy[j] - rkhs.predict(testx[j]);
                rss += r * r;
                r = testy[j] - sparse30.predict(testx[j]);
                sparseRSS30 += r * r;
            }
        }
        System.out.println("Regular 10-CV MSE = " + rss / n);
        System.out.println("Sparse (30) 10-CV MSE = " + sparseRSS30 / n);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : AttributeDataset(smile.data.AttributeDataset) KMeans(smile.clustering.KMeans) ArffParser(smile.data.parser.ArffParser) CrossValidation(smile.validation.CrossValidation) GaussianKernel(smile.math.kernel.GaussianKernel) Test(org.junit.Test)

Example 4 with CrossValidation

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

the class GaussianProcessRegressionTest method testCPU.

/**
     * Test of learn method, of class GaussianProcessRegression.
     */
@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;
        double sparseRSS30 = 0.0;
        double nystromRSS30 = 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]);
            GaussianProcessRegression<double[]> rkhs = new GaussianProcessRegression<>(trainx, trainy, new GaussianKernel(47.02), 0.1);
            KMeans kmeans = new KMeans(trainx, 30, 10);
            double[][] centers = kmeans.centroids();
            double r0 = 0.0;
            for (int l = 0; l < centers.length; l++) {
                for (int j = 0; j < l; j++) {
                    r0 += Math.distance(centers[l], centers[j]);
                }
            }
            r0 /= (2 * centers.length);
            System.out.println("Kernel width = " + r0);
            GaussianProcessRegression<double[]> sparse30 = new GaussianProcessRegression<>(trainx, trainy, centers, new GaussianKernel(r0), 0.1);
            GaussianProcessRegression<double[]> nystrom30 = new GaussianProcessRegression<>(trainx, trainy, centers, new GaussianKernel(r0), 0.1, true);
            for (int j = 0; j < testx.length; j++) {
                double r = testy[j] - rkhs.predict(testx[j]);
                rss += r * r;
                r = testy[j] - sparse30.predict(testx[j]);
                sparseRSS30 += r * r;
                r = testy[j] - nystrom30.predict(testx[j]);
                nystromRSS30 += r * r;
            }
        }
        System.out.println("Regular 10-CV MSE = " + rss / n);
        System.out.println("Sparse (30) 10-CV MSE = " + sparseRSS30 / n);
        System.out.println("Nystrom (30) 10-CV MSE = " + nystromRSS30 / n);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) KMeans(smile.clustering.KMeans) CrossValidation(smile.validation.CrossValidation) GaussianKernel(smile.math.kernel.GaussianKernel) Test(org.junit.Test)

Example 5 with CrossValidation

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

the class GaussianProcessRegressionTest method test2DPlanes.

/**
     * Test of learn method, of class GaussianProcessRegression.
     */
@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[][] x = data.toArray(new double[data.size()][]);
        double[] y = data.toArray(new double[data.size()]);
        int[] perm = Math.permutate(x.length);
        double[][] datax = new double[4000][];
        double[] datay = new double[datax.length];
        for (int i = 0; i < datax.length; i++) {
            datax[i] = x[perm[i]];
            datay[i] = y[perm[i]];
        }
        int n = datax.length;
        int k = 10;
        CrossValidation cv = new CrossValidation(n, k);
        double rss = 0.0;
        double sparseRSS30 = 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]);
            GaussianProcessRegression<double[]> rkhs = new GaussianProcessRegression<>(trainx, trainy, new GaussianKernel(34.866), 0.1);
            KMeans kmeans = new KMeans(trainx, 30, 10);
            double[][] centers = kmeans.centroids();
            double r0 = 0.0;
            for (int l = 0; l < centers.length; l++) {
                for (int j = 0; j < l; j++) {
                    r0 += Math.distance(centers[l], centers[j]);
                }
            }
            r0 /= (2 * centers.length);
            System.out.println("Kernel width = " + r0);
            GaussianProcessRegression<double[]> sparse30 = new GaussianProcessRegression<>(trainx, trainy, centers, new GaussianKernel(r0), 0.1);
            for (int j = 0; j < testx.length; j++) {
                double r = testy[j] - rkhs.predict(testx[j]);
                rss += r * r;
                r = testy[j] - sparse30.predict(testx[j]);
                sparseRSS30 += r * r;
            }
        }
        System.out.println("Regular 10-CV MSE = " + rss / n);
        System.out.println("Sparse (30) 10-CV MSE = " + sparseRSS30 / n);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : AttributeDataset(smile.data.AttributeDataset) KMeans(smile.clustering.KMeans) ArffParser(smile.data.parser.ArffParser) CrossValidation(smile.validation.CrossValidation) GaussianKernel(smile.math.kernel.GaussianKernel) 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