【问题标题】:Run multiple classifiers on arff files in weka automatically在 weka 中自动对 arff 文件运行多个分类器
【发布时间】:2016-03-06 04:55:13
【问题描述】:
【问题讨论】:
标签:
machine-learning
classification
weka
arff
【解决方案1】:
如果您有信心从自己的 Java 代码中运行 Weka,您可以创建一个包含您选择的分类器和文件的数组,并在它们中循环进行测试。代码将是这样的(训练和测试之间的比例为 80/20):
String[] filePaths = {"/some/data1.arff", "/some/data2.arff", "/some/data3.arff"};
for (String path : filePaths) {
DataSource source = new DataSource(path);
Instances data = source.getDataSet();
if (data.classIndex() == -1) {
data.setClassIndex(data.numAttributes() - 1);
}
data.randomize(new java.util.Random(0));
int trainSize = (int) Math.round(data.numInstances() * 0.8);
int testSize = data.numInstances() - trainSize;
Instances train = new Instances(data, 0, trainSize);
Instances test = new Instances(data, trainSize, testSize);
Classifier[] classifiers = {new NaiveBayes, new J48, new MultilayerPerceptron};
for (Classifier c : classifiers) {
Classifier cls = c;
cls.buildClassifier(train);
Evaluation eval = new Evaluation(train);
eval.evaluateModel(cls, test);
System.out.println(eval.toSummaryString("\nResults\n======\n", false));
}
}
【解决方案2】:
您可以从命令行运行 Weka。您首先需要将 Weka.jar 添加到您的类路径中。然后就可以通过命令行开始读取文件、运行过滤器、分类器等。您可以从Weka Primer 轻松开始。
例如,您可以使用以下命令运行 randomForest:
java weka.classifiers.trees.RandomForest -I 10 -S 1 -t data.arff -x 10
地点:
-I <number of trees> Specifies the number of trees to build.
-S <seed number> Specifies the seed for random number generator.
-t <name of training file> Sets training file.
-x <number of folds> Sets number of folds for cross-validation
你可以找到详细的规格here。
如果你想使用拆分百分比而不是交叉验证,你只需替换
-x <number of folds>
与
-split-percentage <percentage>
或者,您可以通过 -T 指定单独的测试集:
-T <name of test file> Sets test file.
如果您按照说明运行命令,结果将打印在命令行中。如果您想将它们保存在单独的文件中,您可以按照建议here