ClassificationClass.java Source code

Java tutorial

Introduction

Here is the source code for ClassificationClass.java

Source

import java.util.logging.Level;
import java.util.logging.Logger;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.functions.SMO;
import weka.classifiers.lazy.IBk;
import weka.classifiers.trees.J48;
import weka.core.Instances;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author ferhat
 */
public class ClassificationClass {

    public Evaluation cls_svm(Instances data) {
        Evaluation eval = null;
        try {
            Classifier classifier;
            data.setClassIndex(data.numAttributes() - 1);
            classifier = new SMO();
            classifier.buildClassifier(data);
            eval = new Evaluation(data);
            eval.evaluateModel(classifier, data);

        } catch (Exception ex) {
            Logger.getLogger(ClassificationClass.class.getName()).log(Level.SEVERE, null, ex);
        }
        return eval;
    }

    public Evaluation cls_knn(Instances data) {
        Evaluation eval = null;
        try {
            Classifier classifier;
            data.setClassIndex(data.numAttributes() - 1);
            classifier = new IBk();
            classifier.buildClassifier(data);
            eval = new Evaluation(data);
            eval.evaluateModel(classifier, data);

            System.out.println(eval.weightedFMeasure());
        } catch (Exception ex) {
            Logger.getLogger(ClassificationClass.class.getName()).log(Level.SEVERE, null, ex);
        }
        return eval;
    }

    public Evaluation cls_naivebayes(Instances data) {
        Evaluation eval = null;
        try {
            Classifier classifier;
            PreparingSteps preparingSteps = new PreparingSteps();
            data.setClassIndex(data.numAttributes() - 1);
            classifier = new NaiveBayes();
            classifier.buildClassifier(data);
            eval = new Evaluation(data);
            eval.evaluateModel(classifier, data);

            System.out.println(eval.toSummaryString());
        } catch (Exception ex) {
            Logger.getLogger(ClassificationClass.class.getName()).log(Level.SEVERE, null, ex);
        }
        return eval;
    }

    public Evaluation cls_c4_5(Instances data) {
        Evaluation eval = null;
        try {
            Classifier classifier;
            PreparingSteps preparingSteps = new PreparingSteps();
            data.setClassIndex(data.numAttributes() - 1);
            classifier = new J48();
            classifier.buildClassifier(data);
            eval = new Evaluation(data);
            eval.evaluateModel(classifier, data);

            System.out.println(eval.toSummaryString());
        } catch (Exception ex) {
            Logger.getLogger(ClassificationClass.class.getName()).log(Level.SEVERE, null, ex);
        }
        return eval;
    }
}