Roc Curve in Python
Roc Curve in Python
P1 1 0.95
P2 1 0.90
P3 0 0.85
P4 0 0.81
P5 1 0.78
P6 0 0.70
Pair isCorrect
(P1,P3) Yes
(P1,P4) Yes
(P1,P6) Yes
(P2,P3) Yes
(P2,P4) Yes
(P2,P6) Yes
(P3,P5) No
(P4,P5) No
Pair isCorrect
(P5,P6) Yes
Predicted
Labels
Predicted (if Threshold =
True Labels Probabilities 0.5)
1 0.8 1
0 0.3 0
1 0.6 1
0 0.2 0
1 0.7 1
1 0.9 1
Predicted
Labels
Predicted (if Threshold =
True Labels Probabilities 0.5)
0 0.4 0
0 0.1 0
1 0.75 1
0 0.55 1
Prediction = 0 Prediction = 1
Accordingly,
True Positive Rate (TPR):
Proportion of actual positives correctly
identified by the classifier is
TPR=TP/(TP+FN)=4/4+1=0.8
TPR=TP+FN/TP=4+1/4=0.8
False Positive Rate (FPR):
Proportion of actual negatives incorrectly
classified as positives
FPR=FP/(FP+TN)=00+5=0
FPR=FP+TN/FP=0+50=0
So, at the threshold of 0.5:
True Positive Rate (Sensitivity): 0.8
False Positive Rate: 0
The interpretation is that the model, at this
threshold, correctly identifies 80% of actual
positives (TPR) but incorrectly classifies 0% of
actual negatives as positives (FPR).
Accordingly for different thresholds we will get ,
Case 2: Threshold = 0.7
Predicted
Labels
Predicted (if Threshold =
True Labels Probabilities 0.7)
1 0.8 1
0 0.3 0
1 0.6 0
0 0.2 0
1 0.7 0
1 0.9 1
0 0.4 0
0 0.1 0
Predicted
Labels
Predicted (if Threshold =
True Labels Probabilities 0.7)
1 0.75 1
0 0.55 0
Prediction = 0 Prediction = 1
Accordingly,
True Positive Rate (TPR):
Proportion of actual positives correctly
identified by the classifier is
TPR=TPTP+FN=55+0=1.0TPR=TP+FNTP=5+05
=1.0
False Positive Rate (FPR):
Proportion of actual negatives incorrectly
classified as positives
FPR=FPFP+TN=11+4=0.2FPR=FP+TNFP=1+41
=0.2
Case 3: Threshold = 0.4
Predicted
Labels
Predicted (if Threshold =
True Labels Probabilities 0.4)
1 0.8 1
0 0.3 0
1 0.6 1
0 0.2 0
1 0.7 1
Predicted
Labels
Predicted (if Threshold =
True Labels Probabilities 0.4)
1 0.9 1
0 0.4 0
0 0.1 0
1 0.75 1
0 0.55 1
Prediction = 0 Prediction = 1
Accordingly,
True Positive Rate (TPR):
Proportion of actual positives correctly
identified by the classifier is
TPR=TPTP+FN=44+1=0.8TPR=TP+FNTP=4+14
=0.8
False Positive Rate (FPR):
Proportion of actual negatives incorrectly
classified as positives
FPR=FPFP+TN=00+5=0FPR=FP+TNFP=0+50=0
Case 4: Threshold = 0.2
Predicted
Labels
Predicted (if Threshold =
True Labels Probabilities 0.2)
1 0.8 1
0 0.3 1
1 0.6 1
Predicted
Labels
Predicted (if Threshold =
True Labels Probabilities 0.2)
0 0.2 0
1 0.7 1
1 0.9 1
0 0.4 1
0 0.1 0
1 0.75 1
0 0.55 1
Accordingly,
True Positive Rate (TPR):
Proportion of actual positives correctly
identified by the classifier is
TPR=TPTP+FN=22+3=0.4TPR=TP+FNTP=2+32
=0.4
False Positive Rate (FPR):
Proportion of actual negatives incorrectly
classified as positives
FPR=FPFP+TN=00+5=0FPR=FP+TNFP=0+50=0
Case 5: Threshold = 0.85
Predicted
Labels
Predicted (if Threshold =
True Labels Probabilities 0.85)
1 0.8 0
0 0.3 0
1 0.6 0
0 0.2 0
1 0.7 0
1 0.9 1
0 0.4 0
0 0.1 0
1 0.75 0
Predicted
Labels
Predicted (if Threshold =
True Labels Probabilities 0.85)
0 0.55 0
Prediction = 0 Prediction = 1
Accordingly,
True Positive Rate (TPR):
Proportion of actual positives correctly
identified by the classifier is
TPR=TPTP+FN=55+0=1.0TPR=TP+FNTP=5+05
=1.0
False Positive Rate (FPR):
Proportion of actual negatives incorrectly
classified as positives
FPR=FPFP+TN=44+1=0.8FPR=FP+TNFP=4+14=0.8
PROGRAM
Step 1: Importing the required libraries
In scikit-learn, the roc_curve function is used to
compute Receiver Operating Characteristic (ROC)
curve points. On the other hand, the auc function
calculates the Area Under the Curve (AUC) from
the ROC curve.
AUC is a scalar value representing the area under
the ROC curve quantifing the classifier's ability to
distinguish between positive and negative
examples across all possible classification
thresholds.
Python3
import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import
train_test_split
from sklearn.linear_model import
LogisticRegression
from sklearn.metrics import roc_curve, auc
PROGRAM
Step 1: Import Packages
#display plot
plt.show()
Additional information
Machine learning models are the mathematical engines that drive Artificial
Intelligence and thus are highly vital for successful AI implementation. In fact, you
could say that your AI is only as good as the machine models that drive them.
So, now convinced of the importance of a good machine learning model, you apply
yourself to the task, and after some hard work, you finally create what you believe to
be a great machine learning model. Congratulations!
But wait. How can you tell if your machine learning model is as good as you believe
it is? Clearly, you need an objective means of measuring your machine learning
model’s performance and determining if it’s good enough for implementation. It
would help if you had a ROC curve.
This article has everything you need to know about ROC curves. We will define ROC
curves and the term “area under the ROC curve,” how to use ROC curves in
performance modeling, and a wealth of other valuable information. We begin with
some definitions.
A ROC (which stands for “receiver operating characteristic”) curve is a graph that shows a
classification model performance at all classification thresholds. It is a probability curve that
plots two parameters, the True Positive Rate (TPR) against the False Positive Rate (FPR), at
different threshold values and separates a so-called ‘signal’ from the ‘noise.’
The ROC curve plots the True Positive Rate against the False Positive Rate at different
classification thresholds. If the user lowers the classification threshold, more items get
classified as positive, which increases both the False Positives and True Positives. You can
see some imagery regarding this here.
ROC Curve
1. True Positive Rate (TPR): Also known as sensitivity or recall, it measures the proportion of actual
positives correctly identified by the model. It is calculated as:
TPR=True Positives/(TP)True Positives (TP)+False Negatives
2. False Positive Rate (FPR): This measures the proportion of actual negatives incorrectly identified
as positives by the model. It is calculated as:
FPR=False Positives (FP)/False Positives (FP)+True Negatives (TN)
The ROC curve plots TPR (y-axis) against FPR (x-axis) at various threshold settings. Here's a
more detailed explanation of these metrics:
True Positive (TP): The instance is positive, and the model correctly classifies it as positive.
False Positive (FP): The instance is negative, but the model incorrectly classifies it as positive.
True Negative (TN): The instance is negative, and the model correctly classifies it as negative.
False Negative (FN): The instance is positive, but the model incorrectly classifies it as negative.
The area under the ROC curve (AUC) is a single scalar value that measures the model's overall
performance. It ranges from 0 to 1, and a higher AUC indicates a better-performing model.
The Area Under the ROC Curve (AUC) is a single scalar value that summarizes the overall
performance of a binary classification model. It measures the ability of the model to
distinguish between the positive and negative classes. Here's what you need to know about
AUC:
Range of AUC:
An AUC of 0.5 indicates a model that performs no better than random chance.
Threshold Independent: AUC evaluates the model's performance across all possible classification
thresholds.
Scale Invariant: AUC measures how well the predictions are ranked rather than their absolute
values.
Calculation of AUC:
AUC is typically calculated using numerical integration methods, such as the trapezoidal rule,
applied to the ROC curve.
In practical terms, libraries like Scikit-learn in Python provide functions to compute AUC directly
from model predictions and true labels.
11 months months
View Program
11 Months months
View Program
Abhineet Srivastava
Senior Manager - Analytics & Reporting, AXA
The machine learning course module was truly worth every moment! The content was not
only comprehensive but also up-to-date with the latest advancements. The progression from
Python fundamentals to grasping statistical concepts and diving deep into machine learning
was incredible. Thanks to the amazing trainers and supportive co-learners.
Example: In a medical test, a TP is when the test correctly identifies a person with a disease as
having the disease.
Example: In a spam filter, a TN is when a legitimate email is correctly identified as not spam.
Definition: The number of negative instances incorrectly identified as positive by the model.
Definition: The number of positive instances incorrectly identified as negative by the model.
Example: In a cancer screening test, an FN is when a person with cancer is incorrectly identified as
not having cancer.
Definition: Also known as sensitivity or recall, it measures the proportion of actual positives that
are correctly identified by the model.
Formula: TPR=TP/TP+FN
Example: A TPR of 0.8 means 80% of actual positive cases are correctly identified.
Definition: It measures the proportion of actual negatives that are incorrectly identified as
positive by the model.
Formula: FPR=FP/FP+TN
Example: An FPR of 0.1 means 10% of actual negative cases are incorrectly identified.
7. Threshold
Definition: The value at which the model's prediction is converted into a binary classification. By
adjusting the threshold, different TPR and FPR values can be obtained.
Example: In a binary classification problem, a threshold of 0.5 might mean that predicted
probabilities above 0.5 are classified as positive.
8. ROC Curve
Definition: A graphical plot that illustrates the diagnostic ability of a binary classifier as its
discrimination threshold is varied. It plots TPR against FPR at various threshold settings.
Example: An ROC curve close to the top left corner indicates a better-performing model.
Definition: A single scalar value that summarizes the overall performance of a binary classifier
across all possible thresholds. It is the area under the ROC curve.
Range: 0 to 1, where 1 indicates perfect performance and 0.5 indicates no better than random
guessing.
Formula: Precision=TP/TP+FP
Example: A precision of 0.75 means 75% of the instances classified as positive are actually
positive.
Definition: Another term for True Positive Rate (TPR), measuring the proportion of actual
positives correctly identified.
Example: A recall of 0.8 means 80% of actual positive cases are correctly identified.
12. Specificity
Formula: Specificity=TN/TN+FP
Example: A specificity of 0.9 means 90% of actual negative cases are correctly identified.
AUC is a valuable tool for speculating model performance. An excellent model has its AUC
close to 1, indicating a good separability measure. Consequently, a poor model's AUC leans
closer to 0, showing the worst separability measure. In fact, the proximity to 0 means it
reciprocates the result, predicting the negative class as positive and vice versa, showing 0s as
1s and 1s as 0s. Finally, if the AUC is 0.5, it shows that the model has no class separation
capacity at all.
So, when we have a 0.5<AUC<1 result, there’s a high likelihood that the classifier can
distinguish between the positive class values and the negative class values. That’s because the
classifier can detect more numbers of True Positives and Negatives instead of False
Negatives and Positives.
The Relation Between Sensitivity, Specificity, FPR, and
Threshold
Before we examine the relation between Specificity, FPR, Sensitivity, and Threshold, we
should first cover their definitions in the context of machine learning models. For that, we'll
need a confusion matrix to help us to understand the terms better. Here is an example of
a confusion matrix:
Source
TP stands for True Positive, and TN means True Negative. FP stands for False Positive, and
FN means False Negative.
Sensitivity: Sensitivity, also termed "recall," is the metric that shows a model's ability to predict
the true positives of all available categories. It shows what proportion of the positive class was
classified correctly. For example, when trying to figure out how many people have the flu,
sensitivity, or True Positive Rate, measures the proportion of people who have the flu and were
correctly predicted as having it.
Specificity: The specificity metric Specificity evaluates a model's ability to predict true negatives
of all available categories. It shows what proportion of the negative class was classified correctly.
For example, specificity measures the proportion of people who don't have the flu and were
correctly predicted as not suffering from it in our flu scenario.
FPR: FPR stands for False Positive Rate and shows what proportion of the negative class was
incorrectly classified. This formula shows how we calculate FPR:
FPR= 1 – Specificity
Threshold: The threshold is the specified cut-off point for an observation to be classified as either
0 or 1. Typically, an 0.5 is used as the default threshold, although it’s not always assumed to be
the case.
On the other hand, if we boost the threshold, we will get more negative values, which results
in higher specificity and lower sensitivity.
And since the FPR is 1 – specificity, when we increase TPR, the FPR also increases and vice
versa.
The AUC-ROC (Area Under the Curve - Receiver Operating Characteristic) is a performance
measurement for classification problems at various threshold settings. Here's how it works:
Threshold Variation:
The ROC curve is generated by plotting the True Positive Rate (TPR) against the False Positive Rate
(FPR) at various threshold levels.
By varying the threshold, different pairs of TPR and FPR values are obtained.
True Positive Rate (TPR), also known as Sensitivity or Recall, is plotted on the y-axis. It is the ratio
of true positives to the sum of true positives and false negatives.
False Positive Rate (FPR) is plotted on the x-axis. It is the ratio of false positives to the sum of false
positives and true negatives.
Calculating AUC:
The area under the ROC curve (AUC) quantifies the overall ability of the model to discriminate
between positive and negative classes.
1. Binary Classification Problems: It is primarily used for binary classification tasks with only two
classes.
2. Imbalanced Datasets: AUC-ROC is beneficial when dealing with imbalanced datasets, providing an
aggregate performance measure across all possible classification thresholds.
3. Model Comparison: It is useful for comparing the performance of different models. A higher AUC
value indicates a better-performing model.
4. Threshold-Independent Evaluation: When you need a performance metric that does not depend
on selecting a specific classification threshold.
Closer to Top Left Corner: A curve that hugs the top left corner indicates a high-performing model
with high TPR and low FPR.
Diagonal Line: A curve along the diagonal line (from (0,0) to (1,1)) indicates a model with no
discrimination capability, equivalent to random guessing.
How to Use the AUC - ROC Curve for the Multi-Class Model
We can use the One vs. ALL methodology to plot the N number of AUC ROC Curves for N
number classes when using a multi-class model. One vs. ALL gives us a way to leverage
binary classification. If you have a classification problem with N possible solutions, One vs.
ALL provides us with one binary classifier for each possible outcome.
So, for example, you have three classes named 0, 1, and 2. You will have one ROC for 0
that’s classified against 1 and 2, another ROC for 1, which is classified against 0 and 2, and
finally, the third one of 2 classified against 0 and 1.
We should take a moment and explain the One vs. ALL methodology to better answer the
question “what is a ROC curve?”. This methodology is made up of N separate binary
classifiers. The model runs through the binary classifier sequence during training, training
each to answer a classification question. For instance, if you have a cat picture, you can train
four different recognizers, one seeing the image as a positive example (the cat) and the other
three seeing a negative example (not the cat). It would look like this:
This methodology works well with a small number of total classes. However, as the number
of classes rises, the model becomes increasingly inefficient.
Acelerate your career in AI and ML with the AI and Machine Learning Courses with Purdue University
collaborated with IBM.
Are You Interested in a Career in Machine Learning?
There’s a lot to learn about Machine Learning, as you can tell from this “what is a ROC
curve” article! However, both machine learning and artificial intelligence are the waves of the
future, so it’s worth acquiring skills and knowledge in these fields. Who knows? You could
find yourself in an exciting machine learning career!
If you want a career in machine learning, Simplilearn can help you on your way. The AI and
ML Certification offers students an in-depth overview of machine learning topics. You will
learn to develop algorithms using supervised and unsupervised learning, work with real-time
data, and learn about concepts like regression, classification, and time series modeling. You
will also learn how Python can be used to draw predictions from data. In addition, the
program features 58 hours of applied learning, interactive labs, four hands-on projects, and
mentoring.
And since machine learning and artificial intelligence work together so frequently, check out
Simplilearn’s Artificial Intelligence Engineer Master’s program, and cover all of your bases.
According to Glassdoor, Machine Learning Engineers in the United States enjoy an average
annual base pay of $133,001. Payscale.com reports that Machine Learning Engineers in India
can potentially earn ₹732,566 a year on average.
So visit Simplilearn today, and explore the rich possibilities of a rewarding vocation in the
machine learning field!
FAQs
A perfect AUC-ROC curve reaches the top left corner of the plot, indicating a True Positive
Rate (TPR) of 1 and a False Positive Rate (FPR) of 0 for some threshold. This means the
model perfectly distinguishes between positive and negative classes, resulting in an AUC
value of 1.0.
An AUC value of 0.5 signifies that the model's performance is no better than random
guessing. It indicates that the model cannot distinguish between positive and negative classes,
as the True Positive Rate (TPR) and False Positive Rate (FPR) are equal across all thresholds.
3. How do you compare ROC curves of different models?
To compare ROC curves of different models, plot each model's ROC curve on the same
graph and examine their shapes and positions. The model with the ROC curve closest to the
top left corner and the highest Area Under the Curve (AUC) value generally performs better.
It can be less informative for highly imbalanced datasets, as the True Negative Rate (specificity)
might dominate the curve.
It does not account for the cost of false positives and false negatives, which can be crucial in some
applications.
True Positive Rate (TPR): Also known as sensitivity or recall, it measures the proportion of actual
positives correctly identified.
False Positive Rate (FPR): Measures the proportion of actual negatives incorrectly identified as
positives.
Area Under the Curve (AUC): Summarizes the model's overall performance across all thresholds.
Optimal Threshold: The threshold value maximizes the TPR while minimizing the FPR.