System.out.print("..Spliting complete!\n");
System.out.print("Training...\n");
af.setStopIncrement(true);
BayesTrainer trainer=new BayesTrainer();
BayesClassifier classifier= (BayesClassifier) trainer.train(trainset);
System.out.print("..Training complete!\n");
System.out.print("Saving model...\n");
classifier.saveTo(bayesModelFile);
classifier = null;
System.out.print("..Saving model complete!\n");
/**
* 测试
*/
System.out.print("Loading model...\n");
BayesClassifier bayes;
bayes =BayesClassifier.loadFrom(bayesModelFile);
System.out.print("..Loading model complete!\n");
System.out.println("Testing Bayes...");
int flag=0;
float[] percents_cs=new float[]{1.0f,0.9f,0.8f,0.7f,0.5f,0.3f,0.2f,0.1f};
int[] counts_cs=new int[10];
for(int test=0;test<percents_cs.length;test++){
System.out.println("Testing Bayes"+percents_cs[test]+"...");
if(test!=0)
bayes.fS_CS(percents_cs[test]);
int count=0;
for(int i=0;i<testset.size();i++){
Instance data = testset.getInstance(i);
Integer gold = (Integer) data.getTarget();
Predict<String> pres=bayes.classify(data, Type.STRING, 3);
String pred_label=pres.getLabel();
String gold_label = bayes.getLabel(gold);
if(pred_label.equals(gold_label)){
count++;
}
else{
flag=i;
// System.err.println(gold_label+"->"+pred_label+" : "+testset.getInstance(i).getTempData());
// for(int j=0;j<3;j++)
// System.out.println(pres.getLabel(j)+":"+pres.getScore(j));
}
}
counts_cs[test]=count;
System.out.println("Bayes Precision("+percents_cs[test]+"):"
+((float)count/testset.size())+"("+count+"/"+testset.size()+")");
}
bayes.noFeatureSelection();
float[] percents_csmax=new float[]{1.0f,0.9f,0.8f,0.7f,0.5f,0.3f,0.2f,0.1f};
int[] counts_csmax=new int[10];
for(int test=0;test<percents_csmax.length;test++){
System.out.println("Testing Bayes"+percents_csmax[test]+"...");
if(test!=0)
bayes.fS_CS_Max(percents_csmax[test]);
int count=0;
for(int i=0;i<testset.size();i++){
Instance data = testset.getInstance(i);
Integer gold = (Integer) data.getTarget();
Predict<String> pres=bayes.classify(data, Type.STRING, 3);
String pred_label=pres.getLabel();
String gold_label = bayes.getLabel(gold);
if(pred_label.equals(gold_label)){
count++;
}
else{
// System.err.println(gold_label+"->"+pred_label+" : "+testset.getInstance(i).getTempData());
// for(int j=0;j<3;j++)
// System.out.println(pres.getLabel(j)+":"+pres.getScore(j));
}
}
counts_csmax[test]=count;
System.out.println("Bayes Precision("+percents_csmax[test]+"):"
+((float)count/testset.size())+"("+count+"/"+testset.size()+")");
}
bayes.noFeatureSelection();
float[] percents_ig=new float[]{1.0f,0.9f,0.8f,0.7f,0.5f,0.3f,0.2f,0.1f};
int[] counts_ig=new int[10];
for(int test=0;test<percents_ig.length;test++){
System.out.println("Testing Bayes"+percents_ig[test]+"...");
if(test!=0)
bayes.fS_IG(percents_ig[test]);
int count=0;
for(int i=0;i<testset.size();i++){
Instance data = testset.getInstance(i);
Integer gold = (Integer) data.getTarget();
Predict<String> pres=bayes.classify(data, Type.STRING, 3);
String pred_label=pres.getLabel();
String gold_label = bayes.getLabel(gold);
if(pred_label.equals(gold_label)){
count++;
}
else{