我正在通过产品规则实现NBTree和Random forest的组合。只要我使用相同的数据集进行训练和测试,我的代码工作正常,但是当我使用新数据集进行测试时,我收到错误。这是实施。
RandomForest objRandomForest = new RandomForest();
Classifier[] objClassifiers = {objNBTree,objRandomForest};
objVote.setClassifiers(objClassifiers);
System.out.println("=== Building Model ===");
objVote.buildClassifier(train);
Evaluation eval = new Evaluation(test);
eval.evaluateModel(objVote, test);
System.out.println(eval.toSummaryString("\nResults\n===============\n", true));
数据集包含许多带有数值的属性,因此我也将它们离散化,以下是代码
BufferedReader reader = new BufferedReader(
new FileReader("C:/Users/MUHAMMAD GALA/Documents/CN/Project/NSL_KDD-master/test1000b.arff"));
Instances data = new Instances(reader);
String[] options = new String[2];
options[0] = "-R";
options[1] = "First-last";
Discretize d = new Discretize();
d.setOptions(options);
d.setInputFormat(data);
Instances newData = Filter.useFilter(data, d);
ArffSaver saver = new ArffSaver();
saver.setInstances(newData);
saver.setFile(new File("C:/Users/MUHAMMAD GALA/Documents/CN/Project/NSL_KDD-master/test1000c.arff"));
saver.writeBatch();
我得到的错误是:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
at weka.classifiers.trees.j48.NBTreeNoSplit.classProb(Unknown Source)
at weka.classifiers.trees.j48.ClassifierTree.getProbs(Unknown Source)
at weka.classifiers.trees.j48.ClassifierTree.getProbs(Unknown Source)
at weka.classifiers.trees.j48.ClassifierTree.distributionForInstance(Unknown Source)
at weka.classifiers.trees.NBTree.distributionForInstance(Unknown Source)
at weka.classifiers.meta.Vote.distributionForInstanceProduct(Unknown Source)
at weka.classifiers.meta.Vote.distributionForInstance(Unknown Source)
at weka.classifiers.Evaluation.evaluateModelOnceAndRecordPrediction(Unknown Source)
at weka.classifiers.Evaluation.evaluateModel(Unknown Source)
at combinationkitesting.CombinationKiTesting.MajorityVotePrediction(CombinationKiTesting.java:88)
at combinationkitesting.CombinationKiTesting.main(CombinationKiTesting.java:54)