python画出AUC曲线

以load_breast_cancer数据集为例,模型细节不重要,重点是画AUC的代码。直接上代码:

from sklearn.datasets import load_breast_cancer
from sklearn import metrics
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import pylab as plt
import warnings;warnings.filterwarnings('ignore')
dataset = load_breast_cancer()
data = dataset.data
target = dataset.target
X_train,X_test,y_train,y_test = train_test_split(data,target,test_size=0.2)
rf = RandomForestClassifier(n_estimators=5)
rf.fit(X_train,y_train)
pred = rf.predict_proba(X_test)[:,1]
#############画图部分
fpr, tpr, threshold = metrics.roc_curve(y_test, pred)
roc_auc = metrics.auc(fpr, tpr)
plt.figure(figsize=(6,6))
plt.title('Validation ROC')
plt.plot(fpr, tpr, 'b', label = 'Val AUC = %0.3f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()

在这里插入图片描述

  • 10
    点赞
  • 87
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 下面是一个简单的 Python 代码示例,用于绘制 AUC 曲线。 ```python import numpy as np import matplotlib.pyplot as plt from sklearn.metrics import roc_curve, auc # 生成随机数据 y_true = np.random.randint(2, size=100) y_scores = np.random.rand(100) # 计算 AUC fpr, tpr, thresholds = roc_curve(y_true, y_scores) roc_auc = auc(fpr, tpr) # 绘制 ROC 曲线 plt.figure() plt.plot(fpr, tpr, color='darkorange', label='ROC curve (area = %0.2f)' % roc_auc) plt.plot([0, 1], [0, 1], color='navy', 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() ``` 这段代码使用了 Scikit-Learn 库中的 `roc_curve` 和 `auc` 函数来计算 AUC 值和 ROC 曲线上的点,然后使用 Matplotlib 库将 ROC 曲线绘制出来。 ### 回答2: Python可以使用多个库来画出AUC曲线,其中最常用的是`sklearn.metrics`库。下面是使用PythonAUC曲线的步骤: 1. 导入所需的库: ```python import matplotlib.pyplot as plt from sklearn import metrics ``` 2. 定义真实标签和预测概率: 假设我们有一个真实的标签列表`y_true`和对应的模型预测的概率列表`y_score`。 3. 计算FPR和TPR以及阈值: ```python fpr, tpr, thresholds = metrics.roc_curve(y_true, y_score) ``` 此函数将根据真实标签和预测概率计算出分类器的FPR(False Positive Rate)、TPR(True Positive Rate)和阈值。 4. 计算AUC: ```python roc_auc = metrics.auc(fpr, tpr) print('AUC值为: ', roc_auc) ``` `auc`函数将根据FPR和TPR计算出AUC值。 5. 画出AUC曲线: ```python plt.figure() 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.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Receiver Operating Characteristic') plt.legend(loc="lower right") plt.show() ``` 使用matplotlib库的`plot`函数画出ROC曲线,并在图中显示AUC值。 上述步骤就是使用PythonAUC曲线的简单步骤。在实际使用中,可以根据需求对图形进行优化和美化。 ### 回答3: 使用Python可以通过以下步骤绘制AUC曲线: 1. 导入所需的库:导入matplotlib库用于绘图,以及numpy和sklearn库用于计算AUC。 2. 获取概率预测值和真实标签:从模型中获取样本的概率预测值和真实标签。 3. 计算FPR和TPR:使用sklearn库中的roc_curve函数计算真正例率(True Positive Rate,TPR)和假正例率(False Positive Rate,FPR)。 4. 计算AUC:使用sklearn库中的auc函数计算AUC值。 5. 绘制AUC曲线:使用matplotlib库的plot函数绘制FPR和TPR的关系图。 下面是一个简单的示例代码: ```python import numpy as np from sklearn.metrics import roc_curve, auc import matplotlib.pyplot as plt # 模拟概率预测值和真实标签 y_proba = np.array([0.1, 0.3, 0.4, 0.7, 0.9, 0.6, 0.2, 0.8]) y_true = np.array([0, 0, 1, 1, 1, 0, 0, 1]) # 计算FPR和TPR fpr, tpr, thresholds = roc_curve(y_true, y_proba) # 计算AUC auc_score = auc(fpr, tpr) # 绘制AUC曲线 plt.title('Receiver Operating Characteristic') plt.plot(fpr, tpr, 'b', label='AUC = %0.2f' % auc_score) plt.legend(loc='lower right') plt.plot([0, 1], [0, 1], 'r--') plt.xlim([-0.1, 1.1]) plt.ylim([-0.1, 1.1]) plt.ylabel('True Positive Rate') plt.xlabel('False Positive Rate') plt.show() ``` 以上代码首先模拟了一组概率预测值和真实标签,接着通过`roc_curve`函数计算出FPR和TPR的值,然后利用`auc`函数计算出AUC值。最后使用`plot`函数绘制AUC曲线,其中标签中包含了AUC值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值