Example usage for weka.classifiers CostMatrix CostMatrix

List of usage examples for weka.classifiers CostMatrix CostMatrix

Introduction

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

Prototype

public CostMatrix(Reader reader) throws Exception 

Source Link

Document

Reads a matrix from a reader.

Usage

From source file:bme.mace.logicdomain.Evaluation.java

License:Open Source License

/**
 * Attempts to load a cost matrix.//from  w  ww . j a v  a  2 s  .  c  o m
 * 
 * @param costFileName the filename of the cost matrix
 * @param numClasses the number of classes that should be in the cost matrix
 *          (only used if the cost file is in old format).
 * @return a <code>CostMatrix</code> value, or null if costFileName is empty
 * @throws Exception if an error occurs.
 */
protected static CostMatrix handleCostOption(String costFileName, int numClasses) throws Exception {

    if ((costFileName != null) && (costFileName.length() != 0)) {
        System.out.println("NOTE: The behaviour of the -m option has changed between WEKA 3.0"
                + " and WEKA 3.1. -m now carries out cost-sensitive *evaluation*"
                + " only. For cost-sensitive *prediction*, use one of the"
                + " cost-sensitive metaschemes such as" + " weka.classifiers.meta.CostSensitiveClassifier or"
                + " weka.classifiers.meta.MetaCost");

        Reader costReader = null;
        try {
            costReader = new BufferedReader(new FileReader(costFileName));
        } catch (Exception e) {
            throw new Exception("Can't open file " + e.getMessage() + '.');
        }
        try {
            // First try as a proper cost matrix format
            return new CostMatrix(costReader);
        } catch (Exception ex) {
            try {
                // Now try as the poxy old format :-)
                // System.err.println("Attempting to read old format cost file");
                try {
                    costReader.close(); // Close the old one
                    costReader = new BufferedReader(new FileReader(costFileName));
                } catch (Exception e) {
                    throw new Exception("Can't open file " + e.getMessage() + '.');
                }
                CostMatrix costMatrix = new CostMatrix(numClasses);
                // System.err.println("Created default cost matrix");
                costMatrix.readOldFormat(costReader);
                return costMatrix;
                // System.err.println("Read old format");
            } catch (Exception e2) {
                // re-throw the original exception
                // System.err.println("Re-throwing original exception");
                throw ex;
            }
        }
    } else {
        return null;
    }
}

From source file:cotraining.copy.Evaluation_D.java

License:Open Source License

/**
 * Attempts to load a cost matrix.//from   ww w  .  j ava  2s  . c  om
 *
 * @param costFileName the filename of the cost matrix
 * @param numClasses the number of classes that should be in the cost matrix
 * (only used if the cost file is in old format).
 * @return a <code>CostMatrix</code> value, or null if costFileName is empty
 * @throws Exception if an error occurs.
 */
protected static CostMatrix handleCostOption(String costFileName, int numClasses) throws Exception {

    if ((costFileName != null) && (costFileName.length() != 0)) {
        System.out.println("NOTE: The behaviour of the -m option has changed between WEKA 3.0"
                + " and WEKA 3.1. -m now carries out cost-sensitive *evaluation*"
                + " only. For cost-sensitive *prediction*, use one of the"
                + " cost-sensitive metaschemes such as" + " weka.classifiers.meta.CostSensitiveClassifier or"
                + " weka.classifiers.meta.MetaCost");

        Reader costReader = null;
        try {
            costReader = new BufferedReader(new FileReader(costFileName));
        } catch (Exception e) {
            throw new Exception("Can't open file " + e.getMessage() + '.');
        }
        try {
            // First try as a proper cost matrix format
            return new CostMatrix(costReader);
        } catch (Exception ex) {
            try {
                // Now try as the poxy old format :-)
                //System.err.println("Attempting to read old format cost file");
                try {
                    costReader.close(); // Close the old one
                    costReader = new BufferedReader(new FileReader(costFileName));
                } catch (Exception e) {
                    throw new Exception("Can't open file " + e.getMessage() + '.');
                }
                CostMatrix costMatrix = new CostMatrix(numClasses);
                //System.err.println("Created default cost matrix");
                costMatrix.readOldFormat(costReader);
                return costMatrix;
                //System.err.println("Read old format");
            } catch (Exception e2) {
                // re-throw the original exception
                //System.err.println("Re-throwing original exception");
                throw ex;
            }
        }
    } else {
        return null;
    }
}

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 w  w w.j  a  v  a2s  . c om*/
        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   w  w w .j  ava  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:milk.classifiers.MIEvaluation.java

License:Open Source License

/**
   * Attempts to load a cost matrix.//from   w  w w  .  j  a  v  a2s. c o m
   *
   * @param costFileName the filename of the cost matrix
   * @param numClasses the number of classes that should be in the cost matrix
   * (only used if the cost file is in old format).
   * @return a <code>CostMatrix</code> value, or null if costFileName is empty
   * @exception Exception if an error occurs.
   */
  private static CostMatrix handleCostOption(String costFileName, int numClasses) throws Exception {

      if ((costFileName != null) && (costFileName.length() != 0)) {
          System.out.println("NOTE: The behaviour of the -m option has changed between WEKA 3.0"
                  + " and WEKA 3.1. -m now carries out cost-sensitive *evaluation*"
                  + " only. For cost-sensitive *prediction*, use one of the"
                  + " cost-sensitive metaschemes such as" + " weka.classifiers.CostSensitiveClassifier or"
                  + " weka.classifiers.MetaCost");

          Reader costReader = null;
          try {
              costReader = new BufferedReader(new FileReader(costFileName));
          } catch (Exception e) {
              throw new Exception("Can't open file " + e.getMessage() + '.');
          }
          try {
              // First try as a proper cost matrix format
              return new CostMatrix(costReader);
          } catch (Exception ex) {
              try {
                  // Now try as the poxy old format :-)
                  //System.err.println("Attempting to read old format cost file");
                  try {
                      costReader.close(); // Close the old one
                      costReader = new BufferedReader(new FileReader(costFileName));
                  } catch (Exception e) {
                      throw new Exception("Can't open file " + e.getMessage() + '.');
                  }
                  CostMatrix costMatrix = new CostMatrix(numClasses);
                  //System.err.println("Created default cost matrix");
                  costMatrix.readOldFormat(costReader);
                  return costMatrix;
                  //System.err.println("Read old format");
              } catch (Exception e2) {
                  // re-throw the original exception
                  //System.err.println("Re-throwing original exception");
                  throw ex;
              }
          }
      } else {
          return null;
      }
  }

From source file:org.openml.webapplication.algorithm.InstancesHelper.java

License:Open Source License

public static CostMatrix doubleToCostMatrix(double[][] cm) {
    CostMatrix costmatrix = new CostMatrix(cm.length);
    for (int i = 0; i < cm.length; ++i) {
        for (int j = 0; j < cm[i].length; ++j) {
            costmatrix.setElement(i, j, cm[i][j]);
        }/*from   w ww .  j a v  a 2 s . co m*/
    }
    return costmatrix;
}