use of smile.math.distance.EuclideanDistance in project smile by haifengl.
the class ValidationTest method testBootstrap_5args_2.
/**
* Test of bootstrap method, of class Validation.
*/
@Test
public void testBootstrap_5args_2() {
System.out.println("bootstrap");
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);
RegressionMeasure[] measures = { new RMSE(), new AbsoluteDeviation() };
double[][] results = Validation.bootstrap(100, trainer, x, y, measures);
System.out.println("100-fold bootstrap RMSE average = " + Math.mean(results[0]));
System.out.println("100-fold bootstrap RMSE std.dev = " + Math.sd(results[0]));
System.out.println("100-fold bootstrap AbsoluteDeviation average = " + Math.mean(results[1]));
System.out.println("100-fold bootstrap AbsoluteDeviation std.dev = " + Math.sd(results[1]));
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.math.distance.EuclideanDistance in project smile by haifengl.
the class ValidationTest method testBootstrap_4args_2.
/**
* Test of bootstrap method, of class Validation.
*/
@Test
public void testBootstrap_4args_2() {
System.out.println("bootstrap");
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.bootstrap(100, trainer, x, y);
System.out.println("100-fold bootstrap RMSE average = " + Math.mean(rmse));
System.out.println("100-fold bootstrap RMSE std.dev = " + Math.sd(rmse));
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.math.distance.EuclideanDistance in project smile by haifengl.
the class RBFNetworkDemo method learn.
@Override
public double[][] learn(double[] x, double[] y) {
double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
int[] label = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
try {
k = Integer.parseInt(kField.getText().trim());
if (k < 2 || k > data.length) {
JOptionPane.showMessageDialog(this, "Invalid K: " + k, "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Invalid K: " + kField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
double[][] centers = new double[k][];
RadialBasisFunction basis = SmileUtils.learnGaussianRadialBasis(data, centers);
RBFNetwork<double[]> rbf = new RBFNetwork<>(data, label, new EuclideanDistance(), basis, centers);
for (int i = 0; i < label.length; i++) {
label[i] = rbf.predict(data[i]);
}
double trainError = error(label, label);
System.out.format("training error = %.2f%%\n", 100 * trainError);
double[][] z = new double[y.length][x.length];
for (int i = 0; i < y.length; i++) {
for (int j = 0; j < x.length; j++) {
double[] p = { x[j], y[i] };
z[i][j] = rbf.predict(p);
}
}
return z;
}
use of smile.math.distance.EuclideanDistance in project smile by haifengl.
the class RBFNetworkTest method testLearn.
/**
* Test of learn method, of class RBFNetwork.
*/
@Test
public void testLearn() {
System.out.println("learn");
ArffParser arffParser = new ArffParser();
arffParser.setResponseIndex(4);
try {
AttributeDataset iris = arffParser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/iris.arff"));
double[][] x = iris.toArray(new double[iris.size()][]);
int[] y = iris.toArray(new int[iris.size()]);
int n = x.length;
LOOCV loocv = new LOOCV(n);
int error = 0;
for (int i = 0; i < n; i++) {
double[][] trainx = Math.slice(x, loocv.train[i]);
int[] trainy = Math.slice(y, loocv.train[i]);
double[][] centers = new double[10][];
RadialBasisFunction[] basis = SmileUtils.learnGaussianRadialBasis(trainx, centers, 5.0);
RBFNetwork<double[]> rbf = new RBFNetwork<>(trainx, trainy, new EuclideanDistance(), basis, centers);
if (y[loocv.test[i]] != rbf.predict(x[loocv.test[i]]))
error++;
}
System.out.println("RBF network error = " + error);
assertTrue(error <= 6);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.math.distance.EuclideanDistance in project smile by haifengl.
the class RBFNetworkTest method testUSPS.
/**
* Test of learn method, of class RBFNetwork.
*/
@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()]);
double[][] centers = new double[200][];
RadialBasisFunction basis = SmileUtils.learnGaussianRadialBasis(x, centers);
RBFNetwork<double[]> rbf = new RBFNetwork<>(x, y, new EuclideanDistance(), new GaussianRadialBasis(8.0), centers);
int error = 0;
for (int i = 0; i < testx.length; i++) {
if (rbf.predict(testx[i]) != testy[i]) {
error++;
}
}
System.out.format("USPS error rate = %.2f%%%n", 100.0 * error / testx.length);
assertTrue(error <= 150);
} catch (Exception ex) {
System.err.println(ex);
}
}
Aggregations