// Build the classifier
filteredClassifier.buildClassifier(train);
// Prepare the output buffer
AbstractOutput output = new PlainText();
output.setBuffer(new StringBuffer());
output.setHeader(test);
output.setAttributes("first");
Evaluation eval = new Evaluation(train);
eval.evaluateModel(filteredClassifier, test, output);
// Convert predictions to CSV
// Format: inst#, actual, predicted, error, probability, (ID)
String[] scores = new String[new Double(eval.numInstances()).intValue()];
double[] probabilities = new double[new Double(eval.numInstances()).intValue()];
for (String line : output.getBuffer().toString().split("\n"))
{
String[] linesplit = line.split("\\s+");
// If there's been an error, the length of linesplit is 6, otherwise 5,
// due to the error flag "+"
int id;
String expectedValue, classification;
double probability;
if (line.contains("+"))
{
id = Integer.parseInt(linesplit[6].substring(1, linesplit[6].length() - 1));
expectedValue = linesplit[2].substring(2);
classification = linesplit[3].substring(2);
probability = Double.parseDouble(linesplit[5]);
} else {
id = Integer.parseInt(linesplit[5].substring(1, linesplit[5].length() - 1));
expectedValue = linesplit[2].substring(2);
classification = linesplit[3].substring(2);
probability = Double.parseDouble(linesplit[4]);
}
scores[id - 1] = classification;
probabilities[id - 1] = probability;
}
System.out.println(eval.toSummaryString());
System.out.println(eval.toMatrixString());
// Output classifications
StringBuilder sb = new StringBuilder();
for (String score : scores)
sb.append(score.toString() + LF);
FileUtils.writeStringToFile(
new File(OUTPUT_DIR + "/" + testDataset.toString() + "/" + wekaClassifier.toString() + "/" + testDataset.toString() + ".csv"),
sb.toString());
// Output probabilities
sb = new StringBuilder();
for (Double probability : probabilities)
sb.append(probability.toString() + LF);
FileUtils.writeStringToFile(
new File(OUTPUT_DIR + "/" + testDataset.toString() + "/" + wekaClassifier.toString() + "/" + testDataset.toString() + ".probabilities.csv"),
sb.toString());
// Output predictions
FileUtils.writeStringToFile(
new File(OUTPUT_DIR + "/" + testDataset.toString() + "/" + wekaClassifier.toString() + "/" + testDataset.toString() + ".predictions.txt"),
output.getBuffer().toString());
// Output meta information
sb = new StringBuilder();
sb.append(classifier.toString() + LF);
sb.append(eval.toSummaryString() + LF);