use of smile.data.parser.ArffParser in project smile by haifengl.
the class SVMTest method testLearn.
/**
* Test of learn method, of class SVM.
*/
@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()]);
SVM<double[]> svm = new SVM<>(new LinearKernel(), 10.0, Math.max(y) + 1, SVM.Multiclass.ONE_VS_ALL);
svm.learn(x, y);
svm.learn(x, y);
svm.finish();
int error = 0;
for (int i = 0; i < x.length; i++) {
if (svm.predict(x[i]) != y[i]) {
error++;
}
}
System.out.println("Linear ONE vs. ALL error = " + error);
assertTrue(error <= 10);
svm = new SVM<>(new GaussianKernel(1), 1.0, Math.max(y) + 1, SVM.Multiclass.ONE_VS_ALL);
svm.learn(x, y);
svm.learn(x, y);
svm.finish();
svm.trainPlattScaling(x, y);
error = 0;
for (int i = 0; i < x.length; i++) {
if (svm.predict(x[i]) != y[i]) {
error++;
}
double[] prob = new double[3];
int yp = svm.predict(x[i], prob);
//System.out.format("%d %d %.2f, %.2f %.2f\n", y[i], yp, prob[0], prob[1], prob[2]);
}
System.out.println("Gaussian ONE vs. ALL error = " + error);
assertTrue(error <= 5);
svm = new SVM<>(new GaussianKernel(1), 1.0, Math.max(y) + 1, SVM.Multiclass.ONE_VS_ONE);
svm.learn(x, y);
svm.learn(x, y);
svm.finish();
svm.trainPlattScaling(x, y);
error = 0;
for (int i = 0; i < x.length; i++) {
if (svm.predict(x[i]) != y[i]) {
error++;
}
double[] prob = new double[3];
int yp = svm.predict(x[i], prob);
//System.out.format("%d %d %.2f, %.2f %.2f\n", y[i], yp, prob[0], prob[1], prob[2]);
}
System.out.println("Gaussian ONE vs. ONE error = " + error);
assertTrue(error <= 5);
svm = new SVM<>(new PolynomialKernel(2), 1.0, Math.max(y) + 1, SVM.Multiclass.ONE_VS_ALL);
svm.learn(x, y);
svm.learn(x, y);
svm.finish();
error = 0;
for (int i = 0; i < x.length; i++) {
if (svm.predict(x[i]) != y[i]) {
error++;
}
}
System.out.println("Polynomial ONE vs. ALL error = " + error);
assertTrue(error <= 5);
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of smile.data.parser.ArffParser in project smile by haifengl.
the class GaussianProcessRegressionTest method test2DPlanes.
/**
* Test of learn method, of class GaussianProcessRegression.
*/
@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[][] x = data.toArray(new double[data.size()][]);
double[] y = data.toArray(new double[data.size()]);
int[] perm = Math.permutate(x.length);
double[][] datax = new double[4000][];
double[] datay = new double[datax.length];
for (int i = 0; i < datax.length; i++) {
datax[i] = x[perm[i]];
datay[i] = y[perm[i]];
}
int n = datax.length;
int k = 10;
CrossValidation cv = new CrossValidation(n, k);
double rss = 0.0;
double sparseRSS30 = 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]);
GaussianProcessRegression<double[]> rkhs = new GaussianProcessRegression<>(trainx, trainy, new GaussianKernel(34.866), 0.1);
KMeans kmeans = new KMeans(trainx, 30, 10);
double[][] centers = kmeans.centroids();
double r0 = 0.0;
for (int l = 0; l < centers.length; l++) {
for (int j = 0; j < l; j++) {
r0 += Math.distance(centers[l], centers[j]);
}
}
r0 /= (2 * centers.length);
System.out.println("Kernel width = " + r0);
GaussianProcessRegression<double[]> sparse30 = new GaussianProcessRegression<>(trainx, trainy, centers, new GaussianKernel(r0), 0.1);
for (int j = 0; j < testx.length; j++) {
double r = testy[j] - rkhs.predict(testx[j]);
rss += r * r;
r = testy[j] - sparse30.predict(testx[j]);
sparseRSS30 += r * r;
}
}
System.out.println("Regular 10-CV MSE = " + rss / n);
System.out.println("Sparse (30) 10-CV MSE = " + sparseRSS30 / n);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.parser.ArffParser in project smile by haifengl.
the class GradientTreeBoostTest method test.
public void test(GradientTreeBoost.Loss loss, String dataset, String url, int response) {
System.out.println(dataset + "\t" + loss);
ArffParser parser = new ArffParser();
parser.setResponseIndex(response);
try {
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile(url));
double[] datay = data.toArray(new double[data.size()]);
double[][] datax = data.toArray(new double[data.size()][]);
int n = datax.length;
int k = 10;
CrossValidation cv = new CrossValidation(n, k);
double rss = 0.0;
double ad = 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]);
GradientTreeBoost boost = new GradientTreeBoost(data.attributes(), trainx, trainy, loss, 100, 6, 0.05, 0.7);
for (int j = 0; j < testx.length; j++) {
double r = testy[j] - boost.predict(testx[j]);
ad += Math.abs(r);
rss += r * r;
}
}
System.out.format("10-CV RMSE = %.4f \t AbsoluteDeviation = %.4f%n", Math.sqrt(rss / n), ad / n);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.parser.ArffParser in project smile by haifengl.
the class RBFNetworkTest method testAilerons.
/**
* Test of learn method, of class RBFNetwork.
*/
@Test
public void testAilerons() {
System.out.println("ailerons");
ArffParser parser = new ArffParser();
parser.setResponseIndex(40);
try {
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/ailerons.arff"));
double[][] datax = data.toArray(new double[data.size()][]);
Math.standardize(datax);
double[] datay = data.toArray(new double[data.size()]);
for (int i = 0; i < datay.length; i++) {
datay[i] *= 10000;
}
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 RBFNetworkTest method testBank32nh.
/**
* Test of learn method, of class RBFNetwork.
*/
@Test
public void testBank32nh() {
System.out.println("bank32nh");
ArffParser parser = new ArffParser();
parser.setResponseIndex(31);
try {
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/bank32nh.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]);
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);
}
}
Aggregations