-
Notifications
You must be signed in to change notification settings - Fork 0
/
Naive Bayes Weka
84 lines (63 loc) · 2.34 KB
/
Naive Bayes Weka
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package weka.distributed;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.BufferedReader;
import java.io.StringReader;
import org.junit.Test;
import weka.classifiers.bayes.NaiveBayes;
import weka.core.Instances;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Remove;
/**
* Test class for WekaScoringMapTask
*
* @author Mark Hall (mhall{[at]}pentaho{[dot]}com)
* @version $Revision: 10926 $
*/
public class WekaScoringTaskTest {
@Test
public void testScoreWithClassifier() throws Exception {
Instances train = new Instances(new BufferedReader(new StringReader(
CorrelationMatrixMapTaskTest.IRIS)));
train.setClassIndex(train.numAttributes() - 1);
NaiveBayes bayes = new NaiveBayes();
bayes.buildClassifier(train);
WekaScoringMapTask task = new WekaScoringMapTask();
task.setModel(bayes, train, train);
assertEquals(0, task.getMissingMismatchAttributeInfo().length());
assertEquals(3, task.getPredictionLabels().size());
for (int i = 0; i < train.numInstances(); i++) {
assertEquals(3, task.processInstance(train.instance(i)).length);
}
}
@Test
public void testScoreWithClassifierSomeMissingFields() throws Exception {
Instances train = new Instances(new BufferedReader(new StringReader(
CorrelationMatrixMapTaskTest.IRIS)));
train.setClassIndex(train.numAttributes() - 1);
NaiveBayes bayes = new NaiveBayes();
bayes.buildClassifier(train);
WekaScoringMapTask task = new WekaScoringMapTask();
Remove r = new Remove();
r.setAttributeIndices("1");
r.setInputFormat(train);
Instances test = Filter.useFilter(train, r);
task.setModel(bayes, train, test);
assertTrue(task.getMissingMismatchAttributeInfo().length() > 0);
assertTrue(task.getMissingMismatchAttributeInfo().equals(
"sepallength missing from incoming data\n"));
assertEquals(3, task.getPredictionLabels().size());
for (int i = 0; i < test.numInstances(); i++) {
assertEquals(3, task.processInstance(test.instance(i)).length);
}
}
public static void main(String[] args) {
try {
WekaScoringTaskTest t = new WekaScoringTaskTest();
t.testScoreWithClassifier();
t.testScoreWithClassifierSomeMissingFields();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}