Example usage for weka.classifiers Evaluation Evaluation

List of usage examples for weka.classifiers Evaluation Evaluation

Introduction

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

Prototype

public Evaluation(Instances data) throws Exception 

Source Link

Usage

From source file:org.uclab.mm.kcl.ddkat.modellearner.ModelLearner.java

License:Apache License

/**
* Method to compute the classification accuracy.
*
* @param algo the algorithm name/*  w w w .  java  2  s . c om*/
* @param data the data instances
* @param datanature the dataset nature (i.e. original or processed data)
* @throws Exception the exception
*/
protected String[] modelAccuracy(String algo, Instances data, String datanature) throws Exception {

    String modelResultSet[] = new String[4];
    String modelStr = "";
    Classifier classifier = null;

    // setting class attribute if the data format does not provide this information           
    if (data.classIndex() == -1)
        data.setClassIndex(data.numAttributes() - 1);

    String decisionAttribute = data.attribute(data.numAttributes() - 1).toString();
    String res[] = decisionAttribute.split("\\s+");
    decisionAttribute = res[1];

    if (algo.equals("BFTree")) {

        // Use BFTree classifiers
        BFTree BFTreeclassifier = new BFTree();
        BFTreeclassifier.buildClassifier(data);
        modelStr = BFTreeclassifier.toString();
        classifier = BFTreeclassifier;

    } else if (algo.equals("FT")) {

        // Use FT classifiers
        FT FTclassifier = new FT();
        FTclassifier.buildClassifier(data);
        modelStr = FTclassifier.toString();
        classifier = FTclassifier;

    } else if (algo.equals("J48")) {

        // Use J48 classifiers
        J48 J48classifier = new J48();
        J48classifier.buildClassifier(data);
        modelStr = J48classifier.toString();
        classifier = J48classifier;
        System.out.println("Model String: " + modelStr);

    } else if (algo.equals("J48graft")) {

        // Use J48graft classifiers
        J48graft J48graftclassifier = new J48graft();
        J48graftclassifier.buildClassifier(data);
        modelStr = J48graftclassifier.toString();
        classifier = J48graftclassifier;

    } else if (algo.equals("RandomTree")) {

        // Use RandomTree classifiers
        RandomTree RandomTreeclassifier = new RandomTree();
        RandomTreeclassifier.buildClassifier(data);
        modelStr = RandomTreeclassifier.toString();
        classifier = RandomTreeclassifier;

    } else if (algo.equals("REPTree")) {

        // Use REPTree classifiers
        REPTree REPTreeclassifier = new REPTree();
        REPTreeclassifier.buildClassifier(data);
        modelStr = REPTreeclassifier.toString();
        classifier = REPTreeclassifier;

    } else if (algo.equals("SimpleCart")) {

        // Use SimpleCart classifiers
        SimpleCart SimpleCartclassifier = new SimpleCart();
        SimpleCartclassifier.buildClassifier(data);
        modelStr = SimpleCartclassifier.toString();
        classifier = SimpleCartclassifier;

    }

    modelResultSet[0] = algo;
    modelResultSet[1] = decisionAttribute;
    modelResultSet[2] = modelStr;

    // Collect every group of predictions for J48 model in a FastVector
    FastVector predictions = new FastVector();

    Evaluation evaluation = new Evaluation(data);
    int folds = 10; // cross fold validation = 10
    evaluation.crossValidateModel(classifier, data, folds, new Random(1));
    // System.out.println("Evaluatuion"+evaluation.toSummaryString());
    System.out.println("\n\n" + datanature + " Evaluatuion " + evaluation.toMatrixString());

    // ArrayList<Prediction> predictions = evaluation.predictions();
    predictions.appendElements(evaluation.predictions());

    System.out.println("\n\n 11111");
    // Calculate overall accuracy of current classifier on all splits
    double correct = 0;

    for (int i = 0; i < predictions.size(); i++) {
        NominalPrediction np = (NominalPrediction) predictions.elementAt(i);
        if (np.predicted() == np.actual()) {
            correct++;
        }
    }

    System.out.println("\n\n 22222");
    double accuracy = 100 * correct / predictions.size();
    String accString = String.format("%.2f%%", accuracy);
    modelResultSet[3] = accString;
    System.out.println(datanature + " Accuracy " + accString);

    String modelFileName = algo + "-DDKA.model";

    System.out.println("\n\n 33333");

    ObjectOutputStream oos = new ObjectOutputStream(
            new FileOutputStream("D:\\DDKAResources\\" + modelFileName));
    oos.writeObject(classifier);
    oos.flush();
    oos.close();

    return modelResultSet;

}

