Created
          September 10, 2021 06:35 
        
      - 
      
 - 
        
Save pat-s/ae290bd6dd8c2970c7aa0baf200483c4 to your computer and use it in GitHub Desktop.  
    OpenDataScience Europe 2021 talk - Patrick Schratz
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | ## ----mlr3-config, echo = FALSE--------------------------------------------------------------------------- | |
| lgr::get_logger("bbotk")$set_threshold("warn") | |
| lgr::get_logger("mlr3")$set_threshold("warn") | |
| ## ----example, fig.show="hide"---------------------------------------------------------------------------- | |
| library("mlr3verse", quietly = TRUE) | |
| set.seed(42) | |
| # example tasks | |
| tasks <- tsks(c("iris", "german_credit")) | |
| # from {mlr3learners} | |
| learners <- lrns(c("classif.rpart", | |
| "classif.ranger")) | |
| # run a cross-val | |
| bmg <- benchmark_grid( | |
| tasks, learners, | |
| rsmp("cv") | |
| ) | |
| bmr <- benchmark(bmg) | |
| # visualize by classification error | |
| autoplot(bmr, measure = msr("classif.ce")) | |
| ## ----mlr3spatial-ex-1------------------------------------------------------------------------------------ | |
| library("mlr3") | |
| library("mlr3learners") | |
| library("mlr3spatial") | |
| tif <- system.file("tif/L7_ETMs.tif", | |
| package = "stars" | |
| ) | |
| l7data <- stars::read_stars(tif) | |
| # create mlr3 backend from sf data | |
| backend <- as_data_backend(l7data) | |
| ## ----mlr3spatial-ex-2------------------------------------------------------------------------------------ | |
| # create a "Random Forest" learner and train it | |
| learner <- lrn("regr.ranger") | |
| task <- as_task_regr(backend, target = "layer.1") | |
| rows_train <- sample(1:task$nrow, 1000) | |
| rows_pred <- setdiff(1:task$nrow, rows_train) | |
| learner$train(task, row_ids = rows_train) | |
| ## ----mlr3spatial-ex-3, warning=FALSE, results=FALSE------------------------------------------------------ | |
| # set the output file and predict with the learner | |
| pred <- predict_spatial(task, learner, format = "stars") | |
| ## ----mlr3spatial-ex-31----------------------------------------------------------------------------------- | |
| pred | |
| ## ----mlr3spatial-ex-4, out.width="50%", fig.align='center'----------------------------------------------- | |
| plot(pred, col = c("#440154FF", "#443A83FF", "#31688EFF", | |
| "#21908CFF", "#35B779FF", "#8FD744FF", "#FDE725FF")) | |
| ## ----mlr3spatiotempcv-ex--------------------------------------------------------------------------------- | |
| library("mlr3spatiotempcv") | |
| # create 'sf' object from example data | |
| data_sf <- sf::st_as_sf(ecuador, coords = c("x", "y"), crs = 32717) | |
| ## ---- echo=FALSE, out.width="100%", out.height="75%"----------------------------------------------------- | |
| mapview::mapview(data_sf) | |
| ## ----mlr3spatiotempcv-ex11------------------------------------------------------------------------------- | |
| # create ClassifST task | |
| task <- TaskClassifST$new("ecuador_sf", backend = data_sf, | |
| target = "slides", positive = "TRUE" | |
| ) | |
| print(task) | |
| ## ----mlr3spatiotempcv-ex2-------------------------------------------------------------------------------- | |
| library("mlr3learners") | |
| library("ranger") | |
| task <- tsk("ecuador") | |
| learner <- lrn("classif.ranger", predict_type = "prob") | |
| resampling_sp <- rsmp("repeated_spcv_coords", | |
| folds = 4, repeats = 2 | |
| ) | |
| rr_sp <- resample( | |
| task = task, | |
| learner = learner, | |
| resampling = resampling_sp | |
| ) | |
| rr_sp$aggregate(measures = msr("classif.ce")) | |
| ## ----mlr3spatiotempcv-ex3, fig.retina=1, dpi=150, fig.dim=c(8, 3)---------------------------------------- | |
| autoplot(resampling_sp, task, fold_id = c(1:2), size = 0.7) * | |
| ggplot2::scale_y_continuous(breaks = seq(-3.97, -4, -0.01)) * | |
| ggplot2::scale_x_continuous(breaks = seq(-79.06, -79.08, -0.01)) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment