我寫過這個方法。我想寫一個貝葉斯網絡,但我在classifyInstance()
方法上遇到了一個異常。weka.classifiers.Classifier.classifyInstance中的ArrayIndexOutOfBoundsException
這裏是我的代碼:
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在 weka.classifiers.bayes.net.estimate.DiscreteEstimatorBayes.getProbability (DiscreteEstimatorBayes.java:106) at weka.classifiers.bayes.net.estimate.SimpleEstimator.distributionForInstance(SimpleEstimator.java:183) at weka.classifie rs.bayes.BayesNet.distributionForInstance(BayesNet.java:386) 在weka.classifiers.Classifier.classifyInstance(Classifier.java:84) 在 ensembleClassifiersV2.EnsembleClassifierV2.bayesNet1(EnsembleClassifierV2.java:1090) 在 ensembleClassifiersV2。 EnsembleClassifierV2.performing(EnsembleClassifierV2.java:800) 在 ensembleClassifiersV2.EnsembleClassifierV2.main(EnsembleClassifierV2.java:1267)
誰能幫我什麼是錯?