Removing+misclassified+instances+from+dataset

toc Sometimes it is necessary to clean out the instances misclassified by a classifier from a dataset. The following example loads a dataset, runs the filter and saves the resulting dataset in another file again: code format="bash" RemoveMisclassifiedTest   code Source code: code format="java" import weka.classifiers.Classifier; import weka.core.Instances; import weka.filters.Filter; import weka.filters.unsupervised.instance.RemoveMisclassified; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; /**  * Runs the RemoveMisclassified filter over a given ARFF file. * First parameter is the input file, the second the classifier * to use and the third one is the output file. *  * Usage: RemoveMisclassifiedTest input.arff classname output.arff *  * @author FracPete (fracpete at waikato dot ac dot nz) */ public class RemoveMisclassifiedTest { public static void main(String[] args) throws Exception { if (args.length != 3) { System.out.println("\nUsage: RemoveMisclassifiedTest input.arff classname output.arff\n"); System.exit(1); }     // get data Instances input = new Instances(         new BufferedReader(new FileReader(args[0]))); input.setClassIndex(input.numAttributes - 1); // get classifier Classifier c = Classifier.forName(args[1], new String[0]); // setup and run filter RemoveMisclassified filter = new RemoveMisclassified; filter.setClassifier(c); filter.setClassIndex(-1); filter.setNumFolds(0); filter.setThreshold(0.1); filter.setMaxIterations(0); filter.setInputFormat(input); Instances output = Filter.useFilter(input, filter); // output file BufferedWriter writer = new BufferedWriter(new FileWriter(args[2])); writer.write(output.toString); writer.newLine; writer.flush; writer.close; } } code

= See also =
 * Use Weka in your Java code - for general use of the Weka API
 * Save Instances to an ARFF File - for saving an Instances object to a file

= Downloads =
 * [[file:RemoveMisclassifiedTest.java]] ([|book], [|stable-3.6], [|developer])