如何在Weka Java中获取Evaluation对象的准确率?
Got it, I’ve been there before—when you just need that one specific metric instead of sifting through the whole summary dump. Here’s how you can grab just the accuracy from your Weka Evaluation object after completing cross-validation:
直接调用Evaluation的内置方法
Weka’s Evaluation class has dedicated methods to fetch individual metrics, so you don’t have to parse the summary string at all.
If you’ve already finished your cross-validation (whether via the built-in crossValidateModel() method or manual fold handling) and have your eval object ready:
// 获取准确率百分比(范围0-100) double accuracyPercentage = eval.pctCorrect(); System.out.println(folds + "-fold Cross-validation Accuracy: " + accuracyPercentage + "%");
可选:格式化输出(更美观)
If you want cleaner, rounded output (like two decimal places), use DecimalFormat to tidy up the number:
import java.text.DecimalFormat; // ... double accuracy = eval.pctCorrect(); DecimalFormat formatter = new DecimalFormat("#.##"); System.out.println(folds + "-fold Cross-validation Accuracy: " + formatter.format(accuracy) + "%");
其他相关方法(按需使用)
eval.correct(): 返回正确分类的实例总数eval.numInstances(): 返回参与评估的总实例数- 你也可以手动计算准确率:
(double) eval.correct() / eval.numInstances() * 100
只要你的Evaluation对象已经正确汇总了所有交叉验证折的结果(比如手动处理时,每个折都调用了eval.evaluateModel(classifier, testSet)更新统计),这些方法都能正常工作。
内容的提问来源于stack exchange,提问作者User505

