如何用python画roc图像
时间: 2024-06-04 21:01:59 浏览: 13
要绘制ROC曲线,可以使用Python中的scikit-learn库和matplotlib库。以下是一个简单的例子:
```python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc
# 随机生成一些测试数据
y_true = np.array([0, 0, 1, 1, 1, 0, 1, 1])
y_scores = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8])
# 计算ROC曲线的假正率(FPR)、真正率(TPR)和阈值
fpr, tpr, thresholds = roc_curve(y_true, y_scores)
# 计算AUC
roc_auc = auc(fpr, tpr)
# 绘制ROC曲线
plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic')
plt.legend(loc="lower right")
plt.show()
```
该代码将生成一个ROC曲线的图像,其中橙色的曲线表示ROC曲线,蓝色的曲线表示随机猜测的曲线。输出图像应该类似于下面的图像:
![ROC Curve](https://cdn.jsdelivr.net/gh/YellowTulipShow/yellowtulipshow.github.io/img/roc-curve.png)