我已经写了这个方法。我想写一个贝叶斯网络,但我在classifyInstance()
方法上遇到了一个异常。
这是我的代码:
public static double bayesNet1(Dataset data, Dataset testingSet) throws Exception {
Instances insts = convertTxtToARFF(data);
K2 learner = new K2();
MultiNomialBMAEstimator estimator = new MultiNomialBMAEstimator();
estimator.setUseK2Prior(true);
EditableBayesNet bn = new EditableBayesNet(insts);
bn.initStructure();
learner.buildStructure(bn, insts);
estimator.estimateCPTs(bn);
double error = 0;
Instances instsTest = convertTxtToARFF(testingSet);
for(int i=0; i<instsTest.numInstances()-1; i++) {
weka.core.Instance inst = instsTest.instance(i);
double predictedValue = bn.classifyInstance(inst);
if(inst.value(inst.classIndex())!= predictedValue)
error++;
}
return error/instsTest.numInstances();
}
这是一个例外:
java.lang.ArrayIndexOutOfBoundsException: 4 at weka.classifiers.bayes.net.estimate.DiscreteEstimatorBayes.getProbability(DiscreteEstimatorBayes.java:106) at weka.classifiers.bayes.net.estimate.SimpleEstimator.distributionForInstance(SimpleEstimator.java:183) at weka.classifiers.bayes.BayesNet.distributionForInstance(BayesNet.java:386) at weka.classifiers.Classifier.classifyInstance(Classifier.java:84) at ensembleClassifiersV2.EnsembleClassifierV2.bayesNet1(EnsembleClassifierV2.java:1090) at ensembleClassifiersV2.EnsembleClassifierV2.performing (EnsembleClassifierV2.java:800) 在 ensembleClassifiersV2.EnsembleClassifierV2.main(EnsembleClassifierV2.java:1267)
谁能帮我出什么问题?