Example usage for weka.classifiers Evaluation evaluateModel

List of usage examples for weka.classifiers Evaluation evaluateModel

Introduction

In this page you can find the example usage for weka.classifiers Evaluation evaluateModel.

Prototype

public static String evaluateModel(Classifier classifier, String[] options) throws Exception 

Source Link

Document

Evaluates a classifier with the options given in an array of strings.

Usage

From source file:Example.java

License:Open Source License

public static void main(String[] argv) throws Exception {
    if (argv.length < 1) {
        System.out.println("Usage: Example <arff file>");
        System.exit(1);//w w  w .j av a2s.  com
    }

    String dataFile = argv[0]; // input arff file

    WLSVM lib = new WLSVM();

    String[] ops = { new String("-t"), dataFile, new String("-x"), // 5 folds CV
            new String("5"), new String("-i"), //
            //---------------
            new String("-S"), // WLSVM options
            new String("0"), // Classification problem
            new String("-K"), // RBF kernel
            new String("2"), new String("-G"), // gamma
            new String("1"), new String("-C"), // C
            new String("7"), new String("-Z"), // normalize input data
            new String("1"), new String("-M"), // cache size in MB
            new String("100") };

    System.out.println(Evaluation.evaluateModel(lib, ops));
}

From source file:CrossValidationMultipleRuns.java

License:Open Source License

/**
 * Performs the cross-validation. See Javadoc of class for information
 * on command-line parameters.//from   ww w  .jav  a  2  s  . c  o m
 *
 * @param args   the command-line parameters
 * @throws Exception   if something goes wrong
 */
public static void main(String[] args) throws Exception {
    // loads data and set class index
    Instances data = DataSource.read(Utils.getOption("t", args));
    String clsIndex = Utils.getOption("c", args);
    if (clsIndex.length() == 0)
        clsIndex = "last";
    if (clsIndex.equals("first"))
        data.setClassIndex(0);
    else if (clsIndex.equals("last"))
        data.setClassIndex(data.numAttributes() - 1);
    else
        data.setClassIndex(Integer.parseInt(clsIndex) - 1);

    // classifier
    String[] tmpOptions;
    String classname;
    tmpOptions = Utils.splitOptions(Utils.getOption("W", args));
    classname = tmpOptions[0];
    tmpOptions[0] = "";
    Classifier cls = (Classifier) Utils.forName(Classifier.class, classname, tmpOptions);

    // other options
    int runs = Integer.parseInt(Utils.getOption("r", args));
    int folds = Integer.parseInt(Utils.getOption("x", args));

    // perform cross-validation
    for (int i = 0; i < runs; i++) {
        // randomize data
        int seed = i + 1;
        Random rand = new Random(seed);
        Instances randData = new Instances(data);
        randData.randomize(rand);
        //if (randData.classAttribute().isNominal())
        //   randData.stratify(folds);

        Evaluation eval = new Evaluation(randData);

        StringBuilder optionsString = new StringBuilder();
        for (String s : cls.getOptions()) {
            optionsString.append(s);
            optionsString.append(" ");
        }

        // output evaluation
        System.out.println();
        System.out.println("=== Setup run " + (i + 1) + " ===");
        System.out.println("Classifier: " + optionsString.toString());
        System.out.println("Dataset: " + data.relationName());
        System.out.println("Folds: " + folds);
        System.out.println("Seed: " + seed);
        System.out.println();

        for (int n = 0; n < folds; n++) {
            Instances train = randData.trainCV(folds, n);
            Instances test = randData.testCV(folds, n);

            // build and evaluate classifier
            Classifier clsCopy = Classifier.makeCopy(cls);
            clsCopy.buildClassifier(train);
            eval.evaluateModel(clsCopy, test);
            System.out.println(eval.toClassDetailsString());
        }

        System.out.println(
                eval.toSummaryString("=== " + folds + "-fold Cross-validation run " + (i + 1) + " ===", false));
    }
}

From source file:homemadeWEKA.java

public static void reevaluateModel(Instances data_train, Instances data_test, Classifier cls) throws Exception {
    Evaluation eval = new Evaluation(data_train);
    eval.evaluateModel(cls, data_test);
    System.out.println(eval.toSummaryString("\nResults\n\n", false));
}

From source file:ClassificationClass.java

public Evaluation cls_svm(Instances data) {
    Evaluation eval = null;
    try {/*from w  ww. jav  a  2  s. c  o  m*/
        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;
}

From source file:ClassificationClass.java

public Evaluation cls_knn(Instances data) {
    Evaluation eval = null;
    try {//from w  ww.ja  va  2 s. c om
        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;
}

From source file:ClassificationClass.java

public Evaluation cls_naivebayes(Instances data) {
    Evaluation eval = null;
    try {/*  w w  w  .  j  av  a 2s.c om*/
        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;
}

From source file:ClassificationClass.java

public Evaluation cls_c4_5(Instances data) {
    Evaluation eval = null;
    try {//w w w .  j  a  v  a  2 s . c  o m
        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;
}

From source file:Pair.java

License:Open Source License

private double getTestError() throws Exception {

    Evaluation evaluation;
    evaluation = new Evaluation(testData);
    evaluation.evaluateModel(this, testData);
    return evaluation.errorRate();

}

From source file:Pair.java

License:Open Source License

/**
 * Main method for testing this class.//from  ww w .  j  a  va  2 s.  c  om
 *
 * @param argv the options
 */
public static void main(String[] argv) {

    try {
        System.out.println(Evaluation.evaluateModel(new TwoStageTrAdaBoostR2(), argv));
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

From source file:CopiaSeg3.java

public static Evaluation simpleClassify(Classifier model, Instances trainingSet, Instances testingSet)
        throws Exception {
    Evaluation validation = new Evaluation(trainingSet);

    model.buildClassifier(trainingSet);//from ww w. j  a v  a 2 s . c  o  m
    validation.evaluateModel(model, testingSet);

    // Imprime el resultado de Weka explorer:
    String strSummary = validation.toSummaryString();
    System.out.println(strSummary);

    return validation;
}