正样本的PR曲线比较好画
import matplotlib.pyplot as plt
from sklearn.metrics import precision_recall_curve
metrics.precision_recall_curve(test_y, test_pred_prob, pos_label=None, sample_weight=None)
precision, recall, thresholds = precision_recall_curve(test_y, test_pred_prob)
#print(precision)
#print(recall)
#print(thresholds)
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.plot(recall,precision)
plt.grid() # 生成网格
plt.figure("P-R Curve")
plt.show()
对于负样本,可以将正负样本的标签、概率值取1的相反数,即可:
import matplotlib.pyplot as plt
from sklearn.metrics import precision_recall_curve
metrics.precision_recall_curve(test_y, test_pred_prob, pos_label=None, sample_weight=None)
precision, recall, thresholds = precision_recall_curve(1-np.array(test_y), 1-np.array(test_pred_prob))
#print(precision)
#print(recall)
#print(thresholds)
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.grid() # 生成网格
plt.plot(recall,precision)
plt.figure("P-R Curve")
plt.show()