计算以下代码的auprcimport numpy as np from matplotlib import pyplot as plt from sklearn.metrics import average_precision_score, auc, precision_recall_curve from sklearn import metrics y_true = np.array([0, 2, 3, 0, 4, 1]) pre = np.array([0, 2, 3, 0, 3, 1])
时间: 2023-12-08 11:06:16 浏览: 71
首先,计算预测结果的精度和召回率。
```python
tp = sum((pre == 1) & (y_true == 1))
fp = sum((pre == 1) & (y_true != 1))
fn = sum((pre != 1) & (y_true == 1))
precision = tp / (tp + fp)
recall = tp / (tp + fn)
```
接下来,计算每个可能的阈值下的精度和召回率,以绘制 P-R 曲线。
```python
precision_curve, recall_curve, thresholds = precision_recall_curve(y_true, pre)
auprc = auc(recall_curve, precision_curve)
plt.step(recall_curve, precision_curve, color='b', alpha=0.2, where='post')
plt.fill_between(recall_curve, precision_curve, step='post', alpha=0.2, color='b')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.title('Precision-Recall curve: AUPRC={0:0.2f}'.format(auprc))
plt.show()
```
根据代码得到,该模型的 AUPRC 约为 0.71。
阅读全文