use of smile.validation.LOOCV in project smile by haifengl.
the class RidgeRegressionTest method testPredict.
/**
* Test of predict method, of class RidgeRegression.
*/
@Test
public void testPredict() {
System.out.println("predict");
for (int lambda = 0; lambda <= 20; lambda += 2) {
int n = longley.length;
LOOCV loocv = new LOOCV(n);
double rss = 0.0;
for (int i = 0; i < n; i++) {
double[][] trainx = Math.slice(longley, loocv.train[i]);
double[] trainy = Math.slice(y, loocv.train[i]);
RidgeRegression ridge = new RidgeRegression(trainx, trainy, 0.01 * lambda);
double r = y[loocv.test[i]] - ridge.predict(longley[loocv.test[i]]);
rss += r * r;
}
System.out.format("LOOCV MSE with lambda %.2f = %.3f%n", 0.01 * lambda, rss / n);
}
}
use of smile.validation.LOOCV 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.validation.LOOCV 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.validation.LOOCV 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.validation.LOOCV in project smile by haifengl.
the class LDATest method testLearn.
/**
* Test of learn method, of class LDA.
*/
@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;
double[] posteriori = new double[3];
for (int i = 0; i < n; i++) {
double[][] trainx = Math.slice(x, loocv.train[i]);
int[] trainy = Math.slice(y, loocv.train[i]);
LDA lda = new LDA(trainx, trainy);
if (y[loocv.test[i]] != lda.predict(x[loocv.test[i]], posteriori))
error++;
//System.out.println(posteriori[0]+"\t"+posteriori[1]+"\t"+posteriori[2]);
}
System.out.println("LDA error = " + error);
assertEquals(22, error);
} catch (Exception ex) {
System.err.println(ex);
}
}
Aggregations