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:core.classification.Classifiers.java

License:Open Source License

public void trainSC() throws Exception {
    String sql;/*from  w w w . ja  v  a 2  s  .c  o  m*/

    // ---
    // Connect to the database
    // ---
    InstanceQuery query = new InstanceQuery();
    query.setDatabaseURL(dbase);
    query.setUsername("");
    query.setPassword("");

    // ---
    // ---
    // SCA
    // ---
    // ---

    sql = "SELECT ";
    sql += "CR.ratio, CR.class ";
    sql += "FROM Class_ratio AS CR;";

    query.setQuery(sql);
    Instances data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    String[] options = Utils.splitOptions(
            "-D -Q weka.classifiers.bayes.net.search.local.K2 -- -P 1 -S BAYES -E weka.classifiers.bayes.net.estimate.SimpleEstimator -- -A 0.5");
    SCA.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train the classifier
    // ---
    System.out.println("Building SCA ...");
    SCA.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Classifier evaluation
    // ---
    System.out.println("Cross-validation for SCA...");
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(SCA, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for SCA: \n\n", false));

    // ---
    // ---
    // SCB
    // ---
    // ---

    sql = "SELECT ";
    sql += "Data.H2, Data.D2, Data.DX, ";
    sql += "Data.PARENT_CHAR AS PCLASS, ";
    sql += "Data.CLASS ";
    sql += "FROM Data ";
    sql += "WHERE (((Data.SEGERR)=0) AND (Data.PARENT_CHAR<>'0') );";

    query.setQuery(sql);
    data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    options = Utils.splitOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a");
    SCB.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train the classifier
    // ---
    System.out.println("Building SCB ...");
    SCB.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Classifier evaluation
    // ---
    System.out.println("Cross-validation for SCB...");
    eval = new Evaluation(data);
    eval.crossValidateModel(SCB, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for SCB: \n\n", false));

    // ---
    // ---
    // SCC
    // ---
    // ---

    // ----
    // SCC1
    // ----

    sql = "SELECT ";
    sql += "Data.LH, Data.LD, Data.LDX, Data.LCLASS, ";
    sql += "Data.CLASS ";
    sql += "FROM Data ";
    sql += "WHERE ( (Data.SEGERR)=0  AND ( (Data.LCLASS)<>'0' ) );";

    query.setQuery(sql);
    data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    options = Utils.splitOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a");
    SCC1.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train the classifier
    // ---
    System.out.println("Building SCC1 ...");
    SCC1.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Classifier evaluation
    // ---
    System.out.println("Cross-validation for SCC1...");
    eval = new Evaluation(data);
    eval.crossValidateModel(SCC1, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for SCC1: \n\n", false));

    // ----
    // SCC2
    // ----

    sql = "SELECT ";
    sql += "Data.EH, Data.ED, Data.EDX, Data.ECLASS, ";
    sql += "Data.CLASS ";
    sql += "FROM Data ";
    sql += "WHERE ( (Data.SEGERR)=0  AND ( (Data.ECLASS)<>'0' ) );";

    query.setQuery(sql);
    data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    //      options = Utils.splitOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a");
    SCC2.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train the classifier
    // ---
    System.out.println("Building SCC2 ...");
    SCC2.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Classifier evaluation
    // ---
    System.out.println("Cross-validation for SCC2...");
    eval = new Evaluation(data);
    eval.crossValidateModel(SCC2, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for SCC2: \n\n", false));

    // ----
    // SCC3
    // ----

    sql = "SELECT ";
    sql += "Data.SH, Data.SD, Data.SDX, Data.SCLASS, ";
    sql += "Data.CLASS ";
    sql += "FROM Data ";
    sql += "WHERE ( (Data.SEGERR)=0  AND ( (Data.SCLASS)<>'0' ) );";

    query.setQuery(sql);
    data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    //      options = Utils.splitOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a");
    SCC3.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train the classifier
    // ---
    System.out.println("Building SCC3 ...");
    SCC3.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Classifier evaluation
    // ---
    System.out.println("Cross-validation for SCC3...");
    eval = new Evaluation(data);
    eval.crossValidateModel(SCC3, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for SCC3: \n\n", false));

}

From source file:core.classification.Classifiers.java

License:Open Source License

public void trainRC() throws Exception {
    // ---/*from  www. j  ava  2 s.co m*/
    // Retrieve the instances in the database
    // ---
    InstanceQuery query = new InstanceQuery();
    query.setDatabaseURL(dbase);
    query.setUsername("");
    query.setPassword("");

    String sql = "SELECT ";
    sql += "Data.H2, Data.D2, Data.DX, ";
    sql += "Data.CLASS, Data.PARENT_CHAR AS PCLASS, ";
    sql += "Data.RELID ";
    sql += "FROM Data ";
    sql += "WHERE (((Data.SEGERR)=0) AND (Data.PARENT_CHAR<>'0') );";

    query.setQuery(sql);
    Instances data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    //      String[] options = Utils.splitOptions("-L 0.2 -M 0.2 -N 50 -V 0 -S 0 -E 20 -H 5 ");
    String[] options = Utils.splitOptions(
            "-cost-matrix \"[0.0 1.0 1.0 0.1 0.1; 1.0 0.0 1.0 0.1 0.1; 1.0 1.0 0.0 0.1 0.1; 10.0 10.0 10.0 0.0 1.0; 10.0 10.0 10.0 1.0 0.0]\" -S 1 -W weka.classifiers.functions.MultilayerPerceptron -- -L 0.2 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a");
    RC.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train
    // ---
    System.out.println("Building RC...");
    RC.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Evaluation
    // ---
    System.out.println("Cross-validation for RC...");
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(RC, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for RC: \n\n", false));

}

From source file:core.classification.Classifiers.java

License:Open Source License

public void trainYNC() throws Exception {
    // ---/*from  www  .j  a  va  2 s .  c  om*/
    // Retrieve the instances in the database
    // ---
    InstanceQuery query = new InstanceQuery();
    query.setDatabaseURL(dbase);
    query.setUsername("");
    query.setPassword("");

    String sql = "SELECT ";
    sql += "YNCdata.PCLASS, YNCdata.CCLASS, YNCdata.RAREA, YNCdata.H, YNCdata.D, YNCdata.V, ";
    sql += "YNCdata.YN ";
    sql += "FROM YNCdata ";

    query.setQuery(sql);
    Instances data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    String[] options = Utils.splitOptions("-R -N 3 -Q 1 -M 30");
    YNC.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train
    // ---
    System.out.println("Building YC...");
    YNC.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Evaluation
    // ---
    System.out.println("Cross-validation for YNC...");
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(YNC, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for YNC: \n\n", false));

}

From source file:cs.man.ac.uk.classifiers.GetAUC.java

License:Open Source License

/**
 * Computes the AUC for the supplied learner.
 * @return the AUC as a double value.//from   ww  w.ja v a 2 s . co  m
 */
@SuppressWarnings("unused")
private static double validate5x2CV() {
    try {
        // other options
        int runs = 5;
        int folds = 2;
        double AUC_SUM = 0;

        // 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()) {
                System.out.println("Stratifying...");
                randData.stratify(folds);
            }

            Evaluation eval = new Evaluation(randData);

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

                // the above code is used by the StratifiedRemoveFolds filter, the
                // code below by the Explorer/Experimenter:
                // Instances train = randData.trainCV(folds, n, rand);

                // build and evaluate classifier
                String[] options = { "-U", "-A" };
                J48 classifier = new J48();
                //HTree classifier = new HTree();

                classifier.setOptions(options);
                classifier.buildClassifier(train);
                eval.evaluateModel(classifier, test);

                // generate curve
                ThresholdCurve tc = new ThresholdCurve();
                int classIndex = 0;
                Instances result = tc.getCurve(eval.predictions(), classIndex);

                // plot curve
                vmc = new ThresholdVisualizePanel();
                AUC_SUM += ThresholdCurve.getROCArea(result);
                System.out.println("AUC: " + ThresholdCurve.getROCArea(result) + " \tAUC SUM: " + AUC_SUM);
            }
        }

        return AUC_SUM / ((double) runs * (double) folds);
    } catch (Exception e) {
        System.out.println("Exception validating data!");
        return 0;
    }
}

From source file:cs.man.ac.uk.classifiers.GetAUC.java

License:Open Source License

/**
 * Computes the AUC for the supplied learner.
 * @param learner the learning algorithm to use.
 * @return the AUC as a double value./*from   w w  w  .  ja  va  2s  .  com*/
 */
@SuppressWarnings("unused")
private static double validate(Classifier learner) {
    try {

        Evaluation eval = new Evaluation(data);
        eval.crossValidateModel(learner, data, 2, new Random(1));

        // generate curve
        ThresholdCurve tc = new ThresholdCurve();
        int classIndex = 0;
        Instances result = tc.getCurve(eval.predictions(), classIndex);

        // plot curve
        vmc = new ThresholdVisualizePanel();
        double AUC = ThresholdCurve.getROCArea(result);
        vmc.setROCString(
                "(Area under ROC = " + Utils.doubleToString(ThresholdCurve.getROCArea(result), 9) + ")");
        vmc.setName(result.relationName());

        PlotData2D tempd = new PlotData2D(result);
        tempd.setPlotName(result.relationName());
        tempd.addInstanceNumberAttribute();

        // specify which points are connected
        boolean[] cp = new boolean[result.numInstances()];
        for (int n = 1; n < cp.length; n++)
            cp[n] = true;

        tempd.setConnectPoints(cp);
        // add plot
        vmc.addPlot(tempd);

        return AUC;
    } catch (Exception e) {
        System.out.println("Exception validating data!");
        return 0;
    }
}

From source file:csav2.Weka_additive.java

public void createTrainingFeatureFile1(String input) throws Exception {
    String file = "Classifier\\featurefile_additive_trial1.arff";
    ArffLoader loader = new ArffLoader();

    //ATTRIBUTES/*from   w  ww  .j  a va  2 s  .  co m*/
    Attribute attr[] = new Attribute[50];

    //numeric
    attr[0] = new Attribute("Autosentiment");

    //class
    FastVector classValue = new FastVector(3);
    classValue.addElement("p");
    classValue.addElement("n");
    classValue.addElement("o");
    attr[1] = new Attribute("answer", classValue);

    FastVector attrs = new FastVector();
    attrs.addElement(attr[0]);
    attrs.addElement(attr[1]);

    // Add Instances
    Instances dataset = new Instances("my_dataset", attrs, 0);

    if (new File(file).isFile()) {
        loader.setFile(new File(file));
        dataset = loader.getDataSet();
    }

    System.out.println("-----------------------------------------");
    System.out.println(input);
    System.out.println("-----------------------------------------");

    StringTokenizer tokenizer = new StringTokenizer(input);

    while (tokenizer.hasMoreTokens()) {
        Instance example = new Instance(2);
        for (int j = 0; j < 2; j++) {
            String st = tokenizer.nextToken();
            System.out.println(j + " " + st);
            if (j == 0)
                example.setValue(attr[j], Float.parseFloat(st));
            else if (j == 1)
                example.setValue(attr[j], st);
            else
                example.setValue(attr[j], Integer.parseInt(st));
        }
        dataset.add(example);
    }

    //Save dataset
    ArffSaver saver = new ArffSaver();
    saver.setInstances(dataset);
    saver.setFile(new File(file));
    saver.writeBatch();

    //Read dataset
    loader.setFile(new File(file));
    dataset = loader.getDataSet();

    //Build classifier
    dataset.setClassIndex(1);
    Classifier classifier = new J48();
    classifier.buildClassifier(dataset);

    //Save classifier
    String file1 = "Classifier\\classifier_add_autosentiment.model";
    OutputStream os = new FileOutputStream(file1);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(os);
    objectOutputStream.writeObject(classifier);

    // Comment out if not needed
    //Read classifier back
    InputStream is = new FileInputStream(file1);
    ObjectInputStream objectInputStream = new ObjectInputStream(is);
    classifier = (Classifier) objectInputStream.readObject();
    objectInputStream.close();

    //Evaluate resample if needed
    //dataset = dataset.resample(new Random(42));
    //split to 70:30 learn and test set
    double percent = 70.0;
    int trainSize = (int) Math.round(dataset.numInstances() * percent / 100);
    int testSize = dataset.numInstances() - trainSize;
    Instances train = new Instances(dataset, 0, trainSize);
    Instances test = new Instances(dataset, trainSize, testSize);
    train.setClassIndex(1);
    test.setClassIndex(1);

    //Evaluate
    Evaluation eval = new Evaluation(dataset); //trainset
    eval.crossValidateModel(classifier, dataset, 10, new Random(1));
    System.out.println("EVALUATION:\n" + eval.toSummaryString());
    System.out.println("WEIGHTED MEASURE:\n" + eval.weightedFMeasure());
    System.out.println("WEIGHTED PRECISION:\n" + eval.weightedPrecision());
    System.out.println("WEIGHTED RECALL:\n" + eval.weightedRecall());
}

From source file:csav2.Weka_additive.java

public void createTrainingFeatureFile2(String input) throws Exception {
    String file = "Classifier\\featurefile_additive_trial2.arff";
    ArffLoader loader = new ArffLoader();

    //ATTRIBUTES//from   w w  w .  j  a  va2 s .c o m
    Attribute attr[] = new Attribute[50];

    //numeric
    attr[0] = new Attribute("Autosentiment");
    attr[1] = new Attribute("PositiveMatch");
    attr[2] = new Attribute("NegativeMatch");

    //class
    FastVector classValue = new FastVector(3);
    classValue.addElement("p");
    classValue.addElement("n");
    classValue.addElement("o");
    attr[3] = new Attribute("answer", classValue);

    FastVector attrs = new FastVector();
    attrs.addElement(attr[0]);
    attrs.addElement(attr[1]);
    attrs.addElement(attr[2]);
    attrs.addElement(attr[3]);

    // Add Instances
    Instances dataset = new Instances("my_dataset", attrs, 0);

    if (new File(file).isFile()) {
        loader.setFile(new File(file));
        dataset = loader.getDataSet();
    }

    System.out.println("-----------------------------------------");
    System.out.println(input);
    System.out.println("-----------------------------------------");

    StringTokenizer tokenizer = new StringTokenizer(input);

    while (tokenizer.hasMoreTokens()) {
        Instance example = new Instance(4);
        for (int j = 0; j < 4; j++) {
            String st = tokenizer.nextToken();
            System.out.println(j + " " + st);
            if (j == 0)
                example.setValue(attr[j], Float.parseFloat(st));
            else if (j == 3)
                example.setValue(attr[j], st);
            else
                example.setValue(attr[j], Integer.parseInt(st));
        }
        dataset.add(example);
    }

    //Save dataset
    ArffSaver saver = new ArffSaver();
    saver.setInstances(dataset);
    saver.setFile(new File(file));
    saver.writeBatch();

    //Read dataset
    loader.setFile(new File(file));
    dataset = loader.getDataSet();

    //Build classifier
    dataset.setClassIndex(3);
    Classifier classifier = new J48();
    classifier.buildClassifier(dataset);

    //Save classifier
    String file1 = "Classifier\\classifier_add_asAndpolarwords.model";
    OutputStream os = new FileOutputStream(file1);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(os);
    objectOutputStream.writeObject(classifier);

    // Comment out if not needed
    //Read classifier back
    InputStream is = new FileInputStream(file1);
    ObjectInputStream objectInputStream = new ObjectInputStream(is);
    classifier = (Classifier) objectInputStream.readObject();
    objectInputStream.close();

    //Evaluate resample if needed
    //dataset = dataset.resample(new Random(42));
    //split to 70:30 learn and test set
    double percent = 70.0;
    int trainSize = (int) Math.round(dataset.numInstances() * percent / 100);
    int testSize = dataset.numInstances() - trainSize;
    Instances train = new Instances(dataset, 0, trainSize);
    Instances test = new Instances(dataset, trainSize, testSize);
    train.setClassIndex(3);
    test.setClassIndex(3);

    //Evaluate
    Evaluation eval = new Evaluation(dataset); //trainset
    eval.crossValidateModel(classifier, dataset, 10, new Random(1));
    System.out.println("EVALUATION:\n" + eval.toSummaryString());
    System.out.println("WEIGHTED MEASURE:\n" + eval.weightedFMeasure());
    System.out.println("WEIGHTED PRECISION:\n" + eval.weightedPrecision());
    System.out.println("WEIGHTED RECALL:\n" + eval.weightedRecall());
}

From source file:csav2.Weka_additive.java

public void createTrainingFeatureFile3(String input) throws Exception {
    String file = "Classifier\\featurefile_additive_trial3.arff";
    ArffLoader loader = new ArffLoader();

    //ATTRIBUTES/*from ww w . jav  a2  s .co m*/
    Attribute attr[] = new Attribute[50];

    //numeric
    attr[0] = new Attribute("Autosentiment");
    attr[1] = new Attribute("PositiveMatch");
    attr[2] = new Attribute("NegativeMatch");
    attr[3] = new Attribute("FW");
    attr[4] = new Attribute("JJ");
    attr[5] = new Attribute("RB");
    attr[6] = new Attribute("RB_JJ");

    //class
    FastVector classValue = new FastVector(3);
    classValue.addElement("p");
    classValue.addElement("n");
    classValue.addElement("o");
    attr[7] = new Attribute("answer", classValue);

    FastVector attrs = new FastVector();
    attrs.addElement(attr[0]);
    attrs.addElement(attr[1]);
    attrs.addElement(attr[2]);
    attrs.addElement(attr[3]);
    attrs.addElement(attr[4]);
    attrs.addElement(attr[5]);
    attrs.addElement(attr[6]);
    attrs.addElement(attr[7]);

    // Add Instances
    Instances dataset = new Instances("my_dataset", attrs, 0);

    if (new File(file).isFile()) {
        loader.setFile(new File(file));
        dataset = loader.getDataSet();
    }

    System.out.println("-----------------------------------------");
    System.out.println(input);
    System.out.println("-----------------------------------------");

    StringTokenizer tokenizer = new StringTokenizer(input);

    while (tokenizer.hasMoreTokens()) {
        Instance example = new Instance(8);
        for (int j = 0; j < 8; j++) {
            String st = tokenizer.nextToken();
            System.out.println(j + " " + st);
            if (j == 0)
                example.setValue(attr[j], Float.parseFloat(st));
            else if (j == 7)
                example.setValue(attr[j], st);
            else
                example.setValue(attr[j], Integer.parseInt(st));
        }
        dataset.add(example);
    }

    //Save dataset
    ArffSaver saver = new ArffSaver();
    saver.setInstances(dataset);
    saver.setFile(new File(file));
    saver.writeBatch();

    //Read dataset
    loader.setFile(new File(file));
    dataset = loader.getDataSet();

    //Build classifier
    dataset.setClassIndex(7);
    Classifier classifier = new J48();
    classifier.buildClassifier(dataset);

    //Save classifier
    String file1 = "Classifier\\classifier_add_asAndpolarwordsAndpos.model";
    OutputStream os = new FileOutputStream(file1);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(os);
    objectOutputStream.writeObject(classifier);

    // Comment out if not needed
    //Read classifier back
    InputStream is = new FileInputStream(file1);
    ObjectInputStream objectInputStream = new ObjectInputStream(is);
    classifier = (Classifier) objectInputStream.readObject();
    objectInputStream.close();

    //Evaluate resample if needed
    //dataset = dataset.resample(new Random(42));
    //split to 70:30 learn and test set
    double percent = 70.0;
    int trainSize = (int) Math.round(dataset.numInstances() * percent / 100);
    int testSize = dataset.numInstances() - trainSize;
    Instances train = new Instances(dataset, 0, trainSize);
    Instances test = new Instances(dataset, trainSize, testSize);
    train.setClassIndex(7);
    test.setClassIndex(7);

    //Evaluate
    Evaluation eval = new Evaluation(dataset); //trainset
    eval.crossValidateModel(classifier, dataset, 10, new Random(1));
    System.out.println("EVALUATION:\n" + eval.toSummaryString());
    System.out.println("WEIGHTED MEASURE:\n" + eval.weightedFMeasure());
    System.out.println("WEIGHTED PRECISION:\n" + eval.weightedPrecision());
    System.out.println("WEIGHTED RECALL:\n" + eval.weightedRecall());
}

From source file:csav2.Weka_additive.java

public void createTrainingFeatureFile4(String input) throws Exception {
    String file = "Classifier\\featurefile_additive_trial4.arff";
    ArffLoader loader = new ArffLoader();

    //ATTRIBUTES/*from w  w  w  .  j a  va 2s  . com*/
    Attribute attr[] = new Attribute[50];

    //numeric
    attr[0] = new Attribute("Autosentiment");
    attr[1] = new Attribute("PositiveMatch");
    attr[2] = new Attribute("NegativeMatch");
    attr[3] = new Attribute("FW");
    attr[4] = new Attribute("JJ");
    attr[5] = new Attribute("RB");
    attr[6] = new Attribute("RB_JJ");
    attr[7] = new Attribute("amod");
    attr[8] = new Attribute("acomp");
    attr[9] = new Attribute("advmod");

    //class
    FastVector classValue = new FastVector(3);
    classValue.addElement("p");
    classValue.addElement("n");
    classValue.addElement("o");
    attr[10] = new Attribute("answer", classValue);

    FastVector attrs = new FastVector();
    attrs.addElement(attr[0]);
    attrs.addElement(attr[1]);
    attrs.addElement(attr[2]);
    attrs.addElement(attr[3]);
    attrs.addElement(attr[4]);
    attrs.addElement(attr[5]);
    attrs.addElement(attr[6]);
    attrs.addElement(attr[7]);
    attrs.addElement(attr[8]);
    attrs.addElement(attr[9]);
    attrs.addElement(attr[10]);

    // Add Instances
    Instances dataset = new Instances("my_dataset", attrs, 0);

    if (new File(file).isFile()) {
        loader.setFile(new File(file));
        dataset = loader.getDataSet();
    }

    System.out.println("-----------------------------------------");
    System.out.println(input);
    System.out.println("-----------------------------------------");

    StringTokenizer tokenizer = new StringTokenizer(input);

    while (tokenizer.hasMoreTokens()) {
        Instance example = new Instance(11);
        for (int j = 0; j < 11; j++) {
            String st = tokenizer.nextToken();
            System.out.println(j + " " + st);
            if (j == 0)
                example.setValue(attr[j], Float.parseFloat(st));
            else if (j == 10)
                example.setValue(attr[j], st);
            else
                example.setValue(attr[j], Integer.parseInt(st));
        }
        dataset.add(example);
    }

    //Save dataset
    ArffSaver saver = new ArffSaver();
    saver.setInstances(dataset);
    saver.setFile(new File(file));
    saver.writeBatch();

    //Read dataset
    loader.setFile(new File(file));
    dataset = loader.getDataSet();

    //Build classifier
    dataset.setClassIndex(10);
    Classifier classifier = new J48();
    classifier.buildClassifier(dataset);

    //Save classifier
    String file1 = "Classifier\\classifier_asAndpolarwordsAndposAnddep.model";
    OutputStream os = new FileOutputStream(file1);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(os);
    objectOutputStream.writeObject(classifier);

    // Comment out if not needed
    //Read classifier back
    InputStream is = new FileInputStream(file1);
    ObjectInputStream objectInputStream = new ObjectInputStream(is);
    classifier = (Classifier) objectInputStream.readObject();
    objectInputStream.close();

    //Evaluate resample if needed
    //dataset = dataset.resample(new Random(42));
    //split to 70:30 learn and test set
    double percent = 70.0;
    int trainSize = (int) Math.round(dataset.numInstances() * percent / 100);
    int testSize = dataset.numInstances() - trainSize;
    Instances train = new Instances(dataset, 0, trainSize);
    Instances test = new Instances(dataset, trainSize, testSize);
    train.setClassIndex(10);
    test.setClassIndex(10);

    //Evaluate
    Evaluation eval = new Evaluation(dataset); //trainset
    eval.crossValidateModel(classifier, dataset, 10, new Random(1));
    System.out.println("EVALUATION:\n" + eval.toSummaryString());
    System.out.println("WEIGHTED MEASURE:\n" + eval.weightedFMeasure());
    System.out.println("WEIGHTED PRECISION:\n" + eval.weightedPrecision());
    System.out.println("WEIGHTED RECALL:\n" + eval.weightedRecall());
}

From source file:csav2.Weka_additive.java

public void createTrainingFeatureFile5(String input) throws Exception {
    String file = "Classifier\\featurefile_additive_trial5.arff";
    ArffLoader loader = new ArffLoader();

    //ATTRIBUTES//from www  .  jav  a 2  s.  c om
    Attribute attr[] = new Attribute[50];

    //numeric
    attr[0] = new Attribute("Autosentiment");
    attr[1] = new Attribute("PositiveMatch");
    attr[2] = new Attribute("NegativeMatch");
    attr[3] = new Attribute("FW");
    attr[4] = new Attribute("JJ");
    attr[5] = new Attribute("RB");
    attr[6] = new Attribute("RB_JJ");
    attr[7] = new Attribute("amod");
    attr[8] = new Attribute("acomp");
    attr[9] = new Attribute("advmod");
    attr[10] = new Attribute("BLPos");
    attr[11] = new Attribute("BLNeg");

    //class
    FastVector classValue = new FastVector(3);
    classValue.addElement("p");
    classValue.addElement("n");
    classValue.addElement("o");
    attr[12] = new Attribute("answer", classValue);

    FastVector attrs = new FastVector();
    attrs.addElement(attr[0]);
    attrs.addElement(attr[1]);
    attrs.addElement(attr[2]);
    attrs.addElement(attr[3]);
    attrs.addElement(attr[4]);
    attrs.addElement(attr[5]);
    attrs.addElement(attr[6]);
    attrs.addElement(attr[7]);
    attrs.addElement(attr[8]);
    attrs.addElement(attr[9]);
    attrs.addElement(attr[10]);
    attrs.addElement(attr[11]);
    attrs.addElement(attr[12]);

    // Add Instances
    Instances dataset = new Instances("my_dataset", attrs, 0);

    if (new File(file).isFile()) {
        loader.setFile(new File(file));
        dataset = loader.getDataSet();
    }

    System.out.println("-----------------------------------------");
    System.out.println(input);
    System.out.println("-----------------------------------------");

    StringTokenizer tokenizer = new StringTokenizer(input);

    while (tokenizer.hasMoreTokens()) {
        Instance example = new Instance(13);
        for (int j = 0; j < 13; j++) {
            String st = tokenizer.nextToken();
            System.out.println(j + " " + st);
            if (j == 0)
                example.setValue(attr[j], Float.parseFloat(st));
            else if (j == 12)
                example.setValue(attr[j], st);
            else
                example.setValue(attr[j], Integer.parseInt(st));
        }
        dataset.add(example);
    }

    //Save dataset
    ArffSaver saver = new ArffSaver();
    saver.setInstances(dataset);
    saver.setFile(new File(file));
    saver.writeBatch();

    //Read dataset
    loader.setFile(new File(file));
    dataset = loader.getDataSet();

    //Build classifier
    dataset.setClassIndex(12);
    Classifier classifier = new J48();
    classifier.buildClassifier(dataset);

    //Save classifier
    String file1 = "Classifier\\classifier_add_asAndpolarwordsAndposAnddepAndbl.model";
    OutputStream os = new FileOutputStream(file1);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(os);
    objectOutputStream.writeObject(classifier);

    // Comment out if not needed
    //Read classifier back
    InputStream is = new FileInputStream(file1);
    ObjectInputStream objectInputStream = new ObjectInputStream(is);
    classifier = (Classifier) objectInputStream.readObject();
    objectInputStream.close();

    //Evaluate resample if needed
    //dataset = dataset.resample(new Random(42));
    //split to 70:30 learn and test set
    double percent = 70.0;
    int trainSize = (int) Math.round(dataset.numInstances() * percent / 100);
    int testSize = dataset.numInstances() - trainSize;
    Instances train = new Instances(dataset, 0, trainSize);
    Instances test = new Instances(dataset, trainSize, testSize);
    train.setClassIndex(12);
    test.setClassIndex(12);

    //Evaluate
    Evaluation eval = new Evaluation(dataset); //trainset
    eval.crossValidateModel(classifier, dataset, 10, new Random(1));
    System.out.println("EVALUATION:\n" + eval.toSummaryString());
    System.out.println("WEIGHTED MEASURE:\n" + eval.weightedFMeasure());
    System.out.println("WEIGHTED PRECISION:\n" + eval.weightedPrecision());
    System.out.println("WEIGHTED RECALL:\n" + eval.weightedRecall());
}