我在Java代码中使用weka,我知道CrossValidation可用于评估模型。但是,我需要使用CrossValidation方法训练模型,然后将相同模型应用于测试集。 我的问题是:在Java代码中使用weka时,此选项是否可能? 我正在使用以下代码,但是由于使用了crossValidation而不是训练来评估,因此无法产生理想的结果。 任何帮助表示赞赏。
//BUILD THE MODEL ON TRAIN SET
J48 treeClassifier = new J48();
treeClassifier.setOptions(options);
treeClassifier.buildClassifier(TrainSet);
//SET THE OPTIONS FOR CROSS-VALIDATION METHODE
Random rand = new Random(1);
int NoFolds = 10;
ThresholdCurve tc = new ThresholdCurve();
//EVALUATE THE MODEL ON TRAIN SET
Evaluation ModelEvalution= new Evaluation(TrainSet);
ModelEvalution.evaluateModel(treeClassifier, TrainSet);
System.out.println("Train without cross Validation");
System.out.println(ModelEvalution.toSummaryString());
//APPLY CROSS-VALIDATION ON TRAIN SET
ModelEvalution.crossValidateModel(treeClassifier,TrainSet,NoFolds,rand);
//PRINT MODEL EVALUTIION ON TRAIN SET
System.out.println(ModelEvalution.toSummaryString());
//EVALUATE THE MODEL ON TEST SET
Evaluation ModelEvalutionTest= new Evaluation(TestSet);
ModelEvalutionTest.evaluateModel(treeClassifier, TestSet);
//PRINT MODEL EVALUATION ON TEST SET
System.out.println("The testing results");
System.out.println(ModelEvalutionTest.toSummaryString());