Skip to content

Instantly share code, notes, and snippets.

@zaxcie
Created January 21, 2018 05:01
Show Gist options
  • Save zaxcie/fbcaaa9cd77ebafe1ef1af83a2906d55 to your computer and use it in GitHub Desktop.
Save zaxcie/fbcaaa9cd77ebafe1ef1af83a2906d55 to your computer and use it in GitHub Desktop.
Find the cut off point of a binary classification python
def Find_Optimal_Cutoff(target, predicted):
""" Find the optimal probability cutoff point for a classification model related to event rate
Parameters
----------
target : Matrix with dependent or target data, where rows are observations
predicted : Matrix with predicted data, where rows are observations
Returns
-------
list type, with optimal cutoff value
"""
fpr, tpr, threshold = roc_curve(target, predicted)
i = np.arange(len(tpr))
roc = pd.DataFrame({'tf' : pd.Series(tpr-(1-fpr), index=i), 'threshold' : pd.Series(threshold, index=i)})
roc_t = roc.ix[(roc.tf-0).abs().argsort()[:1]]
return list(roc_t['threshold'])
@akshaynarkar18
Copy link

Any source code of this function available
plz hepl

@zaxcie
Copy link
Author

zaxcie commented Jul 8, 2019

This is the source code. What are you looking for exactly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment