Instantly share code, notes, and snippets.

# joernhees/recallPrecision.py Created Oct 28, 2014

plot recall precision diagrams with f-measure height lines
 # -*- coding: utf-8 -*- """ Script to plot recall-precision values with f-measure equi-potential lines. Created on Dec 16, 2009 @author: Jörn Hees """ import scipy as sc import pylab as pl import itertools as it def fmeasure(p, r): """Calculates the fmeasure for precision p and recall r.""" return 2*p*r / (p+r) def _fmeasureCurve(f, p): """For a given f1 value and precision get the recall value. The f1 measure is defined as: f(p,r) = 2*p*r / (p + r). If you want to plot "equipotential-lines" into a precision/recall diagramm (recall (y) over precision (x)), for a given fixed f value we get this function by solving for r: """ return f * p / (2 * p - f) def _plotFMeasures(fstepsize=.1, stepsize=0.001): """Plots 10 fmeasure Curves into the current canvas.""" p = sc.arange(0., 1., stepsize)[1:] for f in sc.arange(0., 1., fstepsize)[1:]: points = [(x, _fmeasureCurve(f, x)) for x in p if 0 < _fmeasureCurve(f, x) <= 1.5] xs, ys = zip(*points) curve, = pl.plot(xs, ys, "--", color="gray", linewidth=0.5) # , label=r"\$f=%.1f\$"%f) # exclude labels, for legend # bad hack: # gets the 10th last datapoint, from that goes a bit to the left, and a bit down pl.annotate(r"\$f=%.1f\$" % f, xy=(xs[-10], ys[-10]), xytext=(xs[-10] - 0.05, ys[-10] - 0.035), size="small", color="gray") # def _contourPlotFMeasure(): # delta = 0.01 # x = sc.arange(0.,1.,delta) # y = sc.arange(0.,1.,delta) # X,Y = sc.meshgrid(x,y) # cs = pl.contour(X,Y,fmeasure,sc.arange(0.1,1.0,0.1)) # FIXME: make an array out of fmeasure first # pl.clabel(cs, inline=1, fontsize=10) colors = "bgrcmyk" # 7 is a prime, so we'll loop over all combinations of colors and markers, when zipping their cycles markers = "so^>v
to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.