Share
Embed
tidymodelsによるモデル構築と運用 / tidymodels
Fukuoka.R#15 https://fukuoka-r.connpass.com/event/139211/ での発表資料です
リポジトリ http://github.com/uribo/190831_fukuokar15
Uryu Shinya
August 31, 2019
Transcript
46 slides
-
ϞσϧͷੑೳධՁ λεΫʹԠͨ͡ධՁࢦඪΛར༻͢Δ ܾఆ(R2, RSQ: coefficient of determination) ೋฏۉฏํࠜޡࠩ (RMSE: Root
Mean Square Error) ฏۉઈରޡࠩ(MAE: Mean absolute error) ࠞಉߦྻ ਖ਼ղ ద߹ͱ࠶ݱ ROCۂઢͱAUC ճؼ ྨ -
{parsnip} {recipes} {rsample} {yardstick} {tidymodels}ʹؚ·ΕΔύοέʔδ library(tidymodels) #> Registered S3 method
overwritten by 'xts': #> method from #> as.zoo.xts zoo #> ─ Attaching packages ───── tidymodels 0.0.2 ─ #> ✔ broom 0.5.2 ✔ purrr 0.3.2 #> ✔ dials 0.0.2 ✔ recipes 0.1.6 #> ✔ dplyr 0.8.3 ✔ rsample 0.0.5 #> ✔ ggplot2 3.2.1 ✔ tibble 2.1.3 #> ✔ infer 0.4.0.1 ✔ yardstick 0.0.3 #> ✔ parsnip 0.0.3.1 #> ─ Conflicts ──────── tidymodels_conflicts() ─ #> ✖ purrr ::discard() masks scales ::discard() #> ✖ dplyr ::filter() masks stats ::filter() #> ✖ dplyr ::lag() masks stats ::lag() #> ✖ recipes ::step() masks stats ::step() Ϟσϧߏஙɾద༻ ϞσϧͷੑೳධՁ σʔλલॲཧɺ ಛྔੜ ϦαϯϓϦϯά ಡΈࠐ·ΕΔύοέʔδɺόʔδϣϯ ͓Αͼ໊ؔͷিಥʹ͍ͭͯग़ྗ -
ΞϠϝଐछͷྨ छ໊(Species)Λϥϕϧͱ͠ɺྨΛద༻͢Δ iris <- as_tibble(iris) glimpse(iris) #> Observations: 150 #>
Variables: 5 #> $ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, … #> $ Sepal.Width <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, … #> $ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, … #> $ Petal.Width <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, … #> $ Species <fct> setosa, setosa, setosa, setosa, se… Ֆͷܗଶʹؔ͢Δͭͷಛྔ -
σʔληοτΛ ੳηοτ USBJO ɺධՁηοτ UFTU ʹ͚Δ ੳηοτ ධՁηοτ σʔλׂ 3ͰͷφͳσʔλׂͷΓํSTBNQMFύοέʔδʹΑΔަࠩݕূגࣜձࣾϗΫιΤϜͷϒϩά
IUUQTCMPHIPYPNDPNFOUSZ {rsample} σʔληοτ Ϟσϧͷֶशʹ༻͍Δ ϞσϧͷੑೳධՁΛଌఆ͢ΔͨΊɺ ະͷใͱͯ͠ར༻͞ΕΔ -
ׂϥϯμϜ σʔλׂ {rsample} σʔληοτͷׂΛੳηοτͱ͢Δ iris_split <- initial_split(iris, prop = 0.6)
iris_split #> <90/60/150> iris_train <- training(iris_split) iris_test <- testing(iris_split) -
લॲཧɾಛྔΤϯδχΞϦϯά{recipes} Ϟσϧʹ༻͍ΔσʔλՃͷखଓ͖ΛʮϨγϐʯԽ ϞσϧͰѻ͏σʔλͷલॲཧΛSFDJQFTͰߦ͏גࣜձࣾϗΫιΤϜͷϒϩά IUUQTCMPHIPYPNDPNFOUSZ 1 2 3 recipe() step_*() prep()
bake()/juice() 4 ར༻͢ΔมͷؔΛఆٛ ˠࡐྉΛࢦఆ͢Δ σʔλՃͷखଓ͖Λࢦఆ ˠௐཧ๏Λهड़͢Δ σʔληοτʹద༻ ˠௐཧΛߦ͏ TUFQ@ ͷॲཧΛ౷߹ ˠϨγϐΛ֬ೝ͢Δ -
iris_recipe <- iris_train %>% #> #4QFDJFTΛతมɺଞͷมΛઆ໌มʹͨ͠Ϟσϧ recipe(formula = Species ~
.) %>% #> # εςοϓ1: ͯ͢ͷઆ໌มΛରʹ #> # ૬ؔͷߴ͍(0.9Ҏ্)มͷ͍ͣΕ͔Λআ֎ step_corr(all_predictors(), threshold = 0.9) %>% #> # εςοϓ2: ͯ͢ͷઆ໌มΛରʹมͷඪ४Խ step_normalize(all_predictors(), -all_outcomes()) ϞσϧͷॲཧΛύΠϓԋࢉࢠͰՃ outcome ~ predictors role -
step_*()Ͱͷมͷࢦఆ จࣈྻͰͷࢦఆ tidyselectͷؔ ϞσϧͰͷrole 1 2 3 มͷσʔλܕ 4 all_predictors()
all_outcomes() starts_with() contains()ͳͲ all_nominal() all_numeric() "Species" "Sepal.Length" -
recipeΦϒδΣΫτ iris_recipe #> Data Recipe #> #> Inputs: #> #>
role #variables #> outcome 1 #> predictor 4 #> #> Operations: #> #> Correlation filter on all_predictors #> Centering and scaling for all_predictors, -, all_outcomes() recipe()͓Αͼstep_*()Ͱఆٛͨ͠खॱΛࣔ͢ -
iris_recipe_comp <- iris_recipe %>% prep() #> … தུ #> Training
data contained 90 data points and no missing data. #> #> Operations: #> #> Correlation filter removed Petal.Length [trained] #> Centering and scaling for Sepal.Length, Sepal.Width, Petal.Width [trained] ੳηοτʹద༻ͨ͠ΒͲ͏ͳΔ͔ ͜ͷঢ়ଶͰ·ͩσʔλ͕ੜ͞Ε͍ͯͳ͍ recipeΦϒδΣΫτ -
σʔληοτʹϨγϐΛద༻ iris_training <- juice(iris_recipe_comp) glimpse(iris_training) #> Observations: 90 #> Variables:
4 #> $ Sepal.Length <dbl> -1.35774587, -1.71228299… #> $ Sepal.Width <dbl> 0.230356130, -0.438419732… #> $ Petal.Width <dbl> -1.2122997, -1.2122997… #> $ Species <fct> setosa, se… iris_testing <- iris_recipe_comp %>% bake(new_data = iris_test) ੳηοτ ධՁηοτ Petal.Length͕ফ͍͑ͯΔ iris_testing Ͱಉ༷ -
all.equal( mean(scale(iris_train$Sepal.Length)), mean(iris_training$Sepal.Length)) #> [1] TRUE {recipes}ͷॲཧ༰Λ͔֬ΊΔ ฏۉ ࢄͷඪ४Խ iris_train
%>% recipe(formula = Species ~ .) %>% step_corr(all_predictors(), threshold = 0.9) %>% step_normalize(all_predictors(), -all_outcomes()) -
{recipes}ͷॲཧ༰Λ͔֬ΊΔ corrr ::correlate(iris_test %>% select_if(is.numeric), method = "pearson", use =
"pairwise.complete.obs", quiet = TRUE) #> # A tibble: 4 x 5 #> rowname Sepal.Length Sepal.Width Petal.Length Petal.Width #> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 Sepal.Length NA -0.0685 0.859 0.798 #> 2 Sepal.Width -0.0685 NA -0.395 -0.333 #> 3 Petal.Length 0.859 -0.395 NA 0.958 #> 4 Petal.Width 0.798 -0.333 0.958 NA ૬ؔͷߴ͍มͷยํΛ σʔληοτ͔Βআ֎͢Δ -
recipes͕ఏڙ͢Δstep ม ΤϯίʔσΟϯά ɾ࣌ؒ ϑΟϧλॲཧ step_*() ؔ ͱͯ͠ఏڙ͞ΕΔ ܽଛͷೖ ඪ४Խ
࣍ݩݮ ͳͲ ls("package:recipes", pattern = “^step_") #> # 69ݸͷstep_*ؔ (version 0.1.6) -
step_*()ͷ֦ுͱ࣮ σʔλܕʹԠͨ͡ {textrecipes} {embed} 5FOTPS'MPXϞσϧͰར༻ {tfdatasets} step_*() Λఏڙ͢Δύοέʔδ จࣈྻ ΧςΰϦ
ຊޠͷલॲཧ {washoku} ਖ਼نԽɺ֯શ֯ॲཧɺॅॴׂͳͲ ߏத remotes ::install_github("uribo/washoku") -
Ϟσϧߏங {parsnip} ଟ༷ͳϞσϦϯάύοέʔδΛϥοϓɻ౷Ұతʹૢ࡞Մೳ ༷Λఆٛ ΤϯδϯʢύοέʔδʣΛࢦఆ ϞσϧͷͯΊ ճؼʁྨʁ 1 set_engine() ՝ʹదͨ͠ϞσϧΛબͿ
2 3 fit() rand_forest() linear_reg() logistic_reg() ੳηοτͷֶश -
ύοέʔδ ܾఆʹ༩͑Δ ಛྔ ࡞͢Δ ܾఆͷ ϊʔυʹؚ·ΕΔ ࠷খͷαϯϓϧ ranger mtry num.trees
min.node.size randomForest mtry ntree nodesize sparklyr mtry num.trees min_instances_per_node ؔͷॻ͖ํʢҾͷࢦఆํ๏ʣ͕ ύοέʔδʹΑͬͯҟͳΔ {parsnip}ΛΘͳ͍߹ rand_forest(mode = "classification", mtry, trees, min_n) -
rf_spec <- rand_forest(mode = "classification", trees = 100) #> #ranger
::ranger iris_ranger <- rf_spec %>% set_engine("ranger", seed = 123) %>% fit(Species ~ ., data = iris_training) #> #randomForest ::randomForest iris_rf <- rf_spec %>% set_engine("randomForest") %>% fit(Species ~ ., data = iris_training) ϥϯμϜϑΥϨετΛར༻ͨ͠Ϟσϧ Τϯδϯ ύοέʔδ ʹݻ༗ͷ ΦϓγϣϯΛࢦఆՄೳ -
Ϟσϧͷద༻݁Ռ iris_rf #> Call: #> randomForest(x = as.data.frame(x), y =
y, ntree = ~100) #> Type of random forest: classification #> Number of trees: 100 #> No. of variables tried at each split: 1 #> #> OOB estimate of error rate: 2.22% #> Confusion matrix: #> setosa versicolor virginica class.error #> setosa 36 0 0 0.00000000 #> versicolor 0 26 1 0.03703704 #> virginica 0 1 26 0.03703704 rand_forest()ɺset_engine()Ͱࢦఆͨ͠ॲཧ͕࣮ߦ -
{yardstick} ϞσϧੑೳධՁ ςετσʔλʢ৽͍͠σʔλʣΛରʹϥϕϧͷਪఆ iris_rf_pred <- predict(iris_rf, iris_testing) %>% bind_cols(iris_testing) rf_metrics
<- iris_rf_pred %>% metrics(truth = Species, estimate = .pred_class) rf_metrics #> # A tibble: 2 x 3 #> .metric .estimator .estimate #> <chr> <chr> <dbl> #> 1 accuracy multiclass 0.95 #> 2 kap multiclass 0.925 ྨͷλεΫͰ ਫ਼ BDDVSBDZ ͱ,BQQB ͕ੑೳࢦඪͱͯ͠༻ҙ͞ΕΔ -
{yardstick} ੑೳࢦඪͷΈ߹ΘͤͨΓಛఆͷੑೳࢦඪΛબΔ ϞσϧੑೳධՁ ྨͷλεΫͰͷ ੑೳධՁؔʹ USVUI FTUJNBUFΛࢦఆ͢Δ iris_rf_pred %>% accuracy(Species,
.pred_class) perf_metrics <- metric_set(accuracy, recall, precision) iris_rf_pred %>% perf_metrics(truth = Species, estimate = .pred_class) #> # A tibble: 3 x 3 #> .metric .estimator .estimate #> <chr> <chr> <dbl> #> 1 accuracy multiclass 0.983 #> 2 recall macro 0.981 #> 3 precision macro 0.986 iris_rf_pred %>% kap(Species, .pred_class) -
ggplotϕʔεͷՄࢹԽ iris_ranger %>% predict(iris_testing, type = "prob") %>% bind_cols(iris_testing) %>%
roc_curve(Species, rstarts_with(".pred")) %>% autoplot() ROCۂઢ -
σʔλͷׂҰ͖Γ ަࠩݕূͷͨΊͷϦαϯϓϦϯά {rsample} ੳηοτ ධՁηοτ iris_split <- initial_split(iris, prop =
0.6) ෳͷϦαϯϓϦϯάσʔλ͔Β Ϟσϧͷ൚ԽੑೳΛධՁ͢Δ͜ͱ͕ඞཁˠަࠩݕূ -
iris_cv <- vfold_cv(iris, v = 4) iris_cv #> # A
tibble: 4 x 2 #> splits id #> <named list> <chr> #> 1 <split [112/38]> Fold1 #> 2 <split [112/38]> Fold2 #> 3 <split [113/37]> Fold3 #> 4 <split [113/37]> Fold4 σʔλɺతʹԠׂͨ͡ํ๏ΛબͿ assessment(iris_cv$splits[[1]]) ධՁηοτ analysis(iris_cv$splits[[1]]) ੳηοτ FoldʹϦαϯϓϦϯά͞Εͨ ੳηοτɺධՁηοτΛࢀর σʔλϑϨʔϜͷߦ͝ͱʹ ׂ͞Εͨσʔληοτ͕ ؚ·ΕΔ -
Fold͝ͱʹֶशɺੑೳࢦඪΛࢉग़ #> # 4-fold cross-validation #> # A tibble: 4
x 4 #> splits id recipes accuracy #> * <named list> <chr> <named list> <dbl> #> 1 <split [112/38]> Fold1 <tibble [38 × 5]> 0.974 #> 2 <split [112/38]> Fold2 <tibble [38 × 5]> 0.895 #> 3 <split [113/37]> Fold3 <tibble [37 × 5]> 0.919 #> 4 <split [113/37]> Fold4 <tibble [37 × 5]> 0.973 .Last.value %>% pull(accuracy) %>% mean() #> [1] 0.9400782 -
ϥϯμϜϑΥϨετϞσϧͰ ͭͷϋΠύʔύϥϝʔλͷࢦఆ͕Մೳ ϋΠύʔύϥϝʔλͷ୳ࡧ {dials} ϋΠύʔύϥϝʔλͷ͕Ϟσϧͷਫ਼ʹӨڹ͢Δ rand_forest(mode = "classification", mtry, trees,
min_n) ύϥϝʔλͷΛมԽͤͨ͞ঢ়ଶͰͷੑೳධՁ͕ඞཁ -
set.seed(1234) bst_grid <- grid_random( range_set(trees, c(1, 50)), range_set(min_n, c(2, 30)),
size = 3) #> # A tibble: 3 x 2 #> trees min_n #> <int> <int> #> 1 28 6 #> 2 16 13 #> 3 22 16 άϦουαʔν mod_obj <- rand_forest( ɹɹmode = "classification", ɹɹtrees = varying(), ɹɹmin_n = varying(), ɹɹmtry = 3) ύϥϝʔλ໊ͱ ϥϯμϜʹׂΓৼΔͷ ൣғΛࢦఆ TJ[F݅ͷϥϯμϜσʔλ͕ੜ {parsnip} ͰͷϞσϧ࡞ ஈ֊Ͱݻఆ͠ͳ͍ ύϥϝʔλΛࢦఆ -
lapply(), for()ͰOKͰ͢ purrrͱΈ߹Θͤͯ seq.int(nrow(bst_grid)) %>% purrr ::map( ~ merge(mod_obj, bst_grid[.x,
])) %>% purrr ::flatten() #> #> Random Forest Model Specification (classification) #> #> Main Arguments: #> mtry = 3 #> trees = 28 #> min_n = 6 #> Main Arguments: #> mtry = 3 #> trees = 16 #> min_n = 13 #> Main Arguments: #> mtry = 3 #> trees = 22 #> min_n = 16 ͦΖͦΖखΛग़͢QVSSSOFLPTLZ4QFBLFS%FDL IUUQTTQFBLFSEFDLDPNT@VSZVOFLPTLZ -
·ͱΊ {parsnip} {recipes} {rsample} {yardstick} Ϟσϧͷֶश ੑೳධՁ σʔλՃ ϦαϯϓϦϯά {tidymodels}
ύΠϓϑϨϯυϦʔɺ ౷ܭϞσϧɾػցֶशʹඞཁͳॲཧΛ؆ུԽͰ͖Δ ౷ҰతͳΠϯλʔϑΣʔεΛఏڙ͢Δɻ initial_split() vfold_cv() step_*() bake()/juice() set_engine() metrics() 1 2 3 4