# Python Code to plot Receiver Operating Characteristic (ROC) Curve

A Receiver Operating Characteristic curve (ROC curve) represents the performance of a binary classifier at different discrimination thresholds. It is created by plotting the true positive rate (TPR) against the false positive rate (FPR) at various threshold values.

In the below code, I am using the matplotlib library and various functions of the sklearn library to plot the ROC curve. The roc_curve() function computes FPR, TPR, and thresholds using true binary labels and the target scores, which can either be probability estimates of the positive class, confidence values, or binary decisions. In this code, I have randomly generated binary labels and predicted probabilities using the random module of Python.

``````import numpy as np
import matplotlib.pyplot as plt
import random
from sklearn.metrics import auc, roc_auc_score, roc_curve

def plot_roc_curve(labels, predictions):
"""
Plot ROC curve
"""
fpr, tpr, threshold = roc_curve(labels, predictions)
roc_auc = auc(fpr, tpr)
plt.title('ROC Curve')
plt.plot(fpr, tpr, 'b', label='AUC = %0.3f' % roc_auc)
plt.legend(loc='lower right')
plt.plot([0, 1], [0, 1], 'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.savefig('roc.png')
plt.show()

if __name__ == "__main__":
n = 100  # sample size
k = 30  # number of 1s
y_true = np.zeros(n, dtype=int)
idx = random.sample(range(n), k)
y_true[idx] = 1
# print(y_true)
y_scores = [random.random() for i in range(n)]
# print(y_scores)
plot_roc_curve(y_true, y_scores)
``````

The above code will generate a ROC curve that should look like the following plot:

This site uses Akismet to reduce spam. Learn how your comment data is processed.