用catboost画ROC曲线
时间: 2023-10-24 13:06:06 浏览: 105
ROC.rar_ROC曲线_roc
5星 · 资源好评率100%
首先,需要安装CatBoost库:
```
!pip install catboost
```
然后,我们可以使用以下代码绘制ROC曲线:
```
import numpy as np
from catboost import CatBoostClassifier, Pool
from sklearn.datasets import make_classification
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=20, n_classes=2, random_state=42)
# 将数据集拆分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建CatBoost分类器
clf = CatBoostClassifier(iterations=100, learning_rate=0.1, depth=2, loss_function='Logloss')
# 将训练数据和标签转换为Pool对象
train_data = Pool(data=X_train, label=y_train)
# 训练分类器
clf.fit(train_data)
# 使用分类器进行预测
y_pred = clf.predict_proba(X_test)[:, 1]
# 计算ROC曲线和AUC
fpr, tpr, thresholds = roc_curve(y_test, y_pred)
roc_auc = roc_auc_score(y_test, y_pred)
# 绘制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.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.show()
```
这将生成一个ROC曲线图,如下所示:
![ROC curve](https://i.imgur.com/1vPI8V7.png)
阅读全文