如何绘制ROC-AUC图而不使用scikit-learn



我有以下列表,其中包含多个(TP, FP, FN)元组:

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

每个元组表示单个图像的分数。这意味着我有7张图像,我已经计算了目标检测任务的分数。现在我使用以下函数计算每个图像(元组)的精度和召回率:

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)

函数返回一个包含两个列表的元组。第一个是每个图像的精度值,第二个是每个图像的召回值。现在我的主要目标是仅使用matplotlib绘制ROC和AUC曲线。请注意,我不想使用scikit-learn库。

您可以简单地使用matplotlib.pyplot.plot方法。例如:

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

,然后将其用作plot_PR(precision_bundle, recall_bundle)

注意:这里我还为随机分类器添加了虚线,并且可以保存图形,以防您想要

最新更新