Created
February 11, 2022 17:31
-
-
Save chyld/14978b94bb623dcb5e0cf97bc43181fe to your computer and use it in GitHub Desktop.
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 confusion_matrix | |
def roc_curve(probabilities, labels): | |
''' | |
INPUT: numpy array, numpy array | |
OUTPUT: list, list, list | |
Take a numpy array of the predicted probabilities and a numpy array of the | |
true labels. | |
Return the True Positive Rates, False Positive Rates and Thresholds for the | |
ROC curve. | |
''' | |
n = labels.shape[0] | |
fprs, tprs = np.zeros(n), np.zeros(n) | |
thrs = np.linspace(0, 1, 50) | |
for i, t in enumerate(thrs): | |
predicted = probabilities >= t | |
tn, fp, fn, tp = confusion_matrix(labels, predicted).flatten() | |
tprs[i] = tp / (tp + fn) | |
fprs[i] = fp / (fp + tn) | |
return fprs, tprs, thrs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment