基于python绘制ROC曲线,直接附代码:

from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import train_test_split
##划分数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, test_size=0.1, random_state=0)
y_score = clf.fit(X_train, y_train).predict_proba(X_test) #随机森林
fpr, tpr, thresholds = roc_curve(y_test, y_score[:,1]);
roc_auc = auc(fpr, tpr) 

##确定最佳阈值

right_index = (tpr + (1 - fpr) - 1)
yuzhi = max(right_index)
index = right_index.index(max(right_index))
tpr_val = tpr(index)
fpr_val = fpr(index)
## 绘制roc曲线图
plt.subplots(figsize=(7,5.5));
plt.plot(fpr, tpr, color='darkorange',
         lw=2, label='ROC curve (area = %0.2f)' % roc_auc);
plt.plot([0, 1], [0, 1], color='navy', lw=lw, 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('ROC Curve');
plt.legend(loc="lower right");
plt.show()
基于python绘制ROC曲线

相关文章:

  • 2021-07-05
  • 2021-12-25
  • 2022-12-23
  • 2021-08-22
  • 2023-03-09
猜你喜欢
  • 2022-01-10
  • 2022-12-23
  • 2022-01-01
  • 2022-01-07
  • 2021-08-18
相关资源
相似解决方案