2

I have the following list containing multiple tuples of (TP, FP, FN):

[(12, 0, 0), (5, 2, 2), (10, 0, 1), (7, 1, 1), (13, 0, 0), (7, 2, 2), (11, 0, 2)]

each tuple represents the scores for a single image. This means I have 7 images and I have calculated the scores for a object detection task. Now I calculate precision and recall for each image(tuple) using the following function:

def calculate_recall_precision(data):
    precisions_bundle = []
    recalls_bundle = []

    for tp, fp, fn in data:
        precision = tp / (tp + fp)
        recall = tp / (tp + fn)
        precisions_bundle.append(precision)
        recalls_bundle.append(recall)

    return (precisions_bundle, recalls_bundle)

This function returns a tuple which contains two lists. The first one is precision values for each image and the second one is recall values for each image. Now my main goal is to plot ROC and AUC curves using only matplotlib. Please note that I do not want to use scikit-learn library.

RZAMarefat
  • 133
  • 8

1 Answers1

0

You can simply use matplotlib.pyplot.plot method. For example:

import numpy as np
import matplotlib.pyplot as plt

def plot_PR(precision_bundle, recall_bundle, save_path:Path=None):
    line = plt.plot(recall_bundle, precision_bundle, linewidth=2, markersize=6)
    line = plt.title('Precision/Recall curve', size =18, weight='bold')
    line = plt.ylabel('Precision', size=15)
    line = plt.xlabel('Recall', size=15 )
    
    random_classifier_line_x = np.linspace(0, 1, 10)
    random_classifier_line_y = np.linspace(1, 0, 10)
    _ = plt.plot(random_classifier_line_x, random_classifier_line_y, color='firebrick', linestyle='--')
    if save_path:
        outname = save_path / 'PR_curve_thresh_opt.png'
        _ = plt.savefig(outname, dpi = 100, bbox_inches='tight' )
    return line

and then just use it as plot_PR(precision_bundle, recall_bundle).

Note: here I also added a dashed line for a random classifier and the possibility to save the figure in case you want to

Luca Clissa
  • 810
  • 2
  • 7
  • 27