Write a function from scratch called roc_curve_computer that accepts (in this exact order): a list of true labels a list of prediction probabilities (notice these are probabilities and not predictions - you will need to obtain the predictions from these probabilities) a list of threshold values. The function must compute and
Compute and Plot the ROC Curve
Write a function from scratch called roc_curve_computer that accepts (in this exact order):
- a list of true labels
- a list of prediction probabilities (notice these are probabilities and not predictions - you will need to obtain the predictions from these probabilities)
- a list of threshold values.
The function must compute and return the True Positive Rate (TPR, also called recall) and the False Positive Rate (FPR) for each threshold value in the threshold value list that is passed to the function.
Important: Be sure to reuse functions and code segments from your work above! You should reuse two of your above created functions so that you do not duplicate your code.
The function you will write behaves identically to Scikit-Learn's roc_curve function, except that it will take the list of thresholds in as input rather than return them as output. Your function must calculate one value of TPR and one value of FPR for each of the threshold values in the list.
Your function will output a list of TPR values and a list of FPR values (in that order). You will then take these TPR and FPR values, and plot them against each other to create the Receiver Operating Characteristic (ROC) curve.
You must not use any built-in library function to perform the calculation of a performance metric. You may of course use common, built-in Python functions, such as: range(), len(), et cetera.
Code Check: As an example, calling the roc_curve_computer function with the input true_labels = [1, 0, 1, 0, 0], pred_probs = [0.875, 0.325, 0.6, 0.09, 0.4], and thresholds = [0.00, 0.25, 0.50, 0.75, 1.00] yields the output:
TPR = [1.0, 1.0, 1.0, 0.5, 0.0] and FPR = [1.0, 0.6666, 0.0, 0.0, 0.0].
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images