Search in sources :

Example 21 with RandIndex

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

the class XMeansTest method testUSPS.

/**
     * Test of learn method, of class XMeans.
     */
@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();
        XMeans xmeans = new XMeans(x, 10);
        double r = rand.measure(y, xmeans.getClusterLabel());
        double r2 = ari.measure(y, xmeans.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.4);
        int[] p = new int[testx.length];
        for (int i = 0; i < testx.length; i++) {
            p[i] = xmeans.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.4);
    } 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