Search in sources :

Example 41 with AttributeDataset

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

the class SVMTest method testLearn.

/**
     * Test of learn method, of class SVM.
     */
@Test
public void testLearn() {
    System.out.println("learn");
    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()][]);
        int[] y = iris.toArray(new int[iris.size()]);
        SVM<double[]> svm = new SVM<>(new LinearKernel(), 10.0, Math.max(y) + 1, SVM.Multiclass.ONE_VS_ALL);
        svm.learn(x, y);
        svm.learn(x, y);
        svm.finish();
        int error = 0;
        for (int i = 0; i < x.length; i++) {
            if (svm.predict(x[i]) != y[i]) {
                error++;
            }
        }
        System.out.println("Linear ONE vs. ALL error = " + error);
        assertTrue(error <= 10);
        svm = new SVM<>(new GaussianKernel(1), 1.0, Math.max(y) + 1, SVM.Multiclass.ONE_VS_ALL);
        svm.learn(x, y);
        svm.learn(x, y);
        svm.finish();
        svm.trainPlattScaling(x, y);
        error = 0;
        for (int i = 0; i < x.length; i++) {
            if (svm.predict(x[i]) != y[i]) {
                error++;
            }
            double[] prob = new double[3];
            int yp = svm.predict(x[i], prob);
        //System.out.format("%d %d %.2f, %.2f %.2f\n", y[i], yp, prob[0], prob[1], prob[2]);
        }
        System.out.println("Gaussian ONE vs. ALL error = " + error);
        assertTrue(error <= 5);
        svm = new SVM<>(new GaussianKernel(1), 1.0, Math.max(y) + 1, SVM.Multiclass.ONE_VS_ONE);
        svm.learn(x, y);
        svm.learn(x, y);
        svm.finish();
        svm.trainPlattScaling(x, y);
        error = 0;
        for (int i = 0; i < x.length; i++) {
            if (svm.predict(x[i]) != y[i]) {
                error++;
            }
            double[] prob = new double[3];
            int yp = svm.predict(x[i], prob);
        //System.out.format("%d %d %.2f, %.2f %.2f\n", y[i], yp, prob[0], prob[1], prob[2]);
        }
        System.out.println("Gaussian ONE vs. ONE error = " + error);
        assertTrue(error <= 5);
        svm = new SVM<>(new PolynomialKernel(2), 1.0, Math.max(y) + 1, SVM.Multiclass.ONE_VS_ALL);
        svm.learn(x, y);
        svm.learn(x, y);
        svm.finish();
        error = 0;
        for (int i = 0; i < x.length; i++) {
            if (svm.predict(x[i]) != y[i]) {
                error++;
            }
        }
        System.out.println("Polynomial ONE vs. ALL error = " + error);
        assertTrue(error <= 5);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : AttributeDataset(smile.data.AttributeDataset) PolynomialKernel(smile.math.kernel.PolynomialKernel) ArffParser(smile.data.parser.ArffParser) LinearKernel(smile.math.kernel.LinearKernel) GaussianKernel(smile.math.kernel.GaussianKernel) Test(org.junit.Test)

Example 42 with AttributeDataset

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

the class BIRCHTest method testUSPS.

/**
     * Test of learn method, of class BIRCH.
     */
@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()]);
        BIRCH birch = new BIRCH(x[0].length, 5, 16.0);
        for (int i = 0; i < 20; i++) {
            int[] index = Math.permutate(x.length);
            for (int j = 0; j < x.length; j++) {
                birch.add(x[index[j]]);
            }
        }
        birch.partition(10);
        AdjustedRandIndex ari = new AdjustedRandIndex();
        RandIndex rand = new RandIndex();
        int[] p = new int[x.length];
        for (int i = 0; i < x.length; i++) {
            p[i] = birch.predict(x[i]);
        }
        double r = rand.measure(y, p);
        double r2 = ari.measure(y, p);
        System.out.format("Training rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
        assertTrue(r > 0.65);
        assertTrue(r2 > 0.20);
        p = new int[testx.length];
        for (int i = 0; i < testx.length; i++) {
            p[i] = birch.predict(testx[i]);
        }
        r = rand.measure(testy, p);
        r2 = ari.measure(testy, p);
        System.out.format("Testing rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
        assertTrue(r > 0.65);
        assertTrue(r2 > 0.20);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) RandIndex(smile.validation.RandIndex) AdjustedRandIndex(smile.validation.AdjustedRandIndex) AdjustedRandIndex(smile.validation.AdjustedRandIndex) Test(org.junit.Test)

Example 43 with AttributeDataset

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

Example 44 with AttributeDataset

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

the class GradientTreeBoostTest method test.

public void test(GradientTreeBoost.Loss loss, String dataset, String url, int response) {
    System.out.println(dataset + "\t" + loss);
    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]);
            GradientTreeBoost boost = new GradientTreeBoost(data.attributes(), trainx, trainy, loss, 100, 6, 0.05, 0.7);
            for (int j = 0; j < testx.length; j++) {
                double r = testy[j] - boost.predict(testx[j]);
                ad += Math.abs(r);
                rss += r * 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)

Example 45 with AttributeDataset

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

the class RBFNetworkTest method testAilerons.

/**
     * Test of learn method, of class RBFNetwork.
     */
@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[][] datax = data.toArray(new double[data.size()][]);
        Math.standardize(datax);
        double[] datay = data.toArray(new double[data.size()]);
        for (int i = 0; i < datay.length; i++) {
            datay[i] *= 10000;
        }
        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)

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