use of smile.validation.LOOCV 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.validation.LOOCV in project smile by haifengl.
the class RandomForestTest method testWeather.
/**
* Test of learn method, of class RandomForest.
*/
@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]);
RandomForest forest = new RandomForest(weather.attributes(), trainx, trainy, 100);
if (y[loocv.test[i]] != forest.predict(x[loocv.test[i]]))
error++;
}
System.out.println("Random Forest error = " + error);
assertTrue(error <= 7);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.validation.LOOCV in project smile by haifengl.
the class AdaBoostTest method testWeather.
/**
* Test of learn method, of class AdaBoost.
*/
@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]);
AdaBoost forest = new AdaBoost(weather.attributes(), trainx, trainy, 200, 4);
if (y[loocv.test[i]] != forest.predict(x[loocv.test[i]]))
error++;
}
System.out.println("AdaBoost error = " + error);
assertEquals(3, error);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.validation.LOOCV in project smile by haifengl.
the class LogisticRegressionTest method testIris2.
/**
* Test of learn method, of class LogisticRegression.
*/
@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]);
LogisticRegression logit = new LogisticRegression(trainx, trainy);
if (y[loocv.test[i]] != logit.predict(x[loocv.test[i]]))
error++;
}
System.out.println("Logistic Regression error = " + error);
assertEquals(3, error);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.validation.LOOCV in project smile by haifengl.
the class LogisticRegressionTest method testIris.
/**
* Test of learn method, of class LogisticRegression.
*/
@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]);
LogisticRegression logit = new LogisticRegression(trainx, trainy);
if (y[loocv.test[i]] != logit.predict(x[loocv.test[i]]))
error++;
}
System.out.println("Logistic Regression error = " + error);
assertEquals(3, error);
} catch (Exception ex) {
System.err.println(ex);
}
}
Aggregations