Skip to content

Instantly share code, notes, and snippets.

@abhishek-Kumar009
Created February 10, 2019 15:29
Show Gist options
  • Save abhishek-Kumar009/d3be624d919afe70e63cc2b26b821960 to your computer and use it in GitHub Desktop.
Save abhishek-Kumar009/d3be624d919afe70e63cc2b26b821960 to your computer and use it in GitHub Desktop.
def selectThreshHold(yval, pval):
F1 = 0
bestF1 = 0
bestEpsilon = 0
stepsize = (np.max(pval) - np.min(pval))/1000
epsVec = np.arange(np.min(pval), np.max(pval), stepsize)
noe = len(epsVec)
for eps in range(noe):
epsilon = epsVec[eps]
pred = (pval < epsilon)
prec, rec = 0,0
tp,fp,fn = 0,0,0
try:
for i in range(np.size(pval,0)):
if pred[i] == 1 and yval[i] == 1:
tp+=1
elif pred[i] == 1 and yval[i] == 0:
fp+=1
elif pred[i] == 0 and yval[i] == 1:
fn+=1
prec = tp/(tp + fp)
rec = tp/(tp + fn)
F1 = 2*prec*rec/(prec + rec)
if F1 > bestF1:
bestF1 = F1
bestEpsilon = epsilon
except ZeroDivisionError:
print('Warning dividing by zero!!')
return bestF1, bestEpsilon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment