use of smile.validation.CrossValidation in project smile by haifengl.
the class NaiveBayesTest method testLearnMultinomial2.
/**
* Test of learn method, of class SequenceNaiveBayes.
*/
@Test
public void testLearnMultinomial2() {
System.out.println("online learn Multinomial");
double[][] x = moviex;
int[] y = moviey;
int n = x.length;
int k = 10;
CrossValidation cv = new CrossValidation(n, k);
int error = 0;
int total = 0;
for (int i = 0; i < k; i++) {
double[][] trainx = Math.slice(x, cv.train[i]);
int[] trainy = Math.slice(y, cv.train[i]);
NaiveBayes bayes = new NaiveBayes(NaiveBayes.Model.MULTINOMIAL, 2, feature.length);
for (int j = 0; j < trainx.length; j++) {
bayes.learn(trainx[j], trainy[j]);
}
double[][] testx = Math.slice(x, cv.test[i]);
int[] testy = Math.slice(y, cv.test[i]);
for (int j = 0; j < testx.length; j++) {
int label = bayes.predict(testx[j]);
if (label != -1) {
total++;
if (testy[j] != label) {
error++;
}
}
}
}
System.out.format("Multinomial error = %d of %d%n", error, total);
assertTrue(error < 265);
}
use of smile.validation.CrossValidation in project smile by haifengl.
the class NaiveBayesTest method testLearnBernoulli.
/**
* Test of learn method, of class SequenceNaiveBayes.
*/
@Test
public void testLearnBernoulli() {
System.out.println("batch learn Bernoulli");
double[][] x = moviex;
int[] y = moviey;
int n = x.length;
int k = 10;
CrossValidation cv = new CrossValidation(n, k);
int error = 0;
int total = 0;
for (int i = 0; i < k; i++) {
double[][] trainx = Math.slice(x, cv.train[i]);
int[] trainy = Math.slice(y, cv.train[i]);
NaiveBayes bayes = new NaiveBayes(NaiveBayes.Model.BERNOULLI, 2, feature.length);
bayes.learn(trainx, trainy);
double[][] testx = Math.slice(x, cv.test[i]);
int[] testy = Math.slice(y, cv.test[i]);
for (int j = 0; j < testx.length; j++) {
int label = bayes.predict(testx[j]);
if (label != -1) {
total++;
if (testy[j] != label) {
error++;
}
}
}
}
System.out.format("Bernoulli error = %d of %d%n", error, total);
assertTrue(error < 270);
}
use of smile.validation.CrossValidation in project smile by haifengl.
the class HMMPOSTaggerTest method testBrown.
/**
* Test of learn method, of class HMMPOSTagger.
*/
@Test
public void testBrown() {
System.out.println("BROWN");
load("D:\\sourceforge\\corpora\\PennTreebank\\PennTreebank2\\TAGGED\\POS\\BROWN");
String[][] x = sentences.toArray(new String[sentences.size()][]);
PennTreebankPOS[][] y = labels.toArray(new PennTreebankPOS[labels.size()][]);
int n = x.length;
int k = 10;
CrossValidation cv = new CrossValidation(n, k);
int error = 0;
int total = 0;
for (int i = 0; i < k; i++) {
String[][] trainx = Math.slice(x, cv.train[i]);
PennTreebankPOS[][] trainy = Math.slice(y, cv.train[i]);
String[][] testx = Math.slice(x, cv.test[i]);
PennTreebankPOS[][] testy = Math.slice(y, cv.test[i]);
HMMPOSTagger tagger = HMMPOSTagger.learn(trainx, trainy);
for (int j = 0; j < testx.length; j++) {
PennTreebankPOS[] label = tagger.tag(testx[j]);
total += label.length;
for (int l = 0; l < label.length; l++) {
if (label[l] != testy[j][l]) {
error++;
}
}
}
}
System.out.format("Error rate = %.2f as %d of %d\n", 100.0 * error / total, error, total);
}
use of smile.validation.CrossValidation in project smile by haifengl.
the class HMMPOSTaggerTest method testWSJ.
/**
* Test of learn method, of class HMMPOSTagger.
*/
@Test
public void testWSJ() {
System.out.println("WSJ");
load("D:\\sourceforge\\corpora\\PennTreebank\\PennTreebank2\\TAGGED\\POS\\WSJ");
String[][] x = sentences.toArray(new String[sentences.size()][]);
PennTreebankPOS[][] y = labels.toArray(new PennTreebankPOS[labels.size()][]);
int n = x.length;
int k = 10;
CrossValidation cv = new CrossValidation(n, k);
int error = 0;
int total = 0;
for (int i = 0; i < k; i++) {
String[][] trainx = Math.slice(x, cv.train[i]);
PennTreebankPOS[][] trainy = Math.slice(y, cv.train[i]);
String[][] testx = Math.slice(x, cv.test[i]);
PennTreebankPOS[][] testy = Math.slice(y, cv.test[i]);
HMMPOSTagger tagger = HMMPOSTagger.learn(trainx, trainy);
for (int j = 0; j < testx.length; j++) {
PennTreebankPOS[] label = tagger.tag(testx[j]);
total += label.length;
for (int l = 0; l < label.length; l++) {
if (label[l] != testy[j][l]) {
error++;
}
}
}
}
System.out.format("Error rate = %.2f as %d of %d\n", 100.0 * error / total, error, total);
}
use of smile.validation.CrossValidation in project smile by haifengl.
the class RegressionTreeTest method test.
public void test(String dataset, String url, int response) {
System.out.println(dataset);
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]);
RegressionTree tree = new RegressionTree(data.attributes(), trainx, trainy, 20);
for (int j = 0; j < testx.length; j++) {
double r = testy[j] - tree.predict(testx[j]);
rss += r * r;
ad += Math.abs(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);
}
}
Aggregations