Generating+classifier+evaluation+output+manually

toc

In the following some code snippets that explain how to generate the output Weka generates when one runs a classifier from the commandline. When referring to the class, the  class is meant. This article provides only a quick overview, for more details, please see the [|Javadoc] of the class.

= Model = A classifier's model, if that classifier supports the output of it, can be simply output by using the method after it got trained: code format="java" Instances data = ... // from somewhere Classifier cls = new weka.classifiers.trees.J48; cls.buildClassifier(data); System.out.println(cls); code
 * NB:** Weka always outputs the model based on the //full// training set (provided with the option ), no matter whether cross-validation is used or a designated test set (via ). The 10 models generated during a 10-fold cross-validation run are never output. If you want to output these models you have to simulate the method yourself (see article Generating cross-validation folds (Java approach)), or use the KnowledgeFlow (see article Displaying results of cross-validation folds).

= Statistics = The statistics, also called the summary of an evaluation, can be be generated via the methods. Here is an example of the summary from a cross-validated J48: code format="java" Classifier cls = new J48; Evaluation eval = new Evaluation(data); Random rand = new Random(1); // using seed = 1 int folds = 10; eval.crossValidateModel(cls, data, folds, rand); System.out.println(eval.toSummaryString); code

= Detailed class statistics = In order to generate the detailed statistics per class (on the commandline via option ), one can use the methods. Once again a code snippet featuring a cross-validated J48: code format="java" Classifier cls = new J48; Evaluation eval = new Evaluation(data); Random rand = new Random(1); // using seed = 1 int folds = 10; eval.crossValidateModel(cls, data, folds, rand); System.out.println(eval.toClassDetailsString); code

= Confusion matrix = The confusion matrix is simply output with the or  method of the  class. In the following an example of cross-validating J48 on a dataset and outputting the confusion matrix to stdout. code format="java" Classifier cls = new J48; Evaluation eval = new Evaluation(data); Random rand = new Random(1); // using seed = 1 int folds = 10; eval.crossValidateModel(cls, data, folds, rand); System.out.println(eval.toMatrixString); code

= See also =
 * Use Weka in your Java code - general overview of the Weka API