Last active
July 15, 2019 18:52
-
-
Save allanbatista/eda98d6ab47a78af1619a820d033ea1b to your computer and use it in GitHub Desktop.
Calculate best thresholds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.metrics import roc_curve | |
def calculate_thresholds(n_classes, y_true, y_pred): | |
""" | |
n_classes => 123 | |
y_true => [[1, 0], ...] # one hot encode lavels | |
y_pred => np.array([[0.9, 0.3]]) # with probabilities | |
""" | |
fpr = dict() | |
tpr = dict() | |
thresholds = dict() | |
best_thresholds = dict() | |
for i in n_classes: | |
fpr[i], tpr[i], thresholds[i] = roc_curve(y_true[:, i], y_pred[:, i]) | |
tf = tpr[i]-(1-fpr[i]) | |
best_thresholds[i] = 1-thresholds[i][np.absolute(tf).argsort()[0]] | |
return { | |
'fpr': fpr, | |
'tpr': tpr, | |
'thresholds': thresholds, | |
'best_thresholds': best_thresholds | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment