future_map()でさらに便利に

R による並列プログラミング

Nabeel Imam

Data Scientist

データと背景

head(data)
   state  year month plurality
 1 AK     1995     1         1
 2 AK     1995     1         1
 3 AK     1995     1         1
 4 AK     1995     1         1
 5 AK     1995     1         1
 6 AK     1995     1         1

新しい文字列列を作成

twins <- function (x) {
    ifelse(x == 2,"Twins", "Not twins")
}
R による並列プログラミング

双子にラベル付け

plan(multisession, workers = 4)


data %>% mutate(label = future_map_chr(plurality, twins))
plan(sequential)
   state  year month plurality label    
 1 AK     1995     1         1 Not twins
 2 AK     1995     1         1 Not twins
 3 AK     1995     1         1 Not twins
 4 AK     1995     1         1 Not twins
 5 AK     1995     1         1 Not twins
 6 AK     1995     1         1 Not twins
 7 AK     1995     1         1 Not twins
 8 AK     1995     1         1 Not twins
 9 AK     1995     1         1 Not twins
10 AK     1995     1         1 Not twins
...
R による並列プログラミング

双子にラベル付け

microbenchmark(
  "map_chr" = {
    data %>%
 mutate(label = map_chr(plurality, twins))
  },
  "future_map_chr" = {
    data %>%
 mutate(label = future_map_chr(plurality, twins))
  }
)
単位: 秒
            expr     mean   median neval
1        map_chr 72.69067 73.50705    10
2 future_map_chr 34.52351 34.56357    10

フローチャート。map_chr() と future_map_chr() で「label」列を作成。future_map_chr() は実行時間を50%短縮。

R による並列プログラミング

グループごとの処理

双子の割合

birth_prop <- function (df) {

  N <- sum(df$plurality == 2)  # 双子の出産数
  prop <- N/nrow(df)           # 全出産に占める割合
  names(prop) <- "proportion"  # 値の名前

  return(prop) 
}
R による並列プログラミング

グループごとの処理

データセットにグループA、B、Cがある。グループで分割し、値を集約。集約値を結合してグループごと1行のデータに。

R による並列プログラミング

グループごとの処理

plan(multisession, workers = 6)


data %>% # 年ごとに分割し、future_map_dfr()へ split(data$state) %>%
# 関数だけを渡す future_map_dfr(birth_prop,
# グループ列名を .id に指定 .id = "state")
plan(sequential)

結果は1つのデータフレームに行結合

   year  proportion
 1 AK        0.0114
 2 AL        0.0264
 3 AR        0.0196
 4 AZ        0.0218
 5 CA        0.0197
 6 CO        0.0217
 7 CT        0.0225
 8 DC        0.0283
 9 DE        0.0268
10 FL        0.0212
...
R による並列プログラミング

グローバル変数の利用

# 第2引数で多胎数を指定
birth_prop <- function (df, plur_value) {

  N <- sum(df$plurality == plur_value) # 指定した多胎の出産数
  prop <- N/nrow(df)                   
  names(prop) <- "proportion"          

  return(prop) 
}


new_plur <- 3 # グローバルな単一値の変数
R による並列プログラミング

グローバル変数の利用

config <- furrr_options(globals = "new_plur")


plan(multisession, workers = 4)
data %>% split(data$state) %>% future_map_dfr(birth_prop, plur_value = new_plur, .options = config, .id = "state") plan(sequential)
   state proportion
 1 AK      0       
 2 AL      0.000659
 3 AR      0       
 4 AZ      0.000605
 5 CA      0.000673
 6 CO      0.000776
 7 CT      0.000867
 8 DC      0.00189 
 9 DE      0       
10 FL      0.00106
...
R による並列プログラミング

列結合してデータフレーム化

data %>%
  split(data$state) %>% 
  future_map_dfc(birth_prop, # _dfc 版
                 plur_value = new_plur,
                 .options = config)
     AK       AL    AR       AZ       CA       CO       CT      DC
1     0 0.000659     0 0.000605 0.000673 0.000776 0.000867 0.00189 ...
R による並列プログラミング

Passons à la pratique !

R による並列プログラミング

Preparing Video For Download...