Ejemplo n.º 1
0
  static void mainToOutput(String[] args, PrintWriter output) throws Exception {
    if (parseArgs(args)) {
      if (!showAuc && !showConfusion && !showScores) {
        showAuc = true;
        showConfusion = true;
      }

      Auc collector = new Auc();
      LogisticModelParameters lmp = LogisticModelParameters.loadFrom(new File(modelFile));

      CsvRecordFactory csv = lmp.getCsvRecordFactory();
      OnlineLogisticRegression lr = lmp.createRegression();
      BufferedReader in = TrainLogistic.open(inputFile);
      String line = in.readLine();
      csv.firstLine(line);
      line = in.readLine();
      if (showScores) {
        output.println("\"target\",\"model-output\",\"log-likelihood\"");
      }
      while (line != null) {
        Vector v = new SequentialAccessSparseVector(lmp.getNumFeatures());
        int target = csv.processLine(line, v);

        double score = lr.classifyScalar(v);
        if (showScores) {
          output.printf(
              Locale.ENGLISH, "%d,%.3f,%.6f%n", target, score, lr.logLikelihood(target, v));
        }
        collector.add(target, score);
        line = in.readLine();
      }

      if (showAuc) {
        output.printf(Locale.ENGLISH, "AUC = %.2f%n", collector.auc());
      }
      if (showConfusion) {
        Matrix m = collector.confusion();
        output.printf(
            Locale.ENGLISH,
            "confusion: [[%.1f, %.1f], [%.1f, %.1f]]%n",
            m.get(0, 0),
            m.get(1, 0),
            m.get(0, 1),
            m.get(1, 1));
        m = collector.entropy();
        output.printf(
            Locale.ENGLISH,
            "entropy: [[%.1f, %.1f], [%.1f, %.1f]]%n",
            m.get(0, 0),
            m.get(1, 0),
            m.get(0, 1),
            m.get(1, 1));
      }
    }
  }
 @Override
 public double getLoss(
     List<? extends AbstractInstance> crossValSet, PredictiveModel predictiveModel) {
   Auc auc = new Auc();
   for (AbstractInstance instance : crossValSet) {
     auc.add(
         (Double) instance.getClassification() == 1.0 ? 1 : 0,
         predictiveModel.getProbability(instance.getAttributes(), 1.0));
   }
   return 1 - auc.auc();
 }