Search in sources :

Example 46 with AttributeDataset

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

the class RBFNetworkTest method testBank32nh.

/**
     * Test of learn method, of class RBFNetwork.
     */
@Test
public void testBank32nh() {
    System.out.println("bank32nh");
    ArffParser parser = new ArffParser();
    parser.setResponseIndex(31);
    try {
        AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/bank32nh.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 47 with AttributeDataset

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

the class RandomForestTest method test.

public void test(String dataset, String url, int response) {
    System.out.println(dataset);
    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]);
            RandomForest forest = new RandomForest(data.attributes(), trainx, trainy, 200, n, 5, trainx[0].length / 3);
            System.out.format("OOB error rate = %.4f%n", forest.error());
            for (int j = 0; j < testx.length; j++) {
                double r = testy[j] - forest.predict(testx[j]);
                rss += r * r;
                ad += Math.abs(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 48 with AttributeDataset

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

the class NaiveBayesTest method testPredict.

/**
     * Test of predict method, of class NaiveBayes.
     */
@Test
public void testPredict() {
    System.out.println("predict");
    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()]);
        int n = x.length;
        LOOCV loocv = new LOOCV(n);
        int error = 0;
        for (int l = 0; l < n; l++) {
            double[][] trainx = Math.slice(x, loocv.train[l]);
            int[] trainy = Math.slice(y, loocv.train[l]);
            int p = trainx[0].length;
            int k = Math.max(trainy) + 1;
            double[] priori = new double[k];
            Distribution[][] condprob = new Distribution[k][p];
            for (int i = 0; i < k; i++) {
                priori[i] = 1.0 / k;
                for (int j = 0; j < p; j++) {
                    ArrayList<Double> axi = new ArrayList<>();
                    for (int m = 0; m < trainx.length; m++) {
                        if (trainy[m] == i) {
                            axi.add(trainx[m][j]);
                        }
                    }
                    double[] xi = new double[axi.size()];
                    for (int m = 0; m < xi.length; m++) {
                        xi[m] = axi.get(m);
                    }
                    condprob[i][j] = new GaussianMixture(xi, 3);
                }
            }
            NaiveBayes bayes = new NaiveBayes(priori, condprob);
            if (y[loocv.test[l]] != bayes.predict(x[loocv.test[l]]))
                error++;
        }
        System.out.format("Iris error rate = %.2f%%%n", 100.0 * error / x.length);
        assertEquals(5, error);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : AttributeDataset(smile.data.AttributeDataset) ArrayList(java.util.ArrayList) GaussianMixture(smile.stat.distribution.GaussianMixture) LOOCV(smile.validation.LOOCV) IOException(java.io.IOException) ArffParser(smile.data.parser.ArffParser) Distribution(smile.stat.distribution.Distribution) Test(org.junit.Test)

Example 49 with AttributeDataset

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

the class NeuralNetworkTest method testUSPS.

/**
     * Test of learn method, of class NeuralNetwork.
     */
@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()]);
        int p = x[0].length;
        double[] mu = Math.colMean(x);
        double[] sd = Math.colSd(x);
        for (int i = 0; i < x.length; i++) {
            for (int j = 0; j < p; j++) {
                x[i][j] = (x[i][j] - mu[j]) / sd[j];
            }
        }
        for (int i = 0; i < testx.length; i++) {
            for (int j = 0; j < p; j++) {
                testx[i][j] = (testx[i][j] - mu[j]) / sd[j];
            }
        }
        NeuralNetwork net = new NeuralNetwork(NeuralNetwork.ErrorFunction.CROSS_ENTROPY, NeuralNetwork.ActivationFunction.SOFTMAX, x[0].length, 40, Math.max(y) + 1);
        for (int j = 0; j < 30; j++) {
            net.learn(x, y);
        }
        int error = 0;
        for (int i = 0; i < testx.length; i++) {
            if (net.predict(testx[i]) != testy[i]) {
                error++;
            }
        }
        System.out.format("USPS error rate = %.2f%%%n", 100.0 * error / testx.length);
    } 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 50 with AttributeDataset

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

the class NeuralNetworkTest method testIris2.

/**
     * Test of learn method, of class NeuralNetwork.
     */
@Test
public void testIris2() {
    System.out.println("Iris binary");
    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()]);
        for (int i = 0; i < y.length; i++) {
            if (y[i] == 2) {
                y[i] = 1;
            } else {
                y[i] = 0;
            }
        }
        int n = x.length;
        int p = x[0].length;
        double[] mu = Math.colMean(x);
        double[] sd = Math.colSd(x);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < p; j++) {
                x[i][j] = (x[i][j] - mu[j]) / sd[j];
            }
        }
        LOOCV loocv = new LOOCV(n);
        int error = 0;
        for (int i = 0; i < n; i++) {
            double[][] trainx = Math.slice(x, loocv.train[i]);
            int[] trainy = Math.slice(y, loocv.train[i]);
            NeuralNetwork net = new NeuralNetwork(NeuralNetwork.ErrorFunction.CROSS_ENTROPY, NeuralNetwork.ActivationFunction.LOGISTIC_SIGMOID, x[0].length, 10, 1);
            for (int j = 0; j < 30; j++) {
                net.learn(trainx, trainy);
            }
            if (y[loocv.test[i]] != net.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("Neural network error = " + error);
        assertTrue(error <= 8);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) LOOCV(smile.validation.LOOCV) 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