Search code examples
weka

Get Weka Classifier Results


I am new to Weka and trying to run classifiers on a csv data file. I am able to print the results by getting them in string from classifier.toString(), evaluation.toSummaryString(), evaluation.toMatrixString() method.

But my client's requirement is to return the variables values in json object instead of the output format given by Weka. Can we get all the variables values separately so that I can set them to my custom model and return that as json.

e.g. evaluationSummaryString=
Correctly Classified Instances           4               28.5714 %
Incorrectly Classified Instances        10               71.4286 %
Kappa statistic                         -0.0769
Mean absolute error                      0.4722
Root mean squared error                  0.6514
Relative absolute error                101.9709 %
Root relative squared error            132.1523 %
Coverage of cases (0.95 level)          50      %
Mean rel. region size (0.95 level)      45.2381 %
Total Number of Instances               14     

Can I get above name value pairs read separately instead having them in string.


Solution

  • You can extract the majority of the values you are interested in directly from the evaluation object. I am unsure of "Coverage of cases", and "mean rel. region". The rest can be done as follows:

    Instances train = // load train instances ...
    Instances test = // load test instances ...
    
    
    // build and evaluate a model
    J48 classifier = new J48();
    classifier.buildClassifier(train);
    Evaluation eval = new Evaluation(train);
    eval.evaluateModel(classifier, test);
    
    // extract the values of interest
    double numberCorrect = eval.correct();
    double numberIncorrect = eval.incorrect();
    double pctCorrect = eval.pctCorrect();
    double pctIncorrect = eval.pctIncorrect();
    double Kappa = eval.kappa();
    double MeanAbsoluteError = eval.meanAbsoluteError();
    double RootMeanSquaredError = eval.rootMeanSquaredError();
    double RelativeAbsoluteError = eval.relativeAbsoluteError();
    double RootRelativeSquaredError = eval.rootRelativeSquaredError();
    double TotalNumberOfInstances = eval.numInstances();