前言
關于ROC和PR曲線的介紹請參考:
機器學習:準確率(Precision)、召回率(Recall)、F值(F-Measure)、ROC曲線、PR曲線文章來源:http://www.zghlxwxcb.cn/news/detail-660193.html
參考:
Python下使用sklearn繪制ROC曲線(超詳細)
Python繪圖|Python繪制ROC曲線和PR曲線文章來源地址http://www.zghlxwxcb.cn/news/detail-660193.html
源碼
from sklearn.metrics import roc_curve, auc
from sklearn.metrics import precision_recall_curve, average_precision_score
import matplotlib.pyplot as plt
def draw_roc(labels, preds):
'''
labels: list
preds: list
'''
fpr, tpr, thersholds = roc_curve(labels, preds, pos_label=1) # pos_label指定哪個標簽為正樣本
roc_auc = auc(fpr, tpr) # 計算ROC曲線下面積
plt.figure(figsize=(10,7), dpi=300)
plt.plot(fpr, tpr, '-', color='r', label='ROC (area=%.6f)' % (roc_auc), lw=2)
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC Curve')
plt.legend(loc="lower right")
# plt.show()
plt.savefig('./roc.png', dpi=300, bbox_inches='tight')
def draw_pr(labels, preds):
'''
labels: list
preds: list
'''
precision, recall, thersholds = precision_recall_curve(labels, preds, pos_label=1) # pos_label指定哪個標簽為正樣本
area = average_precision_score(labels, preds, pos_label=1) # 計算PR曲線下面積
plt.figure(figsize=(10,7), dpi=300)
plt.plot(recall, precision, '-', color='r', label='PR (area=%.6f)' % (area), lw=2)
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('PR Curve')
plt.legend(loc="lower left")
# plt.show()
plt.savefig('./pr.png', dpi=300, bbox_inches='tight')
到了這里,關于Python包sklearn畫ROC曲線和PR曲線的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!