h2o.performance的预测结果与h2o.predict有所不同?

3

如果这个问题已经在其他地方回答了,我很抱歉,但我找不到任何信息。

我正在R中使用最新版本的h2o。 我使用h2o.grid(用于参数调整)创建了一个随机森林模型,并将其称为'my_rf'

我的步骤如下:

  1. 训练带参数调整和交叉验证(nfolds = 5)的“randomForests”网格
  2. 获取排序后的模型网格(按AUC),并设置my_rf = 最佳模型
  3. 使用h2o性能(my_rf,test)评估测试集上的AUC,准确度等
  4. 使用h2o.predict在测试集上进行预测并导出结果

我用于h2o.performance的确切命令是:

h2o.performance(my_rf, newdata = as.h2o(test))

...给我一个混淆矩阵,从中可以计算准确率(还可以给出AUC、最大F1值等)。
我认为,使用

结束第一句话是有误的。
h2o.predict(my_rf, newdata = as.h2o(test)) 

我可以复制h2o.performance中的混淆矩阵。但是准确率有所不同,实际上比原来差了3%。

有人能解释一下为什么会这样吗?

此外,在h2o.performance中有没有返回构成混淆矩阵的预测结果的方法?

编辑:以下是相关代码:

library(mlbench)
data(Sonar)
head(Sonar)

mainset <- Sonar
mainset$Class <- ifelse(mainset$Class == "M", 0,1)          #binarize
mainset$Class <- as.factor(mainset$Class)

response <- "Class"
predictors <- setdiff(names(mainset), c(response, "name"))

# split into training and test set

library(caTools)
set.seed(123)
split = sample.split(mainset[,61], SplitRatio = 0.75)
train = subset(mainset, split == TRUE)
test =  subset(mainset, split == FALSE)

# connect to h2o

Sys.unsetenv("http_proxy")
Sys.setenv(JAVA_HOME='C:\\Program Files (x86)\\Java\\jre7')                #set JAVA home for 32 bit
library(h2o)
h2o.init(nthread = -1)

# stacked ensembles

nfolds <- 5
ntrees_opts <- c(20:500)             
max_depth_opts <- c(4,8,12,16,20)
sample_rate_opts <- seq(0.3,1,0.05)
col_sample_rate_opts <- seq(0.3,1,0.05)

rf_hypers <- list(ntrees = ntrees_opts, max_depth = max_depth_opts,
                  sample_rate = sample_rate_opts,
                  col_sample_rate_per_tree = col_sample_rate_opts)

search_criteria <- list(strategy = 'RandomDiscrete', max_runtime_secs = 240, max_models = 15,
stopping_metric = "AUTO", stopping_tolerance = 0.00001, stopping_rounds = 5,seed = 1)

my_rf <- h2o.grid("randomForest", grid_id = "rf_grid", x = predictors, y = response,
                                                                training_frame = as.h2o(train),
                                                                nfolds = 5,
                                                                fold_assignment = "Modulo",
                                                                keep_cross_validation_predictions = TRUE,
                                                                hyper_params = rf_hypers,
                                                                search_criteria = search_criteria)

get_grid_rf <- h2o.getGrid(grid_id = "rf_grid", sort_by = "auc", decreasing = TRUE)                         # get grid of models built
my_rf <- h2o.getModel(get_grid_rf@model_ids[[1]])
perf_rf <- h2o.performance(my_rf, newdata = as.h2o(test))

pred <- h2o.predict(my_rf, newdata = as.h2o(test))
pred <- as.vectpr(pred$predict)

cm <- table(test[,61], pred)
print(cm)

你能在这里分享一下脚本吗? - AvkashChauhan
当然 - 我会在上面添加相关的代码。谢谢。 - Joshua Olney
请您提供一个可重现的例子,包括数据。 - Erin LeDell
嗨@ErinLeDell-抱歉回复晚了。我已经更新了上面的代码,这样你应该能够使用Sonar数据集进行再现了吧?你应该会发现perf_rf中的混淆矩阵与我在最后创建的表cm不匹配。谢谢。 - Joshua Olney
3个回答

3

很可能,函数h2o.performance使用F1阈值来设置yes和no。如果您获取预测结果并将表格分离为基于模型“F1阈值”值的yes / no,则会发现数字几乎匹配。我相信这是您在h2o.performance和h2o.predict结果之间看到差异的主要原因。


谢谢Avkash - 你的意思是使用h2o.ifelse(pred$p1 > f1,1,0)这样的东西吗?这是最大的f1阈值吗? - Joshua Olney
没错。我进行了快速测试,最大f1值和数量几乎匹配。 - AvkashChauhan

0

下面解释了performance()predict()之间的区别。这是直接从H2O的帮助页面中获取的 - http://docs.h2o.ai/h2o/latest-stable/h2o-docs/performance-and-prediction.html#prediction

预测阈值

对于分类问题,当运行h2o.predict()或.predict()时,预测阈值如下选择:

  • 如果您只使用训练数据训练模型,则使用训练数据模型指标中的最大F1阈值。
  • 如果您使用训练和验证数据训练模型,则使用验证数据模型指标中的最大F1阈值。
  • 如果您使用训练数据并设置nfold参数来训练模型,则使用训练数据模型指标中的最大F1阈值。
  • 如果您使用训练数据和验证数据来训练模型,并设置nfold参数,则使用验证数据模型指标中的最大F1阈值。

0

当对没有实际结果进行预测的新数据(在h2o术语中为“y”参数)时,没有F1 Max分数或其他指标,您必须依靠从h2o.predict()中得出的预测。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接