use of smile.validation.AdjustedRandIndex in project smile by haifengl.
the class GMeansTest method testUSPS.
/**
* Test of learn method, of class GMeans.
*/
@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();
GMeans gmeans = new GMeans(x, 10);
double r = rand.measure(y, gmeans.getClusterLabel());
double r2 = ari.measure(y, gmeans.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] = gmeans.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);
}
}
use of smile.validation.AdjustedRandIndex in project smile by haifengl.
the class HierarchicalClusteringTest method testUSPS.
/**
* Test of learn method, of class GMeans.
*/
@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()]);
int n = x.length;
double[][] proximity = new double[n][];
for (int i = 0; i < n; i++) {
proximity[i] = new double[i + 1];
for (int j = 0; j < i; j++) {
proximity[i][j] = Math.distance(x[i], x[j]);
}
}
AdjustedRandIndex ari = new AdjustedRandIndex();
RandIndex rand = new RandIndex();
HierarchicalClustering hc = new HierarchicalClustering(new SingleLinkage(proximity));
int[] label = hc.partition(10);
double r = rand.measure(y, label);
double r2 = ari.measure(y, label);
System.out.format("SingleLinkage rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
assertTrue(r > 0.1);
hc = new HierarchicalClustering(new CompleteLinkage(proximity));
label = hc.partition(10);
r = rand.measure(y, label);
r2 = ari.measure(y, label);
System.out.format("CompleteLinkage rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
assertTrue(r > 0.75);
hc = new HierarchicalClustering(new UPGMALinkage(proximity));
label = hc.partition(10);
r = rand.measure(y, label);
r2 = ari.measure(y, label);
System.out.format("UPGMA rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
assertTrue(r > 0.1);
hc = new HierarchicalClustering(new WPGMALinkage(proximity));
label = hc.partition(10);
r = rand.measure(y, label);
r2 = ari.measure(y, label);
System.out.format("WPGMA rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
assertTrue(r > 0.2);
hc = new HierarchicalClustering(new UPGMCLinkage(proximity));
label = hc.partition(10);
r = rand.measure(y, label);
r2 = ari.measure(y, label);
System.out.format("UPGMC rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
assertTrue(r > 0.1);
hc = new HierarchicalClustering(new WPGMCLinkage(proximity));
label = hc.partition(10);
r = rand.measure(y, label);
r2 = ari.measure(y, label);
System.out.format("WPGMC rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
assertTrue(r > 0.1);
hc = new HierarchicalClustering(new WardLinkage(proximity));
label = hc.partition(10);
r = rand.measure(y, label);
r2 = ari.measure(y, label);
System.out.format("Ward rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
assertTrue(r > 0.9);
assertTrue(r2 > 0.5);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.validation.AdjustedRandIndex in project smile by haifengl.
the class KMeansTest method testBBD64.
/**
* Test of learn method, of class KMeans.
*/
@Test
public void testBBD64() {
System.out.println("BBD 64");
KMeans kmeans = new KMeans(data, 64, 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);
}
use of smile.validation.AdjustedRandIndex in project smile by haifengl.
the class KMeansTest method testLloyd64.
/**
* Test of lloyd method, of class KMeans.
*/
@Test
public void testLloyd64() {
System.out.println("Lloyd 64");
KMeans kmeans = KMeans.lloyd(data, 64, 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);
}
use of smile.validation.AdjustedRandIndex in project smile by haifengl.
the class KMeansTest method testLloyd4.
/**
* Test of lloyd method, of class KMeans.
*/
@Test
public void testLloyd4() {
System.out.println("Lloyd 4");
KMeans kmeans = KMeans.lloyd(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);
}
Aggregations