Search in sources :

Example 6 with RandIndex

use of smile.validation.RandIndex in project smile by haifengl.

the class SpectralClusteringTest method testUSPS.

/**
     * Test of learn method, of class SpectralClustering.
     */
@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"));
        double[][] x = train.toArray(new double[train.size()][]);
        int[] y = train.toArray(new int[train.size()]);
        SpectralClustering spectral = new SpectralClustering(x, 10, 8.0);
        AdjustedRandIndex ari = new AdjustedRandIndex();
        RandIndex rand = new RandIndex();
        double r = rand.measure(y, spectral.getClusterLabel());
        double r2 = ari.measure(y, spectral.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.45);
    } 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 7 with RandIndex

use of smile.validation.RandIndex 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 8 with RandIndex

use of smile.validation.RandIndex 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 9 with RandIndex

use of smile.validation.RandIndex in project smile by haifengl.

the class DENCLUETest method testToy.

/**
     * Test of learn method, of class DENCLUE.
     */
@Test
public void testToy() {
    System.out.println("Toy");
    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][];
    int[] label = new int[10000];
    MultivariateGaussianDistribution g1 = new MultivariateGaussianDistribution(mu1, sigma1);
    for (int i = 0; i < 2000; i++) {
        data[i] = g1.rand();
        label[i] = 0;
    }
    MultivariateGaussianDistribution g2 = new MultivariateGaussianDistribution(mu2, sigma2);
    for (int i = 0; i < 3000; i++) {
        data[2000 + i] = g2.rand();
        label[i] = 1;
    }
    MultivariateGaussianDistribution g3 = new MultivariateGaussianDistribution(mu3, sigma3);
    for (int i = 0; i < 3000; i++) {
        data[5000 + i] = g3.rand();
        label[i] = 2;
    }
    MultivariateGaussianDistribution g4 = new MultivariateGaussianDistribution(mu4, sigma4);
    for (int i = 0; i < 2000; i++) {
        data[8000 + i] = g4.rand();
        label[i] = 3;
    }
    DENCLUE denclue = new DENCLUE(data, 0.8, 50);
    AdjustedRandIndex ari = new AdjustedRandIndex();
    RandIndex rand = new RandIndex();
    double r = rand.measure(label, denclue.getClusterLabel());
    double r2 = ari.measure(label, denclue.getClusterLabel());
    System.out.println("The number of clusters: " + denclue.getNumClusters());
    System.out.format("Training rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
    assertTrue(r > 0.54);
    assertTrue(r2 > 0.2);
}
Also used : MultivariateGaussianDistribution(smile.stat.distribution.MultivariateGaussianDistribution) RandIndex(smile.validation.RandIndex) AdjustedRandIndex(smile.validation.AdjustedRandIndex) AdjustedRandIndex(smile.validation.AdjustedRandIndex) Test(org.junit.Test)

Example 10 with RandIndex

use of smile.validation.RandIndex in project smile by haifengl.

the class KMeansTest method testBBD4.

/**
     * Test of learn method, of class KMeans.
     */
@Test
public void testBBD4() {
    System.out.println("BBD 4");
    KMeans kmeans = new KMeans(data, 4, 100);
    AdjustedRandIndex ari = new AdjustedRandIndex();
    RandIndex rand = new RandIndex();
    double r = rand.measure(label, kmeans.getClusterLabel());
    double r2 = ari.measure(label, kmeans.getClusterLabel());
    System.out.format("Training rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
}
Also used : AdjustedRandIndex(smile.validation.AdjustedRandIndex) RandIndex(smile.validation.RandIndex) AdjustedRandIndex(smile.validation.AdjustedRandIndex) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)21 AdjustedRandIndex (smile.validation.AdjustedRandIndex)21 RandIndex (smile.validation.RandIndex)21 AttributeDataset (smile.data.AttributeDataset)14 NominalAttribute (smile.data.NominalAttribute)14 DelimitedTextParser (smile.data.parser.DelimitedTextParser)14 EuclideanDistance (smile.math.distance.EuclideanDistance)2 MultivariateGaussianDistribution (smile.stat.distribution.MultivariateGaussianDistribution)2 CompleteLinkage (smile.clustering.linkage.CompleteLinkage)1 SingleLinkage (smile.clustering.linkage.SingleLinkage)1 UPGMALinkage (smile.clustering.linkage.UPGMALinkage)1 UPGMCLinkage (smile.clustering.linkage.UPGMCLinkage)1 WPGMALinkage (smile.clustering.linkage.WPGMALinkage)1 WPGMCLinkage (smile.clustering.linkage.WPGMCLinkage)1 WardLinkage (smile.clustering.linkage.WardLinkage)1 SparseDataset (smile.data.SparseDataset)1 LibsvmParser (smile.data.parser.LibsvmParser)1