sklearn.metrics.roc_auc_score和sklearn.metrics.roc_curve怎么用,参数都有哪些,举个例子应用一下说明
时间: 2024-06-07 13:11:53 浏览: 25
首先我们需要了解什么是ROC曲线以及AUC,ROC曲线指的是接收者操作特征曲线(Receiver Operating Characteristic Curve),常用于二分类问题中评价模型的性能。ROC曲线的横轴是假正率(False Positive Rate, FPR),纵轴是真正率(True Positive Rate, TPR),而AUC(Area Under Curve)指的是ROC曲线下的面积,用于衡量模型预测准确性的指标。AUC的取值范围在0.5到1之间,AUC越大,模型的性能越好。
接下来我们来看看如何使用`sklearn.metrics`中的`roc_auc_score`和`roc_curve`函数。
`roc_auc_score`函数的参数说明:
```python
sklearn.metrics.roc_auc_score(y_true, y_score, average='macro', sample_weight=None, max_fpr=None)
```
- `y_true`:真实标签,可以是一个一维数组或列表。
- `y_score`:模型预测的得分,与`y_true`的长度相同。
- `average`:指定AUC计算的方式。默认为`'macro'`,即计算每个类别的AUC值并求平均值。如果设为`'micro'`,则将所有数据合并计算一个AUC值。如果设为`None`,则返回所有类别的AUC值。
- `sample_weight`:样本权重,可以是一个一维数组或列表,长度与`y_true`相同。
- `max_fpr`:最大假正率,用于计算部分AUC值。默认为`None`,即计算完整的ROC曲线下的AUC值。
`roc_curve`函数的参数说明:
```python
sklearn.metrics.roc_curve(y_true, y_score, pos_label=None, sample_weight=None, drop_intermediate=True)
```
- `y_true`:真实标签,可以是一个一维数组或列表。
- `y_score`:模型预测的得分,与`y_true`的长度相同。
- `pos_label`:指定正例的标签值。默认为`None`,即将最大的标签设为正例。
- `sample_weight`:样本权重,可以是一个一维数组或列表,长度与`y_true`相同。
- `drop_intermediate`:是否在计算过程中舍弃中间结果。默认为`True`,即只返回阈值和对应的FPR和TPR值,且只包括第一个和最后一个元素。
下面给出一个例子,使用`sklearn.metrics`中的`roc_auc_score`和`roc_curve`函数来评价一个二分类模型的性能:
```python
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score, roc_curve
import matplotlib.pyplot as plt
# 生成随机数据
X, y = make_classification(n_samples=1000, n_features=10, n_classes=2, random_state=1)
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1)
# 训练模型
model = LogisticRegression()
model.fit(X_train, y_train)
# 预测测试集
y_pred = model.predict(X_test)
y_score = model.predict_proba(X_test)[:, 1]
# 计算AUC
auc = roc_auc_score(y_test, y_score)
print('AUC:', auc)
# 计算ROC曲线
fpr, tpr, thresholds = roc_curve(y_test, y_score)
# 绘制ROC曲线
plt.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % 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 example')
plt.legend(loc="lower right")
plt.show()
```
在上面的例子中,我们使用`make_classification`函数生成了1000个样本,10个特征和2个类别。然后使用`train_test_split`函数将数据划分为训练集和测试集。接着训练一个逻辑回归模型,并预测测试集的标签和得分。最后使用`roc_auc_score`函数计算AUC值,使用`roc_curve`函数计算ROC曲线的FPR和TPR,并绘制ROC曲线图。
运行上述代码,可以得到以下输出:
```
AUC: 0.9311666666666667
```
同时还会出现一个ROC曲线的图像。可以看到该模型的AUC值较高,ROC曲线也比较靠近左上角,说明该模型的预测准确性较好。
相关推荐
![mhtml](https://img-home.csdnimg.cn/images/20210720083646.png)
![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_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)
![](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)