rsample group_bootstrap 比 bootstrap 慢约 2000 倍。为什么?

问题描述 投票:0回答:1

rsample 包包含一个用于引导的函数和另一个允许对数据组进行引导的函数。

分组版本要慢很多(~2000 倍)。我原以为它会慢一点,但我不明白为什么它慢那么多。

(我的示例中的组没有意义,但这应该与测试两个函数的速度无关。)

library(tidyverse)
library(rsample)

dat <- tibble(x = 1:1000)

microbenchmark::microbenchmark(
  grouped = {
    dat %>%
      group_bootstraps(group = x, times = 10)}, 
  simple = {
    dat %>%
      bootstraps(times = 10)},
  times = 5)

enter image description here

r tidymodels rsample
1个回答
0
投票

我可以复制你的时间安排!

与其深入探讨减速的技术原因,不如思考以下几点:

  • 在典型的建模流程中,与建模函数在训练时可能花费的时间相比,在任何一种情况下生成这些重采样的时间都可以忽略不计。我们专注于优化那些运行时间较长的作品。
  • 您引导 1000 个组的示例,每个组都有一个观察值,这是该函数相对于其未分组朋友的性能的最坏情况;除了分组变量之外没有其他变量,每一行都是它自己的组。
  • 与我们期望该函数实际使用的上下文更相似的示例会导致更相似的计时(在函数文档的情况下约为 50 倍):
library(tidyverse)
library(rsample)

data(ames, package = "modeldata")

set.seed(1)

microbenchmark::microbenchmark(
  grouped = {
    ames %>%
      group_bootstraps(group = Neighborhood, times = 10)}, 
  simple = {
    ames %>%
      bootstraps(times = 10)},
  times = 5)
#> Unit: milliseconds
#>     expr        min         lq     mean     median         uq       max neval
#>  grouped 214.892521 217.795854 228.4819 219.397355 228.345646 261.97791     5
#>   simple   4.314799   4.617092   4.6412   4.665144   4.775475   4.83349     5
#>  cld
#>   a 
#>    b

创建于 2024-05-16,使用 reprex v2.1.0

Simon,tidymodels 团队

© www.soinside.com 2019 - 2024. All rights reserved.