Search in sources :

Example 6 with EuclideanDistance

use of smile.math.distance.EuclideanDistance in project smile by haifengl.

the class LinearSearchSpeedTest method testToy.

/**
     * Test of nearest method, of class LinearSearch.
     */
@Test
public void testToy() {
    System.out.println("toy data");
    long start = System.currentTimeMillis();
    double[] mu1 = { 1.0, 1.0, 1.0 };
    double[][] sigma1 = { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } };
    double[] mu2 = { -2.0, -2.0, -2.0 };
    double[][] sigma2 = { { 1.0, 0.3, 0.8 }, { 0.3, 1.0, 0.5 }, { 0.8, 0.5, 1.0 } };
    double[] mu3 = { 4.0, 2.0, 3.0 };
    double[][] sigma3 = { { 1.0, 0.8, 0.3 }, { 0.8, 1.0, 0.5 }, { 0.3, 0.5, 1.0 } };
    double[] mu4 = { 3.0, 5.0, 1.0 };
    double[][] sigma4 = { { 1.0, 0.5, 0.5 }, { 0.5, 1.0, 0.5 }, { 0.5, 0.5, 1.0 } };
    double[][] data = new double[10000][];
    MultivariateGaussianDistribution g1 = new MultivariateGaussianDistribution(mu1, sigma1);
    for (int i = 0; i < 2000; i++) {
        data[i] = g1.rand();
    }
    MultivariateGaussianDistribution g2 = new MultivariateGaussianDistribution(mu2, sigma2);
    for (int i = 0; i < 3000; i++) {
        data[2000 + i] = g2.rand();
    }
    MultivariateGaussianDistribution g3 = new MultivariateGaussianDistribution(mu3, sigma3);
    for (int i = 0; i < 3000; i++) {
        data[5000 + i] = g3.rand();
    }
    MultivariateGaussianDistribution g4 = new MultivariateGaussianDistribution(mu4, sigma4);
    for (int i = 0; i < 2000; i++) {
        data[8000 + i] = g4.rand();
    }
    double time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("Generating toy data (four Gaussians): %.2fs%n", time);
    LinearSearch<double[]> naive = new LinearSearch<>(data, new EuclideanDistance());
    start = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        naive.nearest(data[Math.randomInt(data.length)]);
    }
    time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("NN: %.2fs%n", time);
    start = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        naive.knn(data[Math.randomInt(data.length)], 10);
    }
    time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("10-NN: %.2fs%n", time);
    start = System.currentTimeMillis();
    List<Neighbor<double[], double[]>> n = new ArrayList<>();
    for (int i = 0; i < 1000; i++) {
        naive.range(data[Math.randomInt(data.length)], 1.0, n);
        n.clear();
    }
    time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("Range: %.2fs%n", time);
}
Also used : EuclideanDistance(smile.math.distance.EuclideanDistance) MultivariateGaussianDistribution(smile.stat.distribution.MultivariateGaussianDistribution) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with EuclideanDistance

use of smile.math.distance.EuclideanDistance in project smile by haifengl.

the class RBFNetworkTest method testSegment.

/**
     * Test of learn method, of class RBFNetwork.
     */
@Test
public void testSegment() {
    System.out.println("Segment");
    ArffParser parser = new ArffParser();
    parser.setResponseIndex(19);
    try {
        AttributeDataset train = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/segment-challenge.arff"));
        AttributeDataset test = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/segment-test.arff"));
        double[][] x = train.toArray(new double[0][]);
        int[] y = train.toArray(new int[0]);
        double[][] testx = test.toArray(new double[0][]);
        int[] testy = test.toArray(new int[0]);
        double[][] centers = new double[100][];
        RadialBasisFunction[] basis = SmileUtils.learnGaussianRadialBasis(x, centers, 5.0);
        RBFNetwork<double[]> rbf = new RBFNetwork<>(x, y, new EuclideanDistance(), basis, centers);
        int error = 0;
        for (int i = 0; i < testx.length; i++) {
            if (rbf.predict(testx[i]) != testy[i]) {
                error++;
            }
        }
        System.out.format("Segment error rate = %.2f%%%n", 100.0 * error / testx.length);
        assertTrue(error <= 210);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : RadialBasisFunction(smile.math.rbf.RadialBasisFunction) EuclideanDistance(smile.math.distance.EuclideanDistance) ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) Test(org.junit.Test)

Example 8 with EuclideanDistance

use of smile.math.distance.EuclideanDistance 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)

Example 9 with EuclideanDistance

use of smile.math.distance.EuclideanDistance in project smile by haifengl.

the class RBFNetworkTest method testLearn.

/**
     * Test of learn method, of class RKHSRegression.
     */
@Test
public void testLearn() {
    System.out.println("learn");
    Math.standardize(longley);
    int n = longley.length;
    LOOCV loocv = new LOOCV(n);
    double rss = 0.0;
    for (int i = 0; i < n; i++) {
        double[][] trainx = Math.slice(longley, loocv.train[i]);
        double[] trainy = Math.slice(y, loocv.train[i]);
        double[][] centers = new double[10][];
        RadialBasisFunction[] basis = SmileUtils.learnGaussianRadialBasis(trainx, centers, 5.0);
        RBFNetwork<double[]> rbf = new RBFNetwork<>(trainx, trainy, new EuclideanDistance(), basis, centers);
        double r = y[loocv.test[i]] - rbf.predict(longley[loocv.test[i]]);
        rss += r * r;
    }
    System.out.println("MSE = " + rss / n);
}
Also used : RadialBasisFunction(smile.math.rbf.RadialBasisFunction) EuclideanDistance(smile.math.distance.EuclideanDistance) LOOCV(smile.validation.LOOCV) Test(org.junit.Test)

Example 10 with EuclideanDistance

use of smile.math.distance.EuclideanDistance 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)

Aggregations

EuclideanDistance (smile.math.distance.EuclideanDistance)29 Test (org.junit.Test)22 AttributeDataset (smile.data.AttributeDataset)19 ArffParser (smile.data.parser.ArffParser)14 RadialBasisFunction (smile.math.rbf.RadialBasisFunction)11 RBFNetwork (smile.regression.RBFNetwork)8 ClassifierTrainer (smile.classification.ClassifierTrainer)6 PlotCanvas (smile.plot.PlotCanvas)6 ArrayList (java.util.ArrayList)5 NominalAttribute (smile.data.NominalAttribute)5 DelimitedTextParser (smile.data.parser.DelimitedTextParser)5 CrossValidation (smile.validation.CrossValidation)4 CoverTree (smile.neighbor.CoverTree)3 KDTree (smile.neighbor.KDTree)3 LSH (smile.neighbor.LSH)3 LinearSearch (smile.neighbor.LinearSearch)3 MPLSH (smile.neighbor.MPLSH)3 Neighbor (smile.neighbor.Neighbor)2 AdjustedRandIndex (smile.validation.AdjustedRandIndex)2 LOOCV (smile.validation.LOOCV)2