如果我遗漏了一些明显的东西,我表示歉意。在过去的几天中,我非常享受使用R界面使用h2o的乐趣。我想通过绘制ROC来评估我的模型,例如一个随机森林。该文档似乎暗示有一种简单的方法可以做到这一点:
解释DRF模型
- 默认情况下,显示以下输出:
- 模型参数(隐藏)
- 评分历史记录(树木数量与培训MSE)
- ROC曲线图(TPR与FPR)
- 重要性变化图...
我也看到在python中可以在这里应用roc函数。但是我似乎无法找到在R接口中执行相同操作的方法。目前,我正在使用h2o.cross_validation_holdout_predictions从模型中提取预测,然后使用R中的pROC软件包绘制ROC。但是我希望能够直接从H2O模型对象,或者也许从H2OModelMetrics对象执行此操作。
非常感谢!
小智 5
一个简单的解决方案是使用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')
Run Code Online (Sandbox Code Playgroud)
这将给我们一个情节:
但是很难自定义,尤其是更改线型,因为该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')
Run Code Online (Sandbox Code Playgroud)
那么ROC曲线为: