Search in sources :

Example 11 with AdjustedRandIndex

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

the class KMeansTest method testUSPS.

/**
     * Test of learn method, of class KMeans.
     */
@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()]);
        KMeans kmeans = new KMeans(x, 10, 100, 4);
        AdjustedRandIndex ari = new AdjustedRandIndex();
        RandIndex rand = new RandIndex();
        double r = rand.measure(y, kmeans.getClusterLabel());
        double r2 = ari.measure(y, kmeans.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);
        int[] p = new int[testx.length];
        for (int i = 0; i < testx.length; i++) {
            p[i] = kmeans.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.45);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) AdjustedRandIndex(smile.validation.AdjustedRandIndex) RandIndex(smile.validation.RandIndex) AdjustedRandIndex(smile.validation.AdjustedRandIndex) Test(org.junit.Test)

Example 12 with AdjustedRandIndex

use of smile.validation.AdjustedRandIndex 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 13 with AdjustedRandIndex

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

the class NeuralGasTest method testUSPS.

/**
     * Test of learn method, of class NeuralGas.
     */
@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()]);
        NeuralGas gas = new NeuralGas(x, 10);
        AdjustedRandIndex ari = new AdjustedRandIndex();
        RandIndex rand = new RandIndex();
        double r = rand.measure(y, gas.getClusterLabel());
        double r2 = ari.measure(y, gas.getClusterLabel());
        System.out.format("Training rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
        assertTrue(r > 0.88);
        assertTrue(r2 > 0.45);
        int[] p = new int[testx.length];
        for (int i = 0; i < testx.length; i++) {
            p[i] = gas.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.88);
        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 14 with AdjustedRandIndex

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

the class DBScanTest method testToy.

/**
     * Test of learn method, of class DBScan.
     */
@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;
    }
    DBScan<double[]> dbscan = new DBScan<>(data, new KDTree<>(data, data), 200, 0.8);
    System.out.println(dbscan);
    int[] size = dbscan.getClusterSize();
    int n = 0;
    for (int i = 0; i < size.length - 1; i++) {
        n += size[i];
    }
    int[] y1 = new int[n];
    int[] y2 = new int[n];
    for (int i = 0, j = 0; i < data.length; i++) {
        if (dbscan.getClusterLabel()[i] != Clustering.OUTLIER) {
            y1[j] = label[i];
            y2[j++] = dbscan.getClusterLabel()[i];
        }
    }
    AdjustedRandIndex ari = new AdjustedRandIndex();
    RandIndex rand = new RandIndex();
    double r = rand.measure(y1, y2);
    double r2 = ari.measure(y1, y2);
    System.out.println("The number of clusters: " + dbscan.getNumClusters());
    System.out.format("Training rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
    assertTrue(r > 0.40);
    assertTrue(r2 > 0.15);
}
Also used : MultivariateGaussianDistribution(smile.stat.distribution.MultivariateGaussianDistribution) RandIndex(smile.validation.RandIndex) AdjustedRandIndex(smile.validation.AdjustedRandIndex) AdjustedRandIndex(smile.validation.AdjustedRandIndex) Test(org.junit.Test)

Example 15 with AdjustedRandIndex

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

the class DeterministicAnnealingTest method testUSPS.

/**
     * Test of learn method, of class DeterministicAnnealing.
     */
@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()]);
        DeterministicAnnealing annealing = new DeterministicAnnealing(x, 10, 0.8);
        AdjustedRandIndex ari = new AdjustedRandIndex();
        RandIndex rand = new RandIndex();
        double r = rand.measure(y, annealing.getClusterLabel());
        double r2 = ari.measure(y, annealing.getClusterLabel());
        System.out.format("Training rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
        assertTrue(r > 0.75);
        assertTrue(r2 > 0.25);
        int[] p = new int[testx.length];
        for (int i = 0; i < testx.length; i++) {
            p[i] = annealing.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.75);
        assertTrue(r2 > 0.3);
    } 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)

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