要使用Python绘制PR(Precision-Recall)曲线,你可以按照以下步骤进行:
1. 导入必要的库:
```python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import precision_recall_curve
2. 准备数据:
你需要有两组数据,`y_true` 表示真实标签,`probas_pred` 表示模型预测为正类的概率。
```python
示例数据
y_true = np.array([0, 1, 2, 2, 0])
probas_pred = np.array([0.1, 0.4, 0.35, 0.8, 0.95])
3. 计算PR曲线所需的值:
```python
precision, recall, thresholds = precision_recall_curve(y_true, probas_pred)
4. 绘制PR曲线:
```python
plt.figure()
plt.plot(recall, precision, marker='.')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('PR Curve')
plt.show()
将上述代码整合到一起,完整的示例代码如下:
```python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import precision_recall_curve
示例数据
y_true = np.array([0, 1, 2, 2, 0])
probas_pred = np.array([0.1, 0.4, 0.35, 0.8, 0.95])
计算PR曲线所需的值
precision, recall, thresholds = precision_recall_curve(y_true, probas_pred)
绘制PR曲线
plt.figure()
plt.plot(recall, precision, marker='.')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('PR Curve')
plt.show()
运行这段代码,你将得到一个展示PR曲线的图形窗口。