forked from TheoreticalEcology/machinelearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA4-MLpipeline.qmd
831 lines (622 loc) · 27.8 KB
/
A4-MLpipeline.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
---
output: html_document
editor_options:
chunk_output_type: console
---
# Machine learning pipeline {#workflow}
The Standard Machine Learning Pipeline at the example of the Titanic Data set
Before we specialize on any tuning, it is important to understand that machine learning always consists of a pipeline of actions.
The typical machine learning workflow consist of:
- Data cleaning and exploration (EDA = explorative data analysis) for example with tidyverse.
- Preprocessing and feature selection.
- Splitting data set into training and test set for evaluation.
- Model fitting.
- Model evaluation.
- New predictions.
Here is an (optional) video that explains the entire pipeline from a slightly different perspective:
```{r chunk_chapter4_39, eval=knitr::is_html_output(excludes = "epub"), results = 'asis', echo = F}
cat(
'<iframe width="560" height="315"
src="https://www.youtube.com/embed/nKW8Ndu7Mjw"
frameborder="0" allow="accelerometer; autoplay; encrypted-media;
gyroscope; picture-in-picture" allowfullscreen>
</iframe>'
)
```
In the following example, we use tidyverse, a collection of R packages for data science / data manipulation mainly developed by Hadley Wickham. A video that explains the basics can be found here :
```{r chunk_chapter4_40, eval=knitr::is_html_output(excludes = "epub"), results = 'asis', echo = F}
cat(
'<iframe width="560" height="315"
src="https://www.youtube.com/embed/nRtp7wSEtJA"
frameborder="0" allow="accelerometer; autoplay; encrypted-media;
gyroscope; picture-in-picture" allowfullscreen>
</iframe>'
)
```
Another good reference is "**R for data science**" by Hadley Wickham: <a href="https://r4ds.had.co.nz/" target="_blank" rel="noopener"></a>.
For this lecture you need the Titanic data set provided by us. You can find it in GRIPS (datasets.RData in the data set and submission section) or at <a href="http://rhsbio6.uni-regensburg.de:8500" target="_blank" rel="noopener">http://rhsbio6.uni-regensburg.de:8500</a>.
We have split the data set already into training and test/prediction data sets (the test/prediction split has one column less than the train split, as the result is not known a priori).
## Data preparation
Load necessary libraries:
```{r chunk_chapter4_41, message=FALSE}
library(tidyverse)
```
Load data set:
```{r chunk_chapter4_42}
library(EcoData)
data(titanic_ml)
data = titanic_ml
```
Standard summaries:
```{r chunk_chapter4_43}
str(data)
summary(data)
```
The name variable consists of 1309 unique factors (there are 1309 observations...) and could be now transformed. If you are interested in how to do that, take a look at the following box.
::: {.callout-tip collapse="true"}
## Feature engineering of the name variable
```{r chunk_chapter4_44}
length(unique(data$name))
```
However, there is a title in each name. Let's extract the titles:
1. We will extract all names and split each name after each comma ",".
2. We will split the second split of the name after a point "." and extract the titles.
```{r chunk_chapter4_45}
first_split = sapply(data$name,
function(x) stringr::str_split(x, pattern = ",")[[1]][2])
titles = sapply(first_split,
function(x) strsplit(x, ".",fixed = TRUE)[[1]][1])
```
We get 18 unique titles:
```{r chunk_chapter4_46}
table(titles)
```
A few titles have a very low occurrence rate:
```{r chunk_chapter4_47}
titles = stringr::str_trim((titles))
titles %>%
fct_count()
```
We will combine titles with low occurrences into one title, which we can easily do with the forcats package.
```{r chunk_chapter4_48}
titles2 =
forcats::fct_collapse(titles,
officer = c("Capt", "Col", "Major", "Dr", "Rev"),
royal = c("Jonkheer", "Don", "Sir",
"the Countess", "Dona", "Lady"),
miss = c("Miss", "Mlle"),
mrs = c("Mrs", "Mme", "Ms")
)
```
We can count titles again to see the new number of titles:
```{r chunk_chapter4_49}
titles2 %>%
fct_count()
```
Add new title variable to data set:
```{r chunk_chapter4_50}
data =
data %>%
mutate(title = titles2)
```
:::
### Imputation
NAs are a common problem in ML. For example, the age variable has 20% NAs:
```{r chunk_chapter4_51}
summary(data$age)
sum(is.na(data$age)) / nrow(data)
```
Either we remove all observations with NAs, or we impute (fill) the missing values, e.g. with the median age. However, age itself might depend on other variables such as sex, class and title. We want to fill the NAs with the median age of these groups. In tidyverse we can easily "group" the data, i.e. we will nest the observations (here: group_by after sex, pclass and title). After grouping, all operations (such as our median(age....)) will be done within the specified groups.
```{r chunk_chapter4_52}
data =
data %>%
select(survived, sex, age, fare, pclass) %>%
group_by(sex, pclass) %>%
mutate(age2 = ifelse(is.na(age), median(age, na.rm = TRUE), age)) %>%
mutate(fare2 = ifelse(is.na(fare), median(fare, na.rm = TRUE), fare)) %>%
ungroup()
```
### Preprocessing and Feature Selection
Later (tomorrow), we want to use Keras in our example, but it cannot handle factors and requires the data to be scaled.
Normally, one would do this for all predictors, but as we only show the pipeline here, we have sub-selected a bunch of predictors and do this only for them. We first scale the numeric predictors and change the factors with only two groups/levels into integers (this can be handled by Keras).
```{r chunk_chapter4_53}
data_sub =
data %>%
select(survived, sex, age2, fare2, pclass) %>%
mutate(age2 = scales::rescale(age2, c(0, 1)),
fare2 = scales::rescale(fare2, c(0, 1))) %>%
mutate(sex = as.integer(sex) - 1L,
pclass = as.integer(pclass - 1L))
```
::: {.callout-tip collapse="true"}
## Transforming factors with more than two levels
Factors with more than two levels should be **one hot encoded** (Make columns for every different factor level and write 1 in the respective column for every taken feature value and 0 else. For example: $\{red, green, green, blue, red\} \rightarrow \{(0,0,1), (0,1,0), (0,1,0), (1,0,0), (0,0,1)\}$):
```{r chunk_chapter4_54, eval = FALSE}
one_title = model.matrix(~0+as.factor(title), data = data)
colnames(one_title) = levels(data$title)
one_sex = model.matrix(~0+as.factor(sex), data = data)
colnames(one_sex) = levels(data$sex)
one_pclass = model.matrix(~0+as.factor(pclass), data = data)
colnames(one_pclass) = paste0("pclass", 1:length(unique(data$pclass)))
```
And we have to add the dummy encoded variables to the data set:
```{r chunk_chapter4_55, eval = FALSE}
data = cbind(data.frame(survived= data$survived),
one_title, one_sex, age = data$age2,
fare = data$fare2, one_pclass)
head(data)
```
:::
## Modelling
### Split data for final predictions
To tune our hyperparameters and evaluate our models, we need to split the data as we learned in the CV section. Before doing so, however, we must split off the new observations in the data set :
```{r}
summary(data_sub$survived)
```
655 observations have NAs in our response variable, these are the observations for which we want to make predictions at the end of our pipeline (we have no information about their actual values!).
```{r chunk_chapter4_56}
data_new = data_sub[is.na(data_sub$survived),]
data_obs = data_sub[!is.na(data_sub$survived),]
```
### Training and evaluation
Now, we can do a simple 10xCV with the observed_data:
```{r, warning=FALSE, message=FALSE, results='hide'}
library(glmnet)
library(glmnetUtils)
data_obs = data_sub[!is.na(data_sub$survived),]
cv = 10
outer_split = as.integer(cut(1:nrow(data_obs), breaks = cv))
results = data.frame(
set = 1:cv,
AUC = rep(NA, cv)
)
for(i in 1:cv) {
train_outer = data_obs[outer_split != i, ]
test_outer = data_obs[outer_split == i, ]
model = glmnet(survived~.,data = train_outer, family = "binomial",alpha = 0.2)
results[i, 2] = Metrics::auc(test_outer$survived, predict(model, test_outer,
alpha = 0.2,
s = 0.01,
type = "response"))
}
```
```{r}
print(results)
print(mean(results$AUC))
```
### Hyperparameter optimization
We did a simple 10xCV to evaluate our model but we didn't tune our hyperparameters ($\lambda$ and $\alpha$). If we want to tune them, we need do another CV within each split of the model evaulation CV, which is called nested CV.
We used only one split (the split for the submission server doesn't count) to evaluate the performance of the model before we made the final predictions. If we test many different hyperparameter combinations, how do we ensure that a certain hyperparameter is not only good for our training dataset but also good for the new data (our outer split on the submission server)? You may have guessed it already, we need to do another CV within the previous CV to check whether a certain hyperparameter solution generalizes to the whole data. To tune $\lambda$, we would need to split the data another time (called nested CV).
Why is it important to tune hyperparameters? Hyperparameters (configuration parameters of our ML algorithms that (mostly) control their complexity) are usually tuned (optimized) in an automatic / systematic way. A common procedure, called random search, is to sample random configuration combinations from the set of hyperparameters and test for each combination the prediction error.
Let's implement manually a nested CV to tune the $\alpha$. Let's start with a 5CVx5CV and 20x different alpha values:
```{r}
library(glmnet)
library(glmnetUtils)
set.seed(42)
data_obs = data_sub[!is.na(data_sub$survived),]
cv = 5
cv_inner = 5
hyper_alpha = runif(20,0, 1)
outer_split = as.integer(cut(1:nrow(data_obs), breaks = cv))
results = data.frame(
set = rep(NA, cv),
alpha = rep(NA, cv),
AUC = rep(NA, cv)
)
for(i in 1:cv) {
train_outer = data_obs[outer_split != i, ]
test_outer = data_obs[outer_split == i, ]
best_alpha = NULL
best_auc = NULL
# inner split
for(j in 1:cv_inner) {
inner_split = as.integer(cut(1:nrow(train_outer), breaks = cv_inner))
train_inner = train_outer[inner_split != j, ]
test_inner = train_outer[inner_split == j, ]
tuning_results_inner =
sapply(1:length(hyper_alpha), function(k) {
model = glmnet(survived~.,data = train_inner, family = "binomial",alpha = hyper_alpha[k])
return(Metrics::auc(test_inner$survived, predict(model, test_inner,
alpha = hyper_alpha[k],
s = 0.01,
type = "response")))
})
best_alpha[j] = hyper_alpha[which.max(tuning_results_inner)]
best_auc[j] = max(tuning_results_inner)
}
best_alpha = best_alpha[which.max(best_auc)]
model = glmnet(survived~., data = train_outer, alpha = best_alpha, family = "binomial")
results[i, 1] = i
results[i, 2] = best_alpha
results[i, 3] = Metrics::auc(test_outer$survived, predict(model, test_outer, s = 0.01, alpha = best_alpha, type = "response"))
}
print(results)
```
## Predictions and Submission
When we are satisfied with the performance of our model, we will create predictions for the new observations on the submission server. But first we will train now our model on the full observed dataset:
$\alpha = 0.915$ has the highest AUC, let's use it to train the model on the full dataset:
```{r}
model = glmnet(survived~.,data = data_obs, family = "binomial",alpha = 0.915)
```
We cannot assess the performance for the new observations because the true survival ratio is unknown, however, we can now submit our predictions to the submission server at <a href="http://rhsbio7.uni-regensburg.de:8500" target="_blank" rel="noopener">http://rhsbio7.uni-regensburg.de:8500</a>.
For the submission it is critical to change the predictions into a data.frame, select the second column (the probability to survive), and save it with the write.csv function:
```{r, results='hide', warning=FALSE, message=FALSE}
data_new = data_sub[is.na(data_sub$survived),]
write.csv(data.frame(y = predict(model, data_new, alpha = 0.915, s = 0.01, type = "response")[,1] ), file = "Max_1.csv")
```
We have now used the $\alpha$ value with the highest AUC here, but our tuning has shown that the best value of $\alpha$ depends on the partitioning, so it would probably be better to build ten models and combine their predictions (e.g., by averaging the predictions):
```{r, results='hide',warning=FALSE, message=FALSE}
prediction_ensemble =
sapply(results$alpha, function(alpha) {
model = glmnet(survived~.,data = data_obs, family = "binomial",alpha = alpha)
return(predict(model, data_new, alpha = alpha, s = 0.01, type = "response")[,1])
})
write.csv(data.frame(y = apply(prediction_ensemble, 1, mean)), file = "Max_1.csv")
```
## Exercises
::: {.callout-caution icon="false"}
#### Task: Tuning $\alpha$ and $\lambda$
1. Extend the code from above and tune $\alpha$ and $\lambda$ (Nested-CV or via a simple CV)
2. Train the model with best set of hyperparameters and submit your predictions
3. Compare the predictive performance from the single best model with the ensemble model
Submit both predictions (<http://rhsbio7.uni-regensburg.de:8500/>), which model has a higher AUC?
```{r}
library(EcoData)
library(dplyr)
library(missRanger)
library(glmnet)
library(glmnetUtils)
data(titanic_ml)
data = titanic_ml
data =
data %>% select(survived, sex, age, fare, pclass)
# missRanger uses a random forest to impute NAs (RF is trained on the data to predict values for the NAs)
data[,-1] = missRanger(data[,-1], verbose = 0)
data_sub =
data %>%
mutate(age = scales::rescale(age, c(0, 1)),
fare = scales::rescale(fare, c(0, 1))) %>%
mutate(sex = as.integer(sex) - 1L,
pclass = as.integer(pclass - 1L))
data_new = data_sub[is.na(data_sub$survived),] # for which we want to make predictions at the end
data_obs = data_sub[!is.na(data_sub$survived),] # data with known response
```
Bonus:
- Try different features
- Try cito
- Try different datasets (see @sec-datasets)
Code template for a simple CV (only $\alpha$ is tuned, add the tuning for $\lambda$:
```{r}
library(glmnet)
library(glmnetUtils)
set.seed(42)
data_obs = data_sub[!is.na(data_sub$survived),]
cv = 5
hyper_alpha = runif(20,0, 1)
outer_split = as.integer(cut(1:nrow(data_obs), breaks = cv))
results = data.frame(
set = rep(NA, cv),
alpha = rep(NA, cv),
AUC = rep(NA, cv)
)
for(i in 1:cv) {
train_outer = data_obs[outer_split != i, ]
test_outer = data_obs[outer_split == i, ]
tuning_results =
sapply(1:length(hyper_alpha), function(k) {
model = glmnet(survived~.,data = train_outer, family = "binomial",alpha = hyper_alpha[k])
return(Metrics::auc(test_outer$survived, predict(model, test_outer,
alpha = hyper_alpha[k],
s = 0.01,
type = "response")))
})
best_alpha = hyper_alpha[which.max(tuning_results)]
results[i, 1] = i
results[i, 2] = best_alpha
results[i, 3] = max(tuning_results)
}
print(results)
```
`r hide("Click here to see the solution for the single model")`
Nested CV:
```{r}
set.seed(42)
data_obs = data_sub[!is.na(data_sub$survived),]
cv = 5
cv_inner = 5
hyper_alpha = runif(30,0, 1)
hyper_lambda = runif(30,0, 1)
outer_split = as.integer(cut(1:nrow(data_obs), breaks = cv))
results = data.frame(
set = rep(NA, cv),
alpha = rep(NA, cv),
lambda = rep(NA, cv),
AUC = rep(NA, cv)
)
for(i in 1:cv) {
train_outer = data_obs[outer_split != i, ]
test_outer = data_obs[outer_split == i, ]
best_alpha = NULL
best_lambda = NULL
best_auc = NULL
# inner split
for(j in 1:cv_inner) {
inner_split = as.integer(cut(1:nrow(train_outer), breaks = cv_inner))
train_inner = train_outer[inner_split != j, ]
test_inner = train_outer[inner_split == j, ]
tuning_results_inner =
sapply(1:length(hyper_alpha), function(k) {
model = glmnet(survived~.,data = train_inner, family = "binomial",alpha = hyper_alpha[k])
return(Metrics::auc(test_inner$survived, predict(model, test_inner,
alpha = hyper_alpha[k],
s = hyper_lambda[k],
type = "response")))
})
best_alpha[j] = hyper_alpha[which.max(tuning_results_inner)]
best_lambda[j] = hyper_lambda[which.max(tuning_results_inner)]
best_auc[j] = max(tuning_results_inner)
}
best_alpha = best_alpha[which.max(best_auc)]
best_lambda = best_lambda[which.max(best_auc)]
model = glmnet(survived~., data = train_outer, alpha = best_alpha, family = "binomial")
results[i, 1] = i
results[i, 2] = best_alpha
results[i, 3] = best_lambda
results[i, 4] = Metrics::auc(test_outer$survived, predict(model, test_outer, s = best_lambda, alpha = best_alpha, type = "response"))
}
print(results)
```
Simple CV:
```{r}
set.seed(42)
data_obs = data_sub[!is.na(data_sub$survived),]
cv = 5
hyper_alpha = runif(20,0, 1)
hyper_lambda = runif(20, 0, 1)
outer_split = as.integer(cut(1:nrow(data_obs), breaks = cv))
results = data.frame(
set = rep(NA, cv),
alpha = rep(NA, cv),
lambda = rep(NA, cv),
AUC = rep(NA, cv)
)
for(i in 1:cv) {
train_outer = data_obs[outer_split != i, ]
test_outer = data_obs[outer_split == i, ]
tuning_results =
sapply(1:length(hyper_alpha), function(k) {
model = glmnet(survived~.,data = train_outer, family = "binomial",alpha = hyper_alpha[k])
return(Metrics::auc(test_outer$survived, predict(model, test_outer,
alpha = hyper_alpha[k],
s = hyper_lambda[k],
type = "response")))
})
best_alpha = hyper_alpha[which.max(tuning_results)]
best_lambda = hyper_lambda[which.max(tuning_results)]
results[i, 1] = i
results[i, 2] = best_alpha
results[i, 3] = best_lambda
results[i, 4] = max(tuning_results)
}
print(results)
```
Predictions:
```{r, results='hide', message=FALSE, warning=FALSE}
prediction_ensemble =
sapply(1:nrow(results), function(i) {
model = glmnet(survived~.,data = data_obs, family = "binomial",alpha = results$alpha[i])
return(predict(model, data_new, alpha = results$alpha[i], s = results$lambda[i], type = "response")[,1])
})
# Single predictions from the model with the highest AUC:
write.csv(data.frame(y = prediction_ensemble[,which.max(results$AUC)]), file = "Max_titanic_best_model.csv")
# Single predictions from the ensemble model:
write.csv(data.frame(y = apply(prediction_ensemble, 1, mean)), file = "Max_titanic_ensemble.csv")
```
`r unhide()`
:::
## Machine learning frameworks
As we have seen today, many of the machine learning algorithms are distributed over several packages but the general machine learning pipeline is very similar for all models: feature engineering, feature selection, hyperparameter tuning and cross-validation.
Machine learning frameworks such as `mlr3` or `tidymodels` provide a general interface for the ML pipeline, in particular the training and the hyperparameter tuning with nested CV. They support most ML packages/algorithms.
### mlr3 {#sec-mlr}
The key features of mlr3 are:
- All common machine learning packages are integrated into mlr3, you can easily switch between different machine learning algorithms.
- A common 'language'/workflow to specify machine learning pipelines.
- Support for different cross-validation strategies.
- Hyperparameter tuning for all supported machine learning algorithms.
- Ensemble models.
Useful links:
- <a href="https://mlr3book.mlr-org.com/" target="_blank" rel="noopener">mlr3-book</a> (still in work)
- <a href="https://mlr3.mlr-org.com/" target="_blank" rel="noopener">mlr3 website</a>
- <a href="https://cheatsheets.mlr-org.com/mlr3.pdf" target="_blank" rel="noopener">mlr3 cheatsheet</a>
#### mlr3 - The Basic Workflow
The mlr3 package actually consists of several packages for different tasks (e.g. mlr3tuning for hyperparameter tuning, mlr3pipelines for data preparation pipes). But let's start with the basic workflow:
```{r chunk_chapter4_65, message=FALSE}
library(EcoData)
library(tidyverse)
library(mlr3)
library(mlr3learners)
library(mlr3pipelines)
library(mlr3tuning)
library(mlr3measures)
data(nasa)
str(nasa)
```
Let's drop time, name and ID variable and create a classification task:
```{r chunk_chapter4_66}
data = nasa %>% select(-Orbit.Determination.Date,
-Close.Approach.Date, -Name, -Neo.Reference.ID)
data$Hazardous = as.factor(data$Hazardous)
# Create a classification task.
task = TaskClassif$new(id = "nasa", backend = data,
target = "Hazardous", positive = "1")
```
Create a generic pipeline of data transformation (imputation $\rightarrow$ scaling $\rightarrow$ encoding of categorical variables):
```{r chunk_chapter4_67}
set.seed(123)
# Let's create the preprocessing graph.
preprocessing = po("imputeoor") %>>% po("scale") %>>% po("encode")
# Run the task.
transformed_task = preprocessing$train(task)[[1]]
transformed_task$missings()
```
We can even visualize the preprocessing graph:
```{r chunk_chapter4_68}
preprocessing$plot()
```
To test our model (glmnet) with 10-fold cross-validated, we will do:
- Specify the missing target rows as validation so that they will be ignored.
- Specify the cross-validation, the learner (the machine learning model we want to use), and the measurement (AUC).
- Run (benchmark) our model.
```{r chunk_chapter4_69__mlr1}
set.seed(123)
transformed_task$data()[1,]
transformed_task$set_row_roles((1:nrow(data))[is.na(data$Hazardous)],
"holdout")
cv10 = mlr3::rsmp("cv", folds = 10L)
EN = lrn("classif.glmnet", predict_type = "prob")
measurement = msr("classif.auc")
```
```{r chunk_chapter4_70, eval=FALSE}
result = mlr3::resample(transformed_task,
EN, resampling = cv10, store_models = TRUE)
# Calculate the average AUC of the holdouts.
result$aggregate(measurement)
```
Very cool! Preprocessing + 10-fold cross-validation model evaluation in a few lines of code!
Let's create the final predictions:
```{r chunk_chapter4_71__mlr2, eval=FALSE}
pred = sapply(1:10, function(i) result$learners[[i]]$predict(transformed_task,
row_ids = (1:nrow(data))[is.na(data$Hazardous)])$data$prob[, "1", drop = FALSE])
dim(pred)
predictions = apply(pred, 1, mean)
```
You could now submit the predictions <a href="http://rhsbio7.uni-regensburg.de:8500" target="_blank" rel="noopener">here</a>.
But we are still not happy with the results, let's do some hyperparameter tuning!
#### mlr3 - Hyperparameter Tuning
With mlr3, we can easily extend the above example to do hyperparameter tuning within nested cross-validation (the tuning has its own inner cross-validation).
Print the hyperparameter space of our glmnet learner:
```{r chunk_chapter4_72}
EN$param_set
```
Define the hyperparameter space of the random forest:
```{r chunk_chapter4_73__mlr3}
library(paradox)
EN_pars =
paradox::ParamSet$new(
list(paradox::ParamDbl$new("alpha", lower = 0, upper = 1L),
paradox::ParamDbl$new("lambda", lower = 0, upper = 0.5 )) )
print(EN_pars)
```
To set up the tuning pipeline we need:
- Inner cross-validation resampling object.
- Tuning criterion (e.g. AUC).
- Tuning method (e.g. random or block search).
- Tuning terminator (When should we stop tuning? E.g. after $n$ iterations).
```{r chunk_chapter4_74__mlr4}
set.seed(123)
inner3 = mlr3::rsmp("cv", folds = 3L)
measurement = msr("classif.auc")
tuner = mlr3tuning::tnr("random_search")
terminator = mlr3tuning::trm("evals", n_evals = 5L)
EN = lrn("classif.glmnet", predict_type = "prob")
learner_tuner = AutoTuner$new(learner = EN,
measure = measurement,
tuner = tuner,
terminator = terminator,
search_space = EN_pars,
resampling = inner3)
print(learner_tuner)
```
Now we can wrap it normally into the 10-fold cross-validated setup as done previously:
```{r chunk_chapter4_75, echo=FALSE, results='hide'}
set.seed(123)
outer3 = mlr3::rsmp("cv", folds = 3L)
result = mlr3::resample(transformed_task, learner_tuner,
resampling = outer3, store_models = TRUE)
```
```{r}
# Calculate the average AUC of the holdouts.
result$aggregate(measurement)
```
Let's create the final predictions:
```{r chunk_chapter4_76, eval=FALSE}
pred = sapply(1:3, function(i) result$learners[[i]]$predict(transformed_task,
row_ids = (1:nrow(data))[is.na(data$Hazardous)])$data$prob[, "1", drop = FALSE])
dim(pred)
predictions = apply(pred, 1, mean)
```
## Exercises
::: {.callout-caution icon="false"}
#### Question: Use mlr3 for the titanic dataset
1. Use `mlr3` to tune glmnet for the titanic dataset using nested CV
2. Submit single predictions and multiple predictions
If you need help, take a look at the solution, go through it line by line and try to understand it.
`r hide("Click here to see the solution")`
Prepare data
```{r}
data = titanic_ml %>% select(-name, -ticket, -name, -body)
data$pclass = as.factor(data$pclass)
data$sex = as.factor(data$sex)
data$survived = as.factor(data$survived)
# Change easy things manually:
data$embarked[data$embarked == ""] = "S" # Fill in "empty" values.
data$embarked = droplevels(as.factor(data$embarked)) # Remove unused levels ("").
data$cabin = (data$cabin != "") * 1 # Dummy code the availability of a cabin.
data$fare[is.na(data$fare)] = mean(data$fare, na.rm = TRUE)
levels(data$home.dest)[levels(data$home.dest) == ""] = "unknown"
levels(data$boat)[levels(data$boat) == ""] = "none"
# Create a classification task.
task = TaskClassif$new(id = "titanic", backend = data,
target = "survived", positive = "1")
task$missings()
# Let's create the preprocessing graph.
preprocessing = po("imputeoor") %>>% po("scale") %>>% po("encode")
# Run the task.
transformed_task = preprocessing$train(task)[[1]]
transformed_task$set_row_roles((1:nrow(data))[is.na(data$survived)], "holdout")
```
Hyperparameter tuning:
```{r, results='hide'}
cv10 = mlr3::rsmp("cv", folds = 10L)
inner3 = mlr3::rsmp("cv", folds = 3L)
measurement = msr("classif.auc")
tuner = mlr3tuning::tnr("random_search")
terminator = mlr3tuning::trm("evals", n_evals = 5L)
EN = lrn("classif.glmnet", predict_type = "prob")
EN_pars =
paradox::ParamSet$new(
list(paradox::ParamDbl$new("alpha", lower = 0, upper = 1L),
paradox::ParamDbl$new("lambda", lower = 0, upper = 0.5 )) )
learner_tuner = AutoTuner$new(learner = EN,
measure = measurement,
tuner = tuner,
terminator = terminator,
search_space = EN_pars,
resampling = inner3)
result = mlr3::resample(transformed_task, learner_tuner,
resampling = cv10, store_models = TRUE)
```
Evaluation:
```{r}
measurement = msr("classif.auc")
result$aggregate(measurement)
```
Predictions:
We can extract a learner with optimized hyperparameters:
```{r}
model = result$learners[[1]]$learner$clone()
model$param_set$values
```
And we can fit it then on the full data set:
```{r}
model$train(transformed_task)
predictions = model$predict(transformed_task, row_ids = transformed_task$row_roles$holdout)
predictions = predictions$prob[,1]
head(predictions)
```
And submit to http://rhsbio7.uni-regensburg.de:8500
```{r}
write.csv(data.frame(y = predictions), file = "glmnet.csv")
```
`r unhide()`
:::