-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathtuneThreshold.py
More file actions
executable file
·33 lines (25 loc) · 899 Bytes
/
tuneThreshold.py
File metadata and controls
executable file
·33 lines (25 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/python
#-*- coding: utf-8 -*-
import os
import glob
import sys
import time
from sklearn import metrics
import numpy
import pdb
def tuneThresholdfromScore(scores, labels, target_fa, target_fr = None):
fpr, tpr, thresholds = metrics.roc_curve(labels, scores, pos_label=1)
fnr = 1 - tpr
fnr = fnr*100
fpr = fpr*100
tunedThreshold = [];
if target_fr:
for tfr in target_fr:
idx = numpy.nanargmin(numpy.absolute((tfr - fnr)))
tunedThreshold.append([thresholds[idx], fpr[idx], fnr[idx]]);
for tfa in target_fa:
idx = numpy.nanargmin(numpy.absolute((tfa - fpr))) # numpy.where(fpr<=tfa)[0][-1]
tunedThreshold.append([thresholds[idx], fpr[idx], fnr[idx]]);
idxE = numpy.nanargmin(numpy.absolute((fnr - fpr)))
eer = max(fpr[idxE],fnr[idxE])
return (tunedThreshold, eer, fpr, fnr);