from matplotlib import pyplot as plt
%matplotlib inline
def plot_precision_recall_vs_threshold (precisions, recalls, thresholds) :
#precision_recall_curve函数的源码中,precision : array, shape = [n_thresholds + 1],the last element is 1
#之所以要通过precisions[: -1 ]使准确率数组中的最后一个值删掉,是因为,随着阈值的不断提高,对反例的识别率不断提高,最终的准确率趋近于1
#但此时的阈值可能太大导致阈值数组的范围太大不好表示,所以干脆就省略了这个阈值,直接令准确率数组的最后一个值等于1
plt.plot(thresholds, precisions[: -1 ], "b--" , label= "Precision" )
#recalls[: -1 ]意义同上
plt.plot(thresholds, recalls[: -1 ], "g-" , label= "Recall" )
plt.xlabel( "Threshold" )
plt.legend(loc= "upper left" )
plt.ylim([ 0 , 1 ])
plt.show()
from sklearn.metrics import roc_curve
#绘制图像
def plot_roc_curve (y_true,pre, label=None) :
fpr, tpr, thresholds = roc_curve(y_true,pre)
diff=tpr-fpr #array
index=lis