roc曲线python代码实现
时间: 2023-06-03 14:04:45 浏览: 73
以下是一个简单的ROC曲线的Python代码实现:
```python
import numpy as np
import matplotlib.pyplot as plt
# 随机生成一些样本标签和预测概率值
y_true = np.random.randint(2, size=100)
y_scores = np.random.rand(100)
# 计算真正例率(True Positive Rate)和假正例率(False Positive Rate)
fpr, tpr, thresholds = metrics.roc_curve(y_true, y_scores)
# 计算ROC曲线下的面积(Area Under the Curve,AUC)
roc_auc = metrics.auc(fpr, tpr)
# 绘制ROC曲线
plt.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], 'k--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.0])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic')
plt.legend(loc='lower right')
plt.show()
```
需要导入`numpy`、`matplotlib`和`sklearn.metrics`库。在代码中,先随机生成一些样本标签和预测概率值,然后调用`sklearn.metrics`库中的`roc_curve`函数计算出真正例率(TPR)和假正例率(FPR),再用`auc`函数计算出AUC,最后利用`matplotlib`库绘制ROC曲线。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)