use of smile.data.parser.ArffParser in project smile by haifengl.
the class RBFNetworkTest method test2DPlanes.
/**
* Test of learn method, of class RBFNetwork.
*/
@Test
public void test2DPlanes() {
System.out.println("2dplanes");
ArffParser parser = new ArffParser();
parser.setResponseIndex(10);
try {
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/2dplanes.arff"));
double[] datay = data.toArray(new double[data.size()]);
double[][] datax = data.toArray(new double[data.size()][]);
//Math.normalize(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.data.parser.ArffParser in project smile by haifengl.
the class RandomForestTest method testCPU.
/**
* Test of learn method, of class RandomForest.
*/
@Test
public void testCPU() {
System.out.println("CPU");
ArffParser parser = new ArffParser();
parser.setResponseIndex(6);
try {
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/cpu.arff"));
double[] datay = data.toArray(new double[data.size()]);
double[][] datax = data.toArray(new double[data.size()][]);
int n = datax.length;
int m = 3 * n / 4;
int[] index = Math.permutate(n);
double[][] trainx = new double[m][];
double[] trainy = new double[m];
for (int i = 0; i < m; i++) {
trainx[i] = datax[index[i]];
trainy[i] = datay[index[i]];
}
double[][] testx = new double[n - m][];
double[] testy = new double[n - m];
for (int i = m; i < n; i++) {
testx[i - m] = datax[index[i]];
testy[i - m] = datay[index[i]];
}
RandomForest forest = new RandomForest(data.attributes(), trainx, trainy, 100, n, 5, trainx[0].length / 3);
System.out.format("RMSE = %.4f%n", Validation.test(forest, testx, testy));
double[] rmse = forest.test(testx, testy);
for (int i = 1; i <= rmse.length; i++) {
System.out.format("%d trees RMSE = %.4f%n", i, rmse[i - 1]);
}
double[] importance = forest.importance();
index = QuickSort.sort(importance);
for (int i = importance.length; i-- > 0; ) {
System.out.format("%s importance is %.4f%n", data.attributes()[index[i]], importance[i]);
}
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.parser.ArffParser in project smile by haifengl.
the class ValidationTest method testLoocv_4args_1.
/**
* Test of loocv method, of class Validation.
*/
@Test
public void testLoocv_4args_1() {
System.out.println("loocv");
ArffParser arffParser = new ArffParser();
arffParser.setResponseIndex(4);
try {
AttributeDataset weather = arffParser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/weather.nominal.arff"));
double[][] x = weather.toArray(new double[weather.size()][]);
int[] y = weather.toArray(new int[weather.size()]);
DecisionTree.Trainer trainer = new DecisionTree.Trainer(3);
trainer.setAttributes(weather.attributes());
ClassificationMeasure[] measures = { new Accuracy(), new Recall(), new Precision() };
double[] results = Validation.loocv(trainer, x, y, measures);
for (int i = 0; i < measures.length; i++) {
System.out.println(measures[i] + " = " + results[i]);
}
assertEquals(0.6429, results[0], 1E-4);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.parser.ArffParser 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.data.parser.ArffParser in project smile by haifengl.
the class ValidationTest method testBootstrap_5args_1.
/**
* Test of bootstrap method, of class Validation.
*/
@Test
public void testBootstrap_5args_1() {
System.out.println("bootstrap");
ArffParser arffParser = new ArffParser();
arffParser.setResponseIndex(4);
try {
AttributeDataset weather = arffParser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/weather.nominal.arff"));
double[][] x = weather.toArray(new double[weather.size()][]);
int[] y = weather.toArray(new int[weather.size()]);
DecisionTree.Trainer trainer = new DecisionTree.Trainer(3);
trainer.setAttributes(weather.attributes());
ClassificationMeasure[] measures = { new Accuracy(), new Recall(), new Precision() };
double[][] results = Validation.bootstrap(100, trainer, x, y, measures);
for (int i = 0; i < 100; i++) {
for (int j = 0; j < measures.length; j++) {
System.out.format("%s = %.4f\t", measures[j], results[i][j]);
}
System.out.println();
}
System.out.println("On average:");
double[] avg = Math.colMean(results);
for (int j = 0; j < measures.length; j++) {
System.out.format("%s = %.4f\t", measures[j], avg[j]);
}
} catch (Exception ex) {
System.err.println(ex);
}
}
Aggregations