TrainLogistic { static void mainToOutput(String[] args, PrintWriter output) throws Exception { if (parseArgs(args)) { double logPEstimate = 0; int samples = 0; CsvRecordFactory csv = lmp.getCsvRecordFactory(); OnlineLogisticRegression lr = lmp.createRegression(); for (int pass = 0; pass < passes; pass++) { try (BufferedReader in = open(inputFile)) { csv.firstLine(in.readLine()); String line = in.readLine(); while (line != null) { Vector input = new RandomAccessSparseVector(lmp.getNumFeatures()); int targetValue = csv.processLine(line, input); double logP = lr.logLikelihood(targetValue, input); if (!Double.isInfinite(logP)) { if (samples < 20) { logPEstimate = (samples * logPEstimate + logP) / (samples + 1); } else { logPEstimate = 0.95 * logPEstimate + 0.05 * logP; } samples++; } double p = lr.classifyScalar(input); if (scores) { output.printf(Locale.ENGLISH, "%10d %2d %10.2f %2.4f %10.4f %10.4f%n", samples, targetValue, lr.currentLearningRate(), p, logP, logPEstimate); } lr.train(targetValue, input); line = in.readLine(); } } } try (OutputStream modelOutput = new FileOutputStream(outputFile)) { lmp.saveTo(modelOutput); } output.println(lmp.getNumFeatures()); output.println(lmp.getTargetVariable() + " ~ "); String sep = ""; for (String v : csv.getTraceDictionary().keySet()) { double weight = predictorWeight(lr, 0, csv, v); if (weight != 0) { output.printf(Locale.ENGLISH, "%s%.3f*%s", sep, weight, v); sep = " + "; } } output.printf("%n"); model = lr; for (int row = 0; row < lr.getBeta().numRows(); row++) { for (String key : csv.getTraceDictionary().keySet()) { double weight = predictorWeight(lr, row, csv, key); if (weight != 0) { output.printf(Locale.ENGLISH, "%20s %.5f%n", key, weight); } } for (int column = 0; column < lr.getBeta().numCols(); column++) { output.printf(Locale.ENGLISH, "%15.9f ", lr.getBeta().get(row, column)); } output.println(); } } } private  TrainLogistic(); static void main(String[] args); static OnlineLogisticRegression getModel(); static LogisticModelParameters getParameters();  }
@Test public void example132() throws Exception { String outputFile = getTestTempFile("model").getAbsolutePath(); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw, true); TrainLogistic.mainToOutput(new String[]{ "--input", "donut.csv", "--output", outputFile, "--target", "color", "--categories", "2", "--predictors", "x", "y", "a", "b", "c", "--types", "numeric", "--features", "20", "--passes", "100", "--rate", "50" }, pw); String trainOut = sw.toString(); assertTrue(trainOut.contains("a 0.")); assertTrue(trainOut.contains("b -1.")); assertTrue(trainOut.contains("c -25.")); sw = new StringWriter(); pw = new PrintWriter(sw, true); RunLogistic.mainToOutput(new String[]{ "--input", "donut.csv", "--model", outputFile, "--auc", "--confusion" }, pw); trainOut = sw.toString(); assertTrue(trainOut.contains("AUC = 1.00")); sw = new StringWriter(); pw = new PrintWriter(sw, true); RunLogistic.mainToOutput(new String[]{ "--input", "donut-test.csv", "--model", outputFile, "--auc", "--confusion" }, pw); trainOut = sw.toString(); assertTrue(trainOut.contains("AUC = 0.9")); }