Skip to content

Instantly share code, notes, and snippets.

@sir-wabbit
Created January 17, 2017 21:58
Show Gist options
  • Select an option

  • Save sir-wabbit/fb3aff25ada932a96ea6a931848ac32a to your computer and use it in GitHub Desktop.

Select an option

Save sir-wabbit/fb3aff25ada932a96ea6a931848ac32a to your computer and use it in GitHub Desktop.
def gen_tprf(predictions, positive_count=None, shuffle=True, sort=True):
predictions = predictions if not shuffle else \
fx.shuffle(predictions)
predictions = predictions if not sort else \
sorted(predictions, key=lambda p: -p.p)
N = positive_count if positive_count is not None else \
sum(1 for x in predictions if x.y == 1)
tp = 0
fp = 0
fn = 0
for p in predictions:
if p.y == 1:
tp += 1
elif p.y == -1:
fp += 1
# else:
# assert False
fn = N - tp
T = p.p
P = 0.0
if tp + fp > 0:
P = tp / (tp + fp)
R = 0.0
if tp + fn > 0:
R = tp / (tp + fn)
F = 0.0
if P + R > 0:
F = 2 * P * R / (P + R)
yield TPRF(
threshold=T,
precision=P,
recall=R,
f1=F)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment