use of smile.data.parser.ArffParser 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.data.parser.ArffParser in project smile by haifengl.
the class DecisionTreeTest method testWeather.
/**
* Test of learn method, of class DecisionTree.
*/
@Test
public void testWeather() {
System.out.println("Weather");
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()]);
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]);
DecisionTree tree = new DecisionTree(weather.attributes(), trainx, trainy, 3);
if (y[loocv.test[i]] != tree.predict(x[loocv.test[i]]))
error++;
}
System.out.println("Decision Tree error = " + error);
assertEquals(5, error);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.parser.ArffParser in project smile by haifengl.
the class GradientTreeBoostTest method testIris2.
/**
* Test of predict method, of class GradientTreeBoost.
*/
@Test
public void testIris2() {
System.out.println("Iris binary");
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()]);
for (int i = 0; i < y.length; i++) {
if (y[i] == 2) {
y[i] = 1;
} else {
y[i] = 0;
}
}
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]);
GradientTreeBoost boost = new GradientTreeBoost(iris.attributes(), trainx, trainy, 100);
if (y[loocv.test[i]] != boost.predict(x[loocv.test[i]]))
error++;
}
System.out.println("Gradient Tree Boost error = " + error);
//assertEquals(6, error);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.parser.ArffParser in project smile by haifengl.
the class GradientTreeBoostTest method testIris.
/**
* Test of learn method, of class GradientTreeBoost.
*/
@Test
public void testIris() {
System.out.println("Iris");
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]);
GradientTreeBoost boost = new GradientTreeBoost(iris.attributes(), trainx, trainy, 100);
if (y[loocv.test[i]] != boost.predict(x[loocv.test[i]]))
error++;
}
System.out.println("Gradient Tree Boost error = " + error);
//assertEquals(6, error);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.parser.ArffParser in project smile by haifengl.
the class KNNTest method testLearn_3args.
/**
* Test of learn method, of class KNN.
*/
@Test
public void testLearn_3args() {
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[0][]);
int[] y = iris.toArray(new int[0]);
KNN<double[]> knn = KNN.learn(x, y, 1);
int error = 0;
for (int i = 0; i < x.length; i++) {
if (knn.predict(x[i]) != y[i]) {
error++;
}
}
System.out.println("1-nn error = " + error);
assertEquals(6, error);
knn = KNN.learn(x, y, 3);
error = 0;
for (int i = 0; i < x.length; i++) {
if (knn.predict(x[i]) != y[i]) {
error++;
}
}
System.out.println("3-nn error = " + error);
assertEquals(6, error);
knn = KNN.learn(x, y, 5);
error = 0;
for (int i = 0; i < x.length; i++) {
if (knn.predict(x[i]) != y[i]) {
error++;
}
}
System.out.println("5-nn error = " + error);
assertEquals(5, error);
knn = KNN.learn(x, y, 7);
error = 0;
for (int i = 0; i < x.length; i++) {
if (knn.predict(x[i]) != y[i]) {
error++;
}
}
System.out.println("7-nn error = " + error);
assertEquals(5, error);
knn = KNN.learn(x, y, 9);
error = 0;
for (int i = 0; i < x.length; i++) {
if (knn.predict(x[i]) != y[i]) {
error++;
}
}
System.out.println("9-nn error = " + error);
assertEquals(5, error);
knn = KNN.learn(x, y, 11);
error = 0;
for (int i = 0; i < x.length; i++) {
if (knn.predict(x[i]) != y[i]) {
error++;
}
}
System.out.println("11-nn error = " + error);
assertEquals(4, error);
knn = KNN.learn(x, y, 13);
error = 0;
for (int i = 0; i < x.length; i++) {
if (knn.predict(x[i]) != y[i]) {
error++;
}
}
System.out.println("13-nn error = " + error);
assertEquals(5, error);
knn = KNN.learn(x, y, 15);
error = 0;
for (int i = 0; i < x.length; i++) {
if (knn.predict(x[i]) != y[i]) {
error++;
}
}
System.out.println("15-nn error = " + error);
assertEquals(4, error);
} catch (Exception ex) {
System.err.println(ex);
}
}
Aggregations