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);
}
}
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);
}
}
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);
}
}
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);
}
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);
}
}
Aggregations