From source file:org.wkwk.classifier.Access.java

/**
 * @param args the command line arguments
 * args[0] = filename train set/*from  w ww.  ja  va  2  s  .c o  m*/
 * args[1] = filename test set
 * args[2] = remove attribute
 * args[3] = bias resample
 * @throws java.lang.Exception
 */
public static void main(String[] args) throws Exception {

    // Read Dataset (arff, csv)
    DataSource source = new DataSource("../data/weather.nominal.arff");
    //DataSource testSource = new DataSource(args[1]);
    Instances data = source.getDataSet();

    if (data.classIndex() == -1) {
        data.setClassIndex(data.numAttributes() - 1);
    }

    // Remove attr
    //        String[] rangeOps = new String[2];
    //        rangeOps[0] = "-R";                                    // "range"
    //        rangeOps[1] = args[2];                                 // first attribute
    //        Remove remove = new Remove();                         // new instance of filter
    //        remove.setOptions(rangeOps);                           // set options
    //        remove.setInputFormat(data);                          // inform filter about dataset **AFTER** setting options
    //        Instances newData = Filter.useFilter(data, remove);   // apply filter
    //        
    //        // Filter Resample
    //        String[] biasOps = new String[2];
    //        biasOps[0] = "-B";                                    // "range"
    //        biasOps[1] = args[3];                                 // first attribute
    //        Resample resample = new Resample();
    //        resample.setOptions(biasOps);
    //        resample.setInputFormat(data);
    //        newData = Filter.useFilter(data, resample);
    //        
    // Build Classifier
    MyC45 tree = new MyC45(); // new instance of tree
    tree.buildClassifier(data); // build classifier

    // Evaluation with test set
    //Instances testSet = testSource.getDataSet();
    // train classifier
    //Classifier cls = new MyId3();
    //cls.buildClassifier(data);
    // evaluate classifier and print some statistics
    //Evaluation eval = new Evaluation(data);
    //eval.evaluateModel(cls, testSet);
    //System.out.println(eval.toSummaryString("\nResults\n======\n", false));

    // Evaluation with 10 Fold-CV
    Evaluation evalCV = new Evaluation(data);
    evalCV.crossValidateModel(tree, data, 10, new Random(1));
    System.out.println(evalCV.toSummaryString("\nResults\n======\n", false));
}

From source file:PEBL.TwoStep.java

public static void main(String[] args) throws Exception {

    ConverterUtils.DataSource source = new ConverterUtils.DataSource(
            "Z:\\\\shared from vm\\\\fourthset\\\\mixed.csv");

    Instances data = source.getDataSet();

    // setting class attribute if the data format does not provide this information
    // For example, the XRFF format saves the class attribute information as well
    if (data.classIndex() == -1) {
        data.setClassIndex(data.numAttributes() - 1);
    }/*from w  w  w.  j a  v  a2  s  . c om*/

    NumericToNominal nmf = new NumericToNominal();
    nmf.setInputFormat(data);
    data = Filter.useFilter(data, nmf);

    // build a c4.5 classifier
    String[] options = new String[1];
    // options[0] = "-C 0.25 -M 2";            // unpruned tree
    options[0] = "-K";
    NaiveBayes c = new NaiveBayes(); // new instance of tree
    c.setOptions(options); // set the options
    c.buildClassifier(data); // build classifier

    // eval
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(c, data, 10, new Random(1));
    System.out.println(eval.toSummaryString());
    System.out.println(eval.toMatrixString());
    System.out.println(eval.toClassDetailsString());
    System.out.println("--- model learned on mixed set ---");

    // load unlabeled data
    ConverterUtils.DataSource s = new ConverterUtils.DataSource(
            "Z:\\\\shared from vm\\\\fourthset\\\\unlabelled.csv");
    Instances unlabeled = s.getDataSet();
    // set class attribute
    unlabeled.setClassIndex(unlabeled.numAttributes() - 1);

    nmf = new NumericToNominal();
    nmf.setInputFormat(unlabeled);
    unlabeled = Filter.useFilter(unlabeled, nmf);

    // label instances
    for (int i = 0; i < unlabeled.numInstances(); i++) {
        double classZero = c.distributionForInstance(unlabeled.instance(i))[0];
        double classOne = c.distributionForInstance(unlabeled.instance(i))[1];
        System.out.print(
                "classifying: " + unlabeled.instance(i) + " : " + classZero + " - " + classOne + " == class: ");
        if (classZero > classOne) {
            System.out.print("0");
            unlabeled.instance(i).setClassValue("0");
        } else {
            System.out.print("1");
            unlabeled.instance(i).setClassValue("1");
        }
        System.out.println("");
    }

    // save labeled data
    // BufferedWriter writer = new BufferedWriter(
    //         new FileWriter("Z:\\\\shared from vm\\\\thirdset\\\\relabelled.arff"));
    // writer.write(labeled.toString());
    // writer.newLine();
    // writer.flush();
    // writer.close();
    ArffSaver saver = new ArffSaver();
    saver.setInstances(unlabeled);
    saver.setFile(new File("Z:\\shared from vm\\thirdset\\relabelled.arff"));
    //        saver.setDestination(new File("Z:\\shared from vm\\thirdset\\relabelled.arff"));   // **not** necessary in 3.5.4 and later
    saver.writeBatch();

}

From source file:personality_prediction.Evaluation_Result.java

void eval_result() {
    try {//from  w  ww .j  av  a  2s  .com
        DataSource source_train = new DataSource(
                "C:\\Users\\divya\\Desktop\\Personality Mining\\WEKA_DataSet\\Training dataset\\training_data_neur.csv");
        Instances train = source_train.getDataSet();
        DataSource source_test = new DataSource(
                "C:\\Users\\divya\\Desktop\\Personality Mining\\WEKA_DataSet\\Testing dataset\\Testing_data_neur.csv");
        Instances test = source_test.getDataSet();
        train.setClassIndex(train.numAttributes() - 1);
        test.setClassIndex(train.numAttributes() - 1);
        // train classifier
        Classifier cls = new J48();
        cls.buildClassifier(train);
        Evaluation eval = new Evaluation(train);
        eval.evaluateModel(cls, test);
        System.out.println(eval.toSummaryString("\nResults\n======\n", false));

    } catch (Exception e) {
        System.out.println(e.getLocalizedMessage());
    }
}

From source file:PointAnalyser.Main.java

public static void trainC45Classifier() throws Exception {

    // setting class attribute if the data format does not provide this information
    // For example, the XRFF format saves the class attribute information as well
    if (data.classIndex() == -1) {
        data.setClassIndex(data.numAttributes() - 1);
    }//w w  w .jav  a 2 s .c om

    NumericToNominal nmf = new NumericToNominal();
    nmf.setInputFormat(data);
    data = Filter.useFilter(data, nmf);

    // build a c4.5 classifier
    String[] options = new String[1];
    options[0] = "-C 0.25 -M 2 -U"; // unpruned tree
    tree = new J48(); // new instance of tree
    tree.setOptions(options); // set the options
    tree.buildClassifier(data); // build classifier
    /*
             RemoveMisclassified rm = new RemoveMisclassified();
             rm.setInputFormat(data);
             rm.setClassifier(tree);
             rm.setNumFolds(10);
             rm.setThreshold(0.1);
             rm.setMaxIterations(0);
             data = Filter.useFilter(data, rm);
            
             tree = new J48();         // new instance of tree
             tree.setOptions(options);     // set the options
             tree.buildClassifier(data);   // build classifier
             */
    // eval
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(tree, data, 10, new Random(1));

    System.out.println(eval.toSummaryString());
    System.out.println(eval.toMatrixString());
    System.out.println(eval.toClassDetailsString());

}

From source file:PointAnalyser.Main.java

public static void trainNNClassifier() throws Exception {

    // setting class attribute if the data format does not provide this information
    // For example, the XRFF format saves the class attribute information as well
    if (data.classIndex() == -1) {
        data.setClassIndex(data.numAttributes() - 1);
    }/*from ww  w.  j a v a  2  s  .co m*/

    NumericToNominal nmf = new NumericToNominal();
    nmf.setInputFormat(data);
    data = Filter.useFilter(data, nmf);

    // build a c4.5 classifier
    String[] options = new String[1];
    // options[0] = "-K 1";            // unpruned tree
    nn = new IBk(); // new instance of tree
    //  nn.setCrossValidate(true);
    nn.setKNN(7);
    nn.setNearestNeighbourSearchAlgorithm(new weka.core.neighboursearch.KDTree(data));

    nn.setWindowSize(0);

    // nn.setOptions(options);     // set the options
    nn.buildClassifier(data); // build classifier
    // eval
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(nn, data, 10, new Random(1));

    System.out.println(eval.toSummaryString());
    System.out.println(eval.toMatrixString());
    System.out.println(eval.toClassDetailsString());

}

From source file:predictforex.ANN.java

static void TenFoldTrain_aNN() throws Exception {
    eval = new Evaluation(dataset);
    MultilayerPerceptron tree = new MultilayerPerceptron();
    eval.crossValidateModel(tree, dataset, 10, new Random(1));
    System.out.println(eval.toSummaryString("Results ANN tenfold \n", false));
    //        System.out.println(eval.toClassDetailsString());
    //        System.out.println(eval.fMeasure(1) + " "+eval.precision(1)+" "+eval.recall(1));
    //        System.out.println(eval.toMatrixString());
}

From source file:predictforex.ANN.java

static void FullTraining_aNN() throws Exception {
    Classifier cls = new MultilayerPerceptron();
    cls.buildClassifier(dataset);//from ww w.  j a  v  a 2  s  . com
    eval = new Evaluation(dataset);
    eval.evaluateModel(cls, dataset);
    System.out.println(eval.toSummaryString("Results ANN full training \n", false));
    System.out.println(eval.toClassDetailsString());
    System.out.println(eval.fMeasure(1) + " " + eval.precision(1) + " " + eval.recall(1));
    System.out.println(eval.toMatrixString());
}

From source file:predictor.Predictor.java

public static void multilayerPerceptron() throws Exception {

    DataSource train = new DataSource(configuration.getWorkspace() + "train_common.arff");
    DataSource test = new DataSource(configuration.getWorkspace() + "test_common.arff");

    Instances trainInstances = train.getDataSet();
    Instances testInstances = test.getDataSet();

    //last attribute classify
    trainInstances.setClassIndex(trainInstances.numAttributes() - 1);
    testInstances.setClassIndex(testInstances.numAttributes() - 1);
    //        /*from   ww  w .  j  a  v a  2s.  co  m*/
    //        Classifier cModel = (Classifier)new MultilayerPerceptron();  
    //        cModel.buildClassifier(trainInstances);  
    //
    //        weka.core.SerializationHelper.write("/some/where/nBayes.model", cModel);
    //
    //        Classifier cls = (Classifier) weka.core.SerializationHelper.read("/some/where/nBayes.model");
    //
    //        // Test the model
    //        Evaluation eTest = new Evaluation(trainInstances);
    //        eTest.evaluateModel(cls, testInstances);

    MultilayerPerceptron mlp = new MultilayerPerceptron();
    mlp.buildClassifier(trainInstances);
    mlp.setHiddenLayers(configuration.getHiddenLayers());
    mlp.setLearningRate(configuration.getLearningRate());
    mlp.setTrainingTime(configuration.getEpocs());
    mlp.setMomentum(configuration.getMomentum());

    // train classifier
    Classifier cls = new MultilayerPerceptron();
    cls.buildClassifier(trainInstances);

    // evaluate classifier and print some statistics
    Evaluation eval = new Evaluation(trainInstances);
    eval.evaluateModel(cls, testInstances);

    System.out.println(eval.toSummaryString());
}

From source file:qa.experiment.ProcessFeatureVector.java

public void evaluate(Instances trainingData) throws Exception {
    Classifier c1 = new SMO();
    Evaluation eval = new Evaluation(trainingData);
    eval.crossValidateModel(c1, trainingData, 10, new Random(1));
    System.out.println("Estimated Accuracy: " + Double.toString(eval.pctCorrect()));
}