use of smile.data.parser.ArffParser 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);
}
}
use of smile.data.parser.ArffParser 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.data.parser.ArffParser 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.data.parser.ArffParser in project smile by haifengl.
the class SVMTest method testSegment.
/**
* Test of learn method, of class SVM.
*/
@Test
public void testSegment() {
System.out.println("Segment");
ArffParser parser = new ArffParser();
parser.setResponseIndex(19);
try {
AttributeDataset train = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/segment-challenge.arff"));
AttributeDataset test = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/segment-test.arff"));
System.out.println(train.size() + " " + test.size());
double[][] x = train.toArray(new double[0][]);
int[] y = train.toArray(new int[0]);
double[][] testx = test.toArray(new double[0][]);
int[] testy = test.toArray(new int[0]);
SVM<double[]> svm = new SVM<>(new GaussianKernel(8.0), 5.0, Math.max(y) + 1, SVM.Multiclass.ONE_VS_ALL);
svm.learn(x, y);
svm.finish();
int error = 0;
for (int i = 0; i < testx.length; i++) {
if (svm.predict(testx[i]) != testy[i]) {
error++;
}
}
System.out.format("Segment error rate = %.2f%%%n", 100.0 * error / testx.length);
assertTrue(error < 70);
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of smile.data.parser.ArffParser 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);
}
}
Aggregations