sklearn中ROC
时间: 2024-01-15 12:10:54 浏览: 21
ROC(Receiver Operating Characteristic)曲线是一种用于评估分类模型性能的工具,常用于二分类问题。在scikit-learn中,可以使用`sklearn.metrics.roc_curve`函数来计算ROC曲线的真正率(True Positive Rate,也称为召回率)和假正率(False Positive Rate)。
下面是一个使用`sklearn.metrics.roc_curve`函数计算ROC曲线的示例代码:
```python
from sklearn import metrics
import matplotlib.pyplot as plt
# 计算模型预测结果的概率值
y_pred_proba = model.predict_proba(X_test)[:, 1]
# 计算真正率和假正率
fpr, tpr, thresholds = metrics.roc_curve(y_true, y_pred_proba)
# 计算AUC(Area Under Curve)
auc = metrics.auc(fpr, tpr)
# 绘制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')
plt.legend(loc="lower right")
plt.show()
```
以上代码中,`y_true`是真实的类别标签,`y_pred_proba`是模型预测的概率值。`metrics.roc_curve`函数返回三个数组:fpr(假正率)、tpr(真正率)和阈值(thresholds)。通过绘制fpr和tpr即可得到ROC曲线,AUC则可以通过`metrics.auc`函数计算得到。
希望能解决你的问题!如果你还有其他问题,请随时提问。