训练、测试与验证集划分

Tidyverse 中的机器学习

Dmitriy (Dima) Gorenshteyn

Lead Data Scientist, Memorial Sloan Kettering Cancer Center

训练-测试划分

Tidyverse 中的机器学习

训练-测试划分

Tidyverse 中的机器学习

训练-测试划分

Tidyverse 中的机器学习

initial_split()

library(rsample)
gap_split <- initial_split(gapminder, prop = 0.75)

training_data <- training(gap_split) testing_data <- testing(gap_split)
nrow(training_data)
3003
nrow(testing_data)
1001
Tidyverse 中的机器学习

训练-验证划分

Tidyverse 中的机器学习

训练-验证划分

Tidyverse 中的机器学习

交叉验证

Tidyverse 中的机器学习

vfold_cv()

library(rsample)
cv_split <- vfold_cv(training_data, v = 3)

cv_split
#  3-fold cross-validation 
# A tibble: 3 x 2
  splits        id   
  <list>        <chr>
1 <S3: rsplit>  Fold1
2 <S3: rsplit>  Fold2
3 <S3: rsplit>  Fold3
Tidyverse 中的机器学习

映射 train 与 validate

cv_data <- cv_split %>% 
  mutate(train = map(splits, ~training(.x)),
         validate = map(splits, ~testing(.x)))
Tidyverse 中的机器学习

交叉验证模型

head(cv_data)
# A tibble: 3 x 4
  splits       id    train                validate            
* <list>       <chr> <list>               <list>              
1 <S3: rsplit> Fold1 <tibble [2,002 × 7]> <tibble [1,001 × 7]>
2 <S3: rsplit> Fold2 <tibble [2,002 × 7]> <tibble [1,001 × 7]>
3 <S3: rsplit> Fold3 <tibble [2,002 × 7]> <tibble [1,001 × 7]>
cv_models_lm <- cv_data %>% 
  mutate(model = map(train, ~lm(formula = life_expectancy~., data = .x)))
Tidyverse 中的机器学习

Vamos praticar!

Tidyverse 中的机器学习

Preparing Video For Download...