Example usage for weka.classifiers.meta CostSensitiveClassifier CostSensitiveClassifier

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

Introduction

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

Prototype

public CostSensitiveClassifier() 

Source Link

Document

Default constructor.

Usage

From source file:core.classification.Classifiers.java

License:Open Source License

/**
 * Private constructor for the <code>Classifiers</code> object
 * @param train/*from   w  ww  .  ja  v  a 2 s.co m*/
 */
private Classifiers(boolean train) {
    SCA = new BayesNet();
    SCB = new MultilayerPerceptron();
    SCC1 = new MultilayerPerceptron();
    SCC2 = new MultilayerPerceptron();
    SCC3 = new MultilayerPerceptron();

    RC = new CostSensitiveClassifier();
    YNC = new J48();

    if (train) {
        try {
            this.trainSC();
        } catch (Exception e) {
            System.out.println("The system encountered the following error while training SC:");
            e.printStackTrace();
        }
        try {
            this.trainRC();
        } catch (Exception e) {
            System.out.println("The system encountered the following error while training RC:");
            e.printStackTrace();
        }
        try {
            this.trainYNC();
        } catch (Exception e) {
            System.out.println("The system encountered the following error while training YNC:");
            e.printStackTrace();
        }
    } else {
        try {
            readSC("SCA.model", "SCB.model", "SCC1.model", "SCC2.model", "SCC3.model");
            readRC("RC.model");
            readYNC("YNC.model");
        } catch (Exception e) {
            System.out.println("Error while reading the classifiers: ");
            e.printStackTrace();
        }
    }

    // Strutures Creations
    FastVector labels = new FastVector();
    labels.addElement("0");
    labels.addElement("1");
    labels.addElement("2");
    labels.addElement("3");
    labels.addElement("4");

    FastVector clabels = new FastVector();
    clabels.addElement("1");
    clabels.addElement("2");
    clabels.addElement("3");
    clabels.addElement("4");

    FastVector clabels2 = new FastVector();
    clabels2.addElement("0");
    clabels2.addElement("1");
    clabels2.addElement("2");
    clabels2.addElement("3");
    clabels2.addElement("4");

    FastVector clabels3 = new FastVector();
    clabels3.addElement("Y");
    clabels3.addElement("N");

    // Creating the structure for SC
    FastVector attrs = new FastVector();
    attrs.addElement(new Attribute("RATIO"));
    attrs.addElement(new Attribute("CLASS", clabels));
    dataStructSCA = new Instances("SCA-STRUCT", attrs, 0);
    dataStructSCA.setClassIndex(1);

    FastVector attrsB = new FastVector();
    attrsB.addElement(new Attribute("H2"));
    attrsB.addElement(new Attribute("D2"));
    attrsB.addElement(new Attribute("DX"));
    attrsB.addElement(new Attribute("PCLASS", clabels));
    attrsB.addElement(new Attribute("CLASS", clabels));
    dataStructSCB = new Instances("SCB-STRUCT", attrsB, 0);
    dataStructSCB.setClassIndex(4);

    FastVector attrsC1 = new FastVector();
    FastVector attrsC2 = new FastVector();
    FastVector attrsC3 = new FastVector();
    attrsC1.addElement(new Attribute("LH"));
    attrsC1.addElement(new Attribute("LD"));
    attrsC1.addElement(new Attribute("LDX"));
    attrsC1.addElement(new Attribute("LCLASS", clabels));
    attrsC1.addElement(new Attribute("CLASS", clabels));

    attrsC2.addElement(new Attribute("EH"));
    attrsC2.addElement(new Attribute("ED"));
    attrsC2.addElement(new Attribute("EDX"));
    attrsC2.addElement(new Attribute("ECLASS", clabels));
    attrsC2.addElement(new Attribute("CLASS", clabels));

    attrsC3.addElement(new Attribute("SH"));
    attrsC3.addElement(new Attribute("SD"));
    attrsC3.addElement(new Attribute("SDX"));
    attrsC3.addElement(new Attribute("SCLASS", clabels));
    attrsC3.addElement(new Attribute("CLASS", clabels));

    dataStructSCC1 = new Instances("SCC1-STRUCT", attrsC1, 0);
    dataStructSCC1.setClassIndex(4);

    dataStructSCC2 = new Instances("SCC2-STRUCT", attrsC2, 0);
    dataStructSCC2.setClassIndex(4);

    dataStructSCC3 = new Instances("SCC3-STRUCT", attrsC3, 0);
    dataStructSCC3.setClassIndex(4);

    FastVector attrs2 = new FastVector();
    attrs2.addElement(new Attribute("H2"));
    attrs2.addElement(new Attribute("D2"));
    attrs2.addElement(new Attribute("DX"));
    attrs2.addElement(new Attribute("CLASS", clabels));
    attrs2.addElement(new Attribute("PCLASS", clabels));
    attrs2.addElement(new Attribute("RELID", clabels2));
    dataStructRC = new Instances("RC-STRUCT", attrs2, 0);
    dataStructRC.setClassIndex(5);

    FastVector attrs3 = new FastVector();
    attrs3.addElement(new Attribute("PCLASS", clabels));
    attrs3.addElement(new Attribute("CCLASS", clabels));
    attrs3.addElement(new Attribute("RAREA"));
    attrs3.addElement(new Attribute("H"));
    attrs3.addElement(new Attribute("D"));
    attrs3.addElement(new Attribute("V"));
    attrs3.addElement(new Attribute("YN", clabels3));
    dataStructYC = new Instances("YC-STRUCT", attrs3, 0);
    dataStructYC.setClassIndex(6);
}

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 {/* w  w  w  .ja  v a  2s.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;
    }
}

From source file:jjj.asap.sas.models1.job.BuildBasicMetaCostModels.java

License:Open Source License

@Override
protected void run() throws Exception {

    // validate args
    if (!Bucket.isBucket("datasets", inputBucket)) {
        throw new FileNotFoundException(inputBucket);
    }//from  ww  w  . ja  va 2 s  .  c o m
    if (!Bucket.isBucket("models", outputBucket)) {
        throw new FileNotFoundException(outputBucket);
    }

    // create prototype classifiers
    Map<String, Classifier> prototypes = new HashMap<String, Classifier>();

    // Bagged REPTrees

    Bagging baggedTrees = new Bagging();
    baggedTrees.setNumExecutionSlots(1);
    baggedTrees.setNumIterations(100);
    baggedTrees.setClassifier(new REPTree());
    baggedTrees.setCalcOutOfBag(false);

    prototypes.put("Bagged-REPTrees", baggedTrees);

    // Bagged SMO

    Bagging baggedSVM = new Bagging();
    baggedSVM.setNumExecutionSlots(1);
    baggedSVM.setNumIterations(100);
    baggedSVM.setClassifier(new SMO());
    baggedSVM.setCalcOutOfBag(false);

    prototypes.put("Bagged-SMO", baggedSVM);

    // Meta Cost model for Naive Bayes

    Bagging bagging = new Bagging();
    bagging.setNumExecutionSlots(1);
    bagging.setNumIterations(100);
    bagging.setClassifier(new NaiveBayes());

    CostSensitiveClassifier meta = new CostSensitiveClassifier();
    meta.setClassifier(bagging);
    meta.setMinimizeExpectedCost(true);

    prototypes.put("CostSensitive-MinimizeExpectedCost-NaiveBayes", bagging);

    // init multi-threading
    Job.startService();
    final Queue<Future<Object>> queue = new LinkedList<Future<Object>>();

    // get the input from the bucket
    List<String> names = Bucket.getBucketItems("datasets", this.inputBucket);
    for (String dsn : names) {

        // for each prototype classifier
        for (Map.Entry<String, Classifier> prototype : prototypes.entrySet()) {

            // 
            // speical logic for meta cost
            //

            Classifier alg = AbstractClassifier.makeCopy(prototype.getValue());

            if (alg instanceof CostSensitiveClassifier) {

                int essaySet = Contest.getEssaySet(dsn);

                String matrix = Contest.getRubrics(essaySet).size() == 3 ? "cost3.txt" : "cost4.txt";

                ((CostSensitiveClassifier) alg)
                        .setCostMatrix(new CostMatrix(new FileReader("/asap/sas/trunk/" + matrix)));

            }

            // use InfoGain to discard useless attributes

            AttributeSelectedClassifier classifier = new AttributeSelectedClassifier();

            classifier.setEvaluator(new InfoGainAttributeEval());

            Ranker ranker = new Ranker();
            ranker.setThreshold(0.0001);
            classifier.setSearch(ranker);

            classifier.setClassifier(alg);

            queue.add(Job.submit(
                    new ModelBuilder(dsn, "InfoGain-" + prototype.getKey(), classifier, this.outputBucket)));
        }
    }

    // wait on complete
    Progress progress = new Progress(queue.size(), this.getClass().getSimpleName());
    while (!queue.isEmpty()) {
        try {
            queue.remove().get();
        } catch (Exception e) {
            Job.log("ERROR", e.toString());
        }
        progress.tick();
    }
    progress.done();
    Job.stopService();

}

From source file:org.conqat.engine.commons.machine_learning.BaseWekaClassifier.java

License:Apache License

/**
 * Returns a new classifier based on the given algorithm.
 *//*from w  w  w.ja  va 2 s . c o  m*/
protected weka.classifiers.Classifier getClassifier(EClassificationAlgorithm algorithm) {
    switch (algorithm) {
    case DECISION_TREE_REP:
        return new REPTree();
    case SUPPORT_VECTOR_MACHINE_SMO:
        return new SMO();
    case COST_SENSITIVE_CLASSIFIER:
        return new CostSensitiveClassifier();
    case DECISION_TREE_J48:
        return new J48();
    default:
        throw new AssertionError("Cannot create a classifier without a specified algorithm.");
    }

}