Search in sources :

Example 11 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)

Example 12 with EuclideanDistance

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

the class CLARANSTest method testUSPS.

/**
     * Test of learn method, of class CLARANS.
     */
@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()]);
        AdjustedRandIndex ari = new AdjustedRandIndex();
        RandIndex rand = new RandIndex();
        CLARANS<double[]> clarans = new CLARANS<>(x, new EuclideanDistance(), 10, 50, 8);
        double r = rand.measure(y, clarans.getClusterLabel());
        double r2 = ari.measure(y, clarans.getClusterLabel());
        System.out.format("Training rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
        assertTrue(r > 0.8);
        assertTrue(r2 > 0.28);
        int[] p = new int[testx.length];
        for (int i = 0; i < testx.length; i++) {
            p[i] = clarans.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.8);
        assertTrue(r2 > 0.25);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) RandIndex(smile.validation.RandIndex) AdjustedRandIndex(smile.validation.AdjustedRandIndex) EuclideanDistance(smile.math.distance.EuclideanDistance) NominalAttribute(smile.data.NominalAttribute) AdjustedRandIndex(smile.validation.AdjustedRandIndex) Test(org.junit.Test)

Example 13 with EuclideanDistance

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

the class MECTest method testUSPS.

/**
     * Test of learn method, of class MEC.
     */
@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()]);
        AdjustedRandIndex ari = new AdjustedRandIndex();
        RandIndex rand = new RandIndex();
        MEC<double[]> mec = new MEC<>(x, new EuclideanDistance(), 10, 8.0);
        double r = rand.measure(y, mec.getClusterLabel());
        double r2 = ari.measure(y, mec.getClusterLabel());
        System.out.format("Training rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
        assertTrue(r > 0.85);
        assertTrue(r2 > 0.35);
        int[] p = new int[testx.length];
        for (int i = 0; i < testx.length; i++) {
            p[i] = mec.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.85);
        assertTrue(r2 > 0.35);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) RandIndex(smile.validation.RandIndex) AdjustedRandIndex(smile.validation.AdjustedRandIndex) EuclideanDistance(smile.math.distance.EuclideanDistance) NominalAttribute(smile.data.NominalAttribute) AdjustedRandIndex(smile.validation.AdjustedRandIndex) Test(org.junit.Test)

Example 14 with EuclideanDistance

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

the class CoverTreeTest method testKnn1.

/**
     * Test of knn method, of class CoverTree. The data has only one elements
     */
@Test
public void testKnn1() {
    System.out.println("knn1");
    double[][] data1 = { data[0] };
    EuclideanDistance d = new EuclideanDistance();
    coverTree = new CoverTree<>(data1, d);
    Neighbor[] n1 = coverTree.knn(data[1], 1);
    assertEquals(1, n1.length);
    assertEquals(0, n1[0].index);
    assertEquals(data[0], n1[0].value);
    assertEquals(d.d(data[0], data[1]), n1[0].distance, 1E-7);
}
Also used : EuclideanDistance(smile.math.distance.EuclideanDistance) Test(org.junit.Test)

Example 15 with EuclideanDistance

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

the class ValidationTest method testCv_4args_2.

/**
     * Test of cv method, of class Validation.
     */
@Test
public void testCv_4args_2() {
    System.out.println("cv");
    ArffParser parser = new ArffParser();
    parser.setResponseIndex(6);
    try {
        AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/cpu.arff"));
        double[] y = data.toArray(new double[data.size()]);
        double[][] x = data.toArray(new double[data.size()][]);
        Math.standardize(x);
        RBFNetwork.Trainer<double[]> trainer = new RBFNetwork.Trainer<>(new EuclideanDistance());
        trainer.setNumCenters(20);
        double rmse = Validation.cv(10, trainer, x, y);
        System.out.println("RMSE = " + rmse);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : EuclideanDistance(smile.math.distance.EuclideanDistance) ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) ClassifierTrainer(smile.classification.ClassifierTrainer) RBFNetwork(smile.regression.RBFNetwork) 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