Search in sources :

Example 41 with ArffParser

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

the class SignalNoiseRatioTest method testRank.

/**
     * Test of rank method, of class SignalNoiseRatio.
     */
@Test
public void testRank() {
    System.out.println("rank");
    try {
        ArffParser arffParser = new ArffParser();
        arffParser.setResponseIndex(4);
        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] = 0;
            else
                y[i] = 1;
        }
        SignalNoiseRatio s2n = new SignalNoiseRatio();
        double[] ratio = s2n.rank(x, y);
        assertEquals(4, ratio.length);
        assertEquals(0.8743107, ratio[0], 1E-7);
        assertEquals(0.1502717, ratio[1], 1E-7);
        assertEquals(1.3446912, ratio[2], 1E-7);
        assertEquals(1.4757334, ratio[3], 1E-7);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) Test(org.junit.Test)

Example 42 with ArffParser

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

the class SumSquaresRatioTest method testRank.

/**
     * Test of rank method, of class SumSquaresRatio.
     */
@Test
public void testRank() {
    System.out.println("rank");
    try {
        ArffParser arffParser = new ArffParser();
        arffParser.setResponseIndex(4);
        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()]);
        SumSquaresRatio ssr = new SumSquaresRatio();
        double[] ratio = ssr.rank(x, y);
        assertEquals(4, ratio.length);
        assertEquals(1.6226463, ratio[0], 1E-7);
        assertEquals(0.6444144, ratio[1], 1E-7);
        assertEquals(16.0412833, ratio[2], 1E-7);
        assertEquals(13.0520327, ratio[3], 1E-7);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) Test(org.junit.Test)

Example 43 with ArffParser

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

the class KPCATest method testKPCAThreshold.

/**
     * Test of learn method, of class PCA.
     */
@Test
public void testKPCAThreshold() {
    System.out.println("learn threshold");
    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()][]);
        KPCA<double[]> kpca = new KPCA(x, new GaussianKernel(Math.sqrt(2.5)), 1E-4);
        assertTrue(Math.equals(latent, kpca.getVariances(), 1E-3));
        double[][] points = kpca.project(x);
        points[0] = kpca.project(x[0]);
        assertTrue(Math.equals(points, kpca.getCoordinates(), 1E-7));
    /*
            for (int j = 0; j < points[0].length; j++) {
                double sign = Math.signum(points[0][j] / scores[0][j]);
                for (int i = 0; i < points.length; i++) {
                    points[i][j] *= sign;
                }
            }

            assertTrue(Math.equals(scores, points, 1E-1));
 */
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) GaussianKernel(smile.math.kernel.GaussianKernel) Test(org.junit.Test)

Example 44 with ArffParser

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

the class GaussianProcessRegressionTest method testAilerons.

/**
     * Test of learn method, of class GaussianProcessRegression.
     */
@Test
public void testAilerons() {
    System.out.println("ailerons");
    ArffParser parser = new ArffParser();
    parser.setResponseIndex(40);
    try {
        AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/ailerons.arff"));
        double[][] x = data.toArray(new double[data.size()][]);
        Math.standardize(x);
        double[] y = data.toArray(new double[data.size()]);
        for (int i = 0; i < y.length; i++) {
            y[i] *= 10000;
        }
        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(183.96), 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 45 with ArffParser

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

the class GaussianProcessRegressionTest method testBank32nh.

/**
     * Test of learn method, of class GaussianProcessRegression.
     */
@Test
public void testBank32nh() {
    System.out.println("bank32nh");
    ArffParser parser = new ArffParser();
    parser.setResponseIndex(32);
    try {
        AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/bank32nh.arff"));
        double[] y = data.toArray(new double[data.size()]);
        double[][] x = data.toArray(new double[data.size()][]);
        Math.standardize(x);
        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(55.3), 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

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