pr曲线绘制python
时间: 2023-10-29 19:59:27 浏览: 38
PR曲线是用于评估二分类模型的性能的一种可视化工具,它是Precision-Recall曲线的简称。在Python中,可以使用Scikit-learn库中的函数来绘制PR曲线。以下是一个简单的例子:
```python
from sklearn.metrics import precision_recall_curve
import matplotlib.pyplot as plt
# 真实标签和预测概率
y_true = [1, 0, 0, 1, 1, 0, 1]
y_scores = [0.9, 0.8, 0.6, 0.7, 0.7, 0.5, 0.4]
# 计算precision和recall
precision, recall, thresholds = precision_recall_curve(y_true, y_scores)
# 绘制PR曲线
plt.plot(recall, precision)
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('Precision-Recall Curve')
plt.show()
```
在这个例子中,我们首先定义了真实标签和模型预测的概率。然后使用`precision_recall_curve`函数计算出所有可能的阈值下的precision和recall。最后,我们使用Matplotlib库绘制PR曲线。
运行上述代码后,会得到一个类似下图的PR曲线:
![PR曲线](https://img-blog.csdnimg.cn/2021041811141417.png)