Search in sources :

Example 26 with AttributeDataset

use of smile.data.AttributeDataset in project smile by haifengl.

the class AdaBoostTest method testUSPS.

/**
     * Test of learn method, of class AdaBoost.
     */
@Test
public void testUSPS() {
    System.out.println("USPS");
    DelimitedTextParser parser = new DelimitedTextParser();
    parser.setResponseIndex(new NominalAttribute("class"), 0);
    try {
        AttributeDataset train = parser.parse("USPS Train", smile.data.parser.IOUtils.getTestDataFile("usps/zip.train"));
        AttributeDataset test = parser.parse("USPS Test", smile.data.parser.IOUtils.getTestDataFile("usps/zip.test"));
        double[][] x = train.toArray(new double[train.size()][]);
        int[] y = train.toArray(new int[train.size()]);
        double[][] testx = test.toArray(new double[test.size()][]);
        int[] testy = test.toArray(new int[test.size()]);
        for (int i = 0; i < y.length; i++) {
            if (y[i] != 0)
                y[i] = 1;
        }
        for (int i = 0; i < testy.length; i++) {
            if (testy[i] != 0)
                testy[i] = 1;
        }
        AdaBoost forest = new AdaBoost(x, y, 100, 6);
        int error = 0;
        for (int i = 0; i < testx.length; i++) {
            if (forest.predict(testx[i]) != testy[i]) {
                error++;
            }
        }
        System.out.println("AdaBoost error = " + error);
        System.out.format("USPS error rate = %.2f%%%n", 100.0 * error / testx.length);
        assertTrue(error <= 25);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) Test(org.junit.Test)

Example 27 with AttributeDataset

use of smile.data.AttributeDataset in project smile by haifengl.

the class AdaBoostTest method testUSPSNominal.

/**
     * Test of learn method, of class AdaBoost.
     */
@Test
public void testUSPSNominal() {
    System.out.println("USPS nominal");
    DelimitedTextParser parser = new DelimitedTextParser();
    parser.setResponseIndex(new NominalAttribute("class"), 0);
    try {
        AttributeDataset train = parser.parse("USPS Train", smile.data.parser.IOUtils.getTestDataFile("usps/zip.train"));
        AttributeDataset test = parser.parse("USPS Test", smile.data.parser.IOUtils.getTestDataFile("usps/zip.test"));
        double[][] x = train.toArray(new double[train.size()][]);
        int[] y = train.toArray(new int[train.size()]);
        double[][] testx = test.toArray(new double[test.size()][]);
        int[] testy = test.toArray(new int[test.size()]);
        for (double[] xi : x) {
            for (int i = 0; i < xi.length; i++) {
                xi[i] = Math.round(255 * (xi[i] + 1) / 2);
            }
        }
        for (double[] xi : testx) {
            for (int i = 0; i < xi.length; i++) {
                xi[i] = Math.round(255 * (xi[i] + 1) / 2);
            }
        }
        Attribute[] attributes = new Attribute[256];
        String[] values = new String[attributes.length];
        for (int i = 0; i < attributes.length; i++) {
            values[i] = String.valueOf(i);
        }
        for (int i = 0; i < attributes.length; i++) {
            attributes[i] = new NominalAttribute("V" + i, values);
        }
        for (int i = 0; i < y.length; i++) {
            if (y[i] != 0)
                y[i] = 1;
        }
        for (int i = 0; i < testy.length; i++) {
            if (testy[i] != 0)
                testy[i] = 1;
        }
        AdaBoost forest = new AdaBoost(attributes, x, y, 100, 6);
        int error = 0;
        for (int i = 0; i < testx.length; i++) {
            if (forest.predict(testx[i]) != testy[i]) {
                error++;
            }
        }
        System.out.println("AdaBoost error = " + error);
        System.out.format("USPS error rate = %.2f%%%n", 100.0 * error / testx.length);
        assertTrue(error <= 25);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) Attribute(smile.data.Attribute) NominalAttribute(smile.data.NominalAttribute) NominalAttribute(smile.data.NominalAttribute) Test(org.junit.Test)

Example 28 with AttributeDataset

use of smile.data.AttributeDataset in project smile by haifengl.

the class KPCATest method testKPCAK.

/**
     * Test of learn method, of class PCA.
     */
@Test
public void testKPCAK() {
    System.out.println("learn k");
    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)), 29);
        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 29 with AttributeDataset

use of smile.data.AttributeDataset 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 30 with AttributeDataset

use of smile.data.AttributeDataset 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)

Aggregations

AttributeDataset (smile.data.AttributeDataset)140 Test (org.junit.Test)125 ArffParser (smile.data.parser.ArffParser)75 NominalAttribute (smile.data.NominalAttribute)50 DelimitedTextParser (smile.data.parser.DelimitedTextParser)48 Attribute (smile.data.Attribute)29 EuclideanDistance (smile.math.distance.EuclideanDistance)19 LOOCV (smile.validation.LOOCV)18 CrossValidation (smile.validation.CrossValidation)17 AdjustedRandIndex (smile.validation.AdjustedRandIndex)14 RandIndex (smile.validation.RandIndex)14 ClassifierTrainer (smile.classification.ClassifierTrainer)13 GaussianKernel (smile.math.kernel.GaussianKernel)11 IOException (java.io.IOException)10 RadialBasisFunction (smile.math.rbf.RadialBasisFunction)9 RBFNetwork (smile.regression.RBFNetwork)8 ArrayList (java.util.ArrayList)6 KMeans (smile.clustering.KMeans)6 Datum (smile.data.Datum)6 NumericAttribute (smile.data.NumericAttribute)6