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

Written by - Aionlinecourse3348 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)

Recommended Projects

Deep Learning Interview Guide

Topic modeling using K-means clustering to group customer reviews

Have you ever thought about the ways one can analyze a review to extract all the misleading or useful information?...

Natural Language Processing
Deep Learning Interview Guide

Medical Image Segmentation With UNET

Have you ever thought about how doctors are so precise in diagnosing any conditions based on medical images? Quite simply,...

Computer Vision
Deep Learning Interview Guide

Build A Book Recommender System With TF-IDF And Clustering(Python)

Have you ever thought about the reasons behind the segregation and recommendation of books with similarities? This project is aimed...

Machine LearningDeep LearningNatural Language Processing
Deep Learning Interview Guide

Automatic Eye Cataract Detection Using YOLOv8

Cataracts are a leading cause of vision impairment worldwide, affecting millions of people every year. Early detection and timely intervention...

Computer Vision
Deep Learning Interview Guide

Crop Disease Detection Using YOLOv8

In this project, we are utilizing AI for a noble objective, which is crop disease detection. Well, you're here if...

Computer Vision
Deep Learning Interview Guide

Vegetable classification with Parallel CNN model

The Vegetable Classification project shows how CNNs can sort vegetables efficiently. As industries like agriculture and food retail grow, automating...

Machine LearningDeep Learning
Deep Learning Interview Guide

Banana Leaf Disease Detection using Vision Transformer model

Banana cultivation is a significant agricultural activity in many tropical and subtropical regions, providing a vital source of income and...

Deep LearningComputer Vision