use of smile.data.AttributeDataset 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.AttributeDataset 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.data.AttributeDataset in project smile by haifengl.
the class AdaBoostTest method testUSPS10.
/**
* Test of learn method, of class AdaBoost.
*/
@Test
public void testUSPS10() {
System.out.println("USPS 10 classes");
DelimitedTextParser parser = new DelimitedTextParser();
parser.setResponseIndex(new NominalAttribute("class"), 0);
try {
AttributeDataset train = parser.parse("USPS Train", smile.data.parser.IOUtils.getTestDataFile("usps/zip.train"));
AttributeDataset test = parser.parse("USPS Test", smile.data.parser.IOUtils.getTestDataFile("usps/zip.test"));
double[][] x = train.toArray(new double[train.size()][]);
int[] y = train.toArray(new int[train.size()]);
double[][] testx = test.toArray(new double[test.size()][]);
int[] testy = test.toArray(new int[test.size()]);
AdaBoost forest = new AdaBoost(x, y, 100, 64);
int error = 0;
for (int i = 0; i < testx.length; i++) {
if (forest.predict(testx[i]) != testy[i]) {
error++;
}
}
System.out.println("AdaBoost error = " + error);
System.out.format("USPS error rate = %.2f%%%n", 100.0 * error / testx.length);
double[] accuracy = forest.test(testx, testy);
for (int i = 1; i <= accuracy.length; i++) {
System.out.format("%d trees accuracy = %.2f%%%n", i, 100.0 * accuracy[i - 1]);
}
double[] importance = forest.importance();
int[] index = QuickSort.sort(importance);
for (int i = importance.length; i-- > 0; ) {
System.out.format("%s importance is %.4f%n", train.attributes()[index[i]], importance[i]);
}
assertTrue(error <= 170);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.AttributeDataset 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.data.AttributeDataset 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