How to calculate TPR and FPR in Python without using sklearn?

Written by- Aionlinecourse3018 times views

To calculate true positive rate (TPR) and false positive rate (FPR) in Python, you can use the following steps:

1.  First, you will need to have a set of predictions and a set of ground truth labels. Let's say you have two lists: predictions and labels, where predictions contains your model's predictions and labels contains the ground truth labels.
2. You will also need to decide on a threshold value for classifying a prediction as positive. This threshold can be any value between 0 and 1, depending on the desired sensitivity and specificity of your model. Let's say you choose a threshold of 0.5.
3.  Next, you can iterate through the predictions and labels and count the number of true positives (TP), false positives (FP), true negatives (TN), and false negatives (FN). A true positive is a prediction that is positive (above the threshold) and the ground truth label is also positive. A false positive is a prediction that is positive (above the threshold) but the ground truth label is negative. A true negative is a prediction that is negative (below the threshold) and the ground truth label is also negative. A false negative is a prediction that is negative (below the threshold) but the ground truth label is positive.
4. Once you have counted the number of TP, FP, TN, and FN, you can calculate the TPR and FPR as follows:

TPR = TP / (TP + FN)
FPR = FP / (FP + TN)

Here is some example code that shows how to implement this in Python:

predictions = [0.9, 0.3, 0.8, 0.1, 0.2]
labels = [1, 0, 1, 0, 0]
threshold = 0.5

TP = 0
FP = 0
TN = 0
FN = 0

for i in range(len(predictions)):
  if predictions[i] > threshold:
    # Prediction is positive
    if labels[i] == 1:
      # True positive
      TP += 1
    else:
      # False positive
      FP += 1
  else:
    # Prediction is negative
    if labels[i] == 0:
      # True negative
      TN += 1
    else:
      # False negative
      FN += 1

TPR = TP / (TP + FN)
FPR = FP / (FP + TN)

print("TPR:", TPR)
print("FPR:", FPR)