ROC Curve Python

The definitive ROC Curve in Python code

Learn the ROC Curve Python code: The ROC Curve and the AUC are one of the standard ways to calculate the performance of a classification Machine Learning problem. You can check our the what ROC curve is in this article: The ROC Curve explained.

In this short code snippet we teach you how to implement the ROC Curve Python code that we think is best and most simple to understand. Also, we will explain all the parameters that the function uses so that you have the detail of everything that is going on.

To run this code you need to have previously separated the test and train data (you should never plot a ROC or calculate any other evaluation metric like the Confusion Matrix on Training data), and calculated the probability predictions for your model on the test data.

Lets see the ROC Code and after we will explain the parameters:

# Imports
import matplotlib.pyplot as plt
from sklearn.metrics import roc_auc_score, roc_curve

# ROC Function
def plot_roc_curve(Y_test, model_probs):

	random_probs = [0 for _ in range(len(Y_test))]
	# calculate AUC
	model_auc = roc_auc_score(Y_test, model_probs)
	# summarize score
	print('Model: ROC AUC=%.3f' % (model_auc))
	# calculate ROC Curve
		# For the Random Model
	random_fpr, random_tpr, _ = roc_curve(Y_test, random_probs)
		# For the actual model
	model_fpr, model_tpr, _ = roc_curve(Y_test, model_probs)
	# Plot the roc curve for the model and the random model line
	plt.plot(random_fpr, random_tpr, linestyle='--', label='Random')
	plt.plot(model_fpr, model_tpr, marker='.', label='Model')
	# Create labels for the axis
	plt.xlabel('False Positive Rate')
	plt.ylabel('True Positive Rate')
	# show the legend
	plt.legend()
	# show the plot
	plt.show()

This code will calculate the ROC and the AUC for our model with two parameters:

  • Y_test: array with the labels of the test data
  • model_probs: array with the probability predictions made by the model. Important: These predictions are not the binary 0 or 1s, but the probabilities calculated using the predict_proba sklearn function (this example is for an SVM but most models have it) or other similar ones. model_probs is an array of probabilities like [0.82, 0.12, 0.34,…] and so on.

It is also important to know that the Y_test and model_probs arrays must have the same length for the code to work.

After you execute the function like so: plot_roc_curve(test_labels, predictions), you will get an image like the following, and a print out with the AUC Score and the ROC Curve Python plot:

Model: ROC AUC=0.835
roc curve python

That is it, hope you make good use of this quick code snippet for the ROC Curve in Python and its parameters! Follow us on Twitter here! Also, if you have any doubts or comments, please feel free to contact us at howtolearnmachinelearning@gmail.com.
Spread the love and have a fantastic day 🙂

The Hundred-Page Machine Learning Book
  • Burkov, Andriy (Author)
  • English (Publication Language)
  • 160 Pages - 01/13/2019 (Publication Date) - Andriy Burkov (Publisher)

Check out our reviews of awesome Machine Learning books that will teach you all of the theory behind concepts like the Confusion Matrix and the ROC Curve:

Book reviews: