use of smile.data.parser.ArffParser in project smile by haifengl.
the class ValidationTest method testLoocv_3args_2.
/**
* Test of loocv method, of class Validation.
*/
@Test
public void testLoocv_3args_2() {
System.out.println("loocv");
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.loocv(trainer, x, y);
System.out.println("RMSE = " + rmse);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.parser.ArffParser in project smile by haifengl.
the class ValidationTest method testLoocv_3args_1.
/**
* Test of loocv method, of class Validation.
*/
@Test
public void testLoocv_3args_1() {
System.out.println("loocv");
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()]);
ClassifierTrainer<double[]> trainer = new LDA.Trainer();
double accuracy = Validation.loocv(trainer, x, y);
System.out.println("LOOCV accuracy = " + accuracy);
assertEquals(0.8533, accuracy, 1E-4);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.parser.ArffParser in project smile by haifengl.
the class ValidationTest method testCv_5args_2.
/**
* Test of cv method, of class Validation.
*/
@Test
public void testCv_5args_2() {
System.out.println("cv");
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.cv(10, trainer, x, y, measures);
System.out.println("RMSE = " + results[0]);
System.out.println("Absolute Deviation = " + results[1]);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.parser.ArffParser in project smile by haifengl.
the class RidgeRegressionTest method testCPU.
/**
* Test of learn method, of class LinearRegression.
*/
@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[][] datax = data.toArray(new double[data.size()][]);
double[] datay = data.toArray(new double[data.size()]);
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]);
RidgeRegression ridge = new RidgeRegression(trainx, trainy, 10.0);
for (int j = 0; j < testx.length; j++) {
double r = testy[j] - ridge.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 SVRTest method testCPU.
/**
* Test of learn method, of class SVR.
*/
@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()][]);
Math.standardize(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]);
SVR<double[]> svr = new SVR<>(trainx, trainy, new PolynomialKernel(3, 1.0, 1.0), 0.1, 1.0);
for (int j = 0; j < testx.length; j++) {
double r = testy[j] - svr.predict(testx[j]);
rss += r * r;
}
}
System.out.println("10-CV RMSE = " + Math.sqrt(rss / n));
} catch (Exception ex) {
System.err.println(ex);
}
}
Aggregations