Example usage for weka.classifiers.meta CostSensitiveClassifier setCostMatrix

List of usage examples for weka.classifiers.meta CostSensitiveClassifier setCostMatrix

Introduction

In this page you can find the example usage for weka.classifiers.meta CostSensitiveClassifier setCostMatrix.

Prototype

public void setCostMatrix(CostMatrix newCostMatrix) 

Source Link

Document

Sets the misclassification cost matrix.

Usage

From source file:etc.aloe.cscw2013.TrainingImpl.java

License:Open Source License

@Override
public WekaModel train(ExampleSet examples) {
    System.out.println("SMO Options: " + SMO_OPTIONS);
    SMO smo = new SMO();
    try {/*from   ww  w  .j a  va 2 s.  c o  m*/
        smo.setOptions(Utils.splitOptions(SMO_OPTIONS));
    } catch (Exception ex) {
        System.err.println("Unable to configure SMO.");
        System.err.println("\t" + ex.getMessage());
        return null;
    }

    //Build logistic models if desired
    smo.setBuildLogisticModels(isBuildLogisticModel());

    Classifier classifier = smo;

    if (useCostTraining) {
        CostSensitiveClassifier cost = new CostSensitiveClassifier();
        cost.setClassifier(smo);
        CostMatrix matrix = new CostMatrix(2);
        matrix.setElement(0, 0, 0);
        matrix.setElement(0, 1, falsePositiveCost);
        matrix.setElement(1, 0, falseNegativeCost);
        matrix.setElement(1, 1, 0);
        cost.setCostMatrix(matrix);

        classifier = cost;

        System.out.print("Wrapping SMO in CostSensitiveClassifier " + matrix.toMatlab());

        if (useReweighting) {
            cost.setMinimizeExpectedCost(false);
            System.out.println(" using re-weighting.");
        } else {
            cost.setMinimizeExpectedCost(true);
            System.out.println(" using min-cost criterion.");
        }
    }

    try {
        System.out.print("Training SMO on " + examples.size() + " examples... ");
        classifier.buildClassifier(examples.getInstances());
        System.out.println("done.");

        WekaModel model = new WekaModel(classifier);
        return model;
    } catch (Exception ex) {
        System.err.println("Unable to train SMO.");
        System.err.println("\t" + ex.getMessage());
        return null;
    }
}