r - 如何在R中直接绘制h2o模型对象的ROC

标签 r h2o roc

如果我遗漏了一些明显的东西,我深表歉意。在过去的几天里,我非常喜欢使用 R 界面与 h2o 一起工作。我想通过绘制 ROC 来评估我的模型,例如随机森林。该文档似乎表明有一种简单的方法可以做到这一点:

Interpreting a DRF Model

  • By default, the following output displays:
  • Model parameters (hidden)
  • A graph of the scoring history (number of trees vs. training MSE)
  • A graph of the ROC curve (TPR vs. FPR)
  • A graph of the variable importances ...

我还看到在Python中你可以应用roc函数here 。但我似乎无法找到在 R 界面中执行相同操作的方法。目前,我正在使用 h2o.cross_validation_holdout_predictions 从模型中提取预测,然后使用 R 中的 pROC 包来绘制 ROC。但我希望能够直接从 H2O 模型对象或 H2OModelMetrics 对象来完成此操作。

非常感谢!

最佳答案

一个简单的解决方案是使用 plot() 通用函数来绘制 H2OMetrics 对象:

logit_fit <- h2o.glm(colnames(training)[-1],'y',training_frame =
    training.hex,validation_frame=validation.hex,family = 'binomial')
plot(h2o.performance(logit_fit),valid=T),type='roc')

这将为我们提供一个情节:

enter image description here

但是很难自定义,尤其是更改线型,因为 type 参数已被视为“roc”。另外,我还没有找到一种方法将多个模型的 ROC 曲线一起绘制在一个图上。我想出了一种从 H2OMetrics 对象中提取真阳性率和假阳性率的方法,并使用 ggplot2 自己在一个图上绘制 ROC 曲线。这是示例代码(使用了大量 tidyverse 语法):

# for example I have 4 H2OModels
list(logit_fit,dt_fit,rf_fit,xgb_fit) %>% 
  # map a function to each element in the list
  map(function(x) x %>% h2o.performance(valid=T) %>% 
        # from all these 'paths' in the object
        .@metrics %>% .$thresholds_and_metric_scores %>% 
        # extracting true positive rate and false positive rate
        .[c('tpr','fpr')] %>% 
        # add (0,0) and (1,1) for the start and end point of ROC curve
        add_row(tpr=0,fpr=0,.before=T) %>% 
        add_row(tpr=0,fpr=0,.before=F)) %>% 
  # add a column of model name for future grouping in ggplot2
  map2(c('Logistic Regression','Decision Tree','Random Forest','Gradient Boosting'),
        function(x,y) x %>% add_column(model=y)) %>% 
  # reduce four data.frame to one
  reduce(rbind) %>% 
  # plot fpr and tpr, map model to color as grouping
  ggplot(aes(fpr,tpr,col=model))+
  geom_line()+
  geom_segment(aes(x=0,y=0,xend = 1, yend = 1),linetype = 2,col='grey')+
  xlab('False Positive Rate')+
  ylab('True Positive Rate')+
  ggtitle('ROC Curve for Four Models')

那么ROC曲线为:

enter image description here

关于r - 如何在R中直接绘制h2o模型对象的ROC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44034944/

相关文章:

r - 强制 HCPC 对象到 hclust 以使用库(ape)

r - 独特的字符串组合

r - h2o.importFile() 不在 R 中导入完整的数据帧

java - 如何重命名 h2o POJO?

tensorflow - Keras,训练期间验证集上的 auc 与 sklearn auc 不匹配

r - 在 R 中,如何反转 split()

r - 在给定其他变量的情况下显示一个变量的存在百分比

python - 找到 H2O 框架中的小时

python - 使用交叉验证 (CV) 计算 scikit-learn 多类 ROC 曲线

r - 具有自定义截止值的 pROC R 包?