public static void createROC(ClassifierOutput classifierOutput, String filename) { WriteTextFile out = null; if (outputROCcsv) { out = new WriteTextFile(filename.replace(".gif", ".csv")); out.writeln("FPR,TPR"); } BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = (Graphics2D) img.getGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setColor(Color.WHITE); g2d.fillRect(0, 0, width, height); renderAxes(g2d, 0.2f, 1f); // renderDiagonal(g2d); renderROC(g2d, classifierOutput, out); if (includeAuCinROC) { double auc = ClassifierEvaluator.areaUnderCurveConfidenceInterval(classifierOutput).pointEstimate; g2d.setColor(Color.black); g2d.setFont(new Font("Arial", Font.PLAIN, 50)); FontMetrics metrics = g2d.getFontMetrics(); String text = "AUC = " + StringUtilities.formatNumber("0.00", auc); int textWidth = metrics.stringWidth(text); int textHeight = metrics.getHeight(); g2d.drawString( text, Math.round(width - textWidth - borderWidth), Math.round(height - textHeight) + 7); } try { ImageIO.write(img, "GIF", new File(filename)); } catch (IOException e) { e.printStackTrace(); } if (out != null) out.close(); }
private static void renderROC( Graphics2D g2d, ClassifierOutput classifierOutput, WriteTextFile out) { Collections.sort(classifierOutput.scoreLabelPairs); int postiveCount = countPositives(classifierOutput); g2d.setColor(new Color(200, 0, 0)); g2d.setStroke(new BasicStroke(4)); float areaWidth = width - 2 * borderWidth; float areaHeight = height - 2 * borderWidth; float x = borderWidth; float y = height - borderWidth; float previousX = x; float previousY = y; double score = classifierOutput.scoreLabelPairs.get(0).score; int fpCount = 0; int tpCount = 0; for (ScoreLabelPair pair : classifierOutput.scoreLabelPairs) { if (pair.score != score) { // In case of a tie, just make a linear interpolation g2d.draw(new Line2D.Float(previousX, previousY, x, y)); previousX = x; previousY = y; score = pair.score; } if (pair.label) tpCount++; else fpCount++; float fpr = (float) fpCount / (float) (classifierOutput.scoreLabelPairs.size() - postiveCount); float tpr = (float) tpCount / (float) postiveCount; if (out != null) out.writeln(Float.toString(fpr) + "," + Float.toString(tpr)); x = borderWidth + fpr * areaWidth; y = height - borderWidth - tpr * areaHeight; } g2d.draw(new Line2D.Float(previousX, previousY, x, y)); if (out != null) out.writeln("1,1"); }