How to plot ROC Curve in Python

Hits: 618

How to plot ROC Curve in Python

When building a machine learning model for a binary classification problem, it’s important to evaluate its performance using various metrics. One way to do this is by plotting an ROC (Receiver Operating Characteristic) curve.

An ROC curve is a graph that shows the relationship between the model’s true positive rate (sensitivity) and its false positive rate (1-specificity) at different classification thresholds. It helps to understand how the model’s performance changes as the threshold is adjusted.

In Python, the library scikit-learn provides an easy way to plot an ROC curve using the function roc_curve().

The first step is to import the library and load the dataset into a pandas dataframe. Then, split the data into training and testing sets, and train the model on the training set.

After that, use the roc_curve() function, which takes the actual class labels and the predicted class probabilities as inputs. The function returns three arrays: the false positive rate, true positive rate, and thresholds.

You can use the matplotlib library to plot the ROC curve, by plotting the false positive rate against the true positive rate.

Additionally, you can use roc_auc_score() function to calculate the area under the ROC curve which is a measure of the model’s performance, higher the area under the curve, better the model.

In summary, an ROC curve is a powerful tool for evaluating the performance of a machine learning model for a binary classification problem. By using the roc_curve() function in scikit-learn, it’s easy to plot an ROC curve in Python, making it a valuable tool for data scientists and machine learning practitioners.


In this Learn through Codes example, you will learn: How to plot ROC Curve in Python.


Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners