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)); } } }
static void mainToOutput(String[] args, PrintWriter output) throws Exception { if (!parseArgs(args)) { return; } AdaptiveLogisticModelParameters lmp = AdaptiveLogisticModelParameters.loadFromFile(new File(modelFile)); CsvRecordFactory csv = lmp.getCsvRecordFactory(); csv.setIdName(idColumn); AdaptiveLogisticRegression lr = lmp.createAdaptiveLogisticRegression(); State<Wrapper, CrossFoldLearner> best = lr.getBest(); if (best == null) { output.println("AdaptiveLogisticRegression has not be trained probably."); return; } CrossFoldLearner learner = best.getPayload().getLearner(); BufferedReader in = TrainAdaptiveLogistic.open(inputFile); BufferedWriter out = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(outputFile), Charsets.UTF_8)); out.write(idColumn + ",target,score"); out.newLine(); String line = in.readLine(); csv.firstLine(line); line = in.readLine(); Map<String, Double> results = new HashMap<String, Double>(); int k = 0; while (line != null) { Vector v = new SequentialAccessSparseVector(lmp.getNumFeatures()); csv.processLine(line, v, false); Vector scores = learner.classifyFull(v); results.clear(); if (maxScoreOnly) { results.put(csv.getTargetLabel(scores.maxValueIndex()), scores.maxValue()); } else { for (int i = 0; i < scores.size(); i++) { results.put(csv.getTargetLabel(i), scores.get(i)); } } for (Map.Entry<String, Double> entry : results.entrySet()) { out.write(csv.getIdString(line) + ',' + entry.getKey() + ',' + entry.getValue()); out.newLine(); } k++; if (k % 100 == 0) { output.println(k + " records processed"); } line = in.readLine(); } out.flush(); out.close(); output.println(k + " records processed totally."); }