-1

ML beginner here, so apologies for any wrong terminology.

I have two questions. Firstly, is it possible to add an additional "unknown" class, when the probability of any one class is very low? And, secondly, how can I return the probability values for each cell for each class as a layer?

Here's a reprex and shortened version of the code I'm running:

library(terra, exclude = "resample") # working with rasters
library(sf) # working with vectors

library(mlr3verse) # machine learning - lazy load of most mlr3 packages
library(mlr3spatiotempcv) # spatial resampling methods
library(mlr3spatial)

leipzig = read_sf(system.file("extdata", "leipzig_points.gpkg", package = "mlr3spatial"), stringsAsFactors = TRUE)

leipzig_raster = rast(system.file("extdata", "leipzig_raster.tif", package = "mlr3spatial"))

task = as_task_classif_st(leipzig, 
                          id = "veg_class", 
                          target = "land_cover", 
                          backend = leipzig)

resample_method = rsmp("repeated_spcv_coords", folds = 10)

rf_ranger_lrn = lrn("classif.ranger", predict_type = "prob", importance = "impurity")

resample_rf = resample(task = task,
                       learner = rf_ranger_lrn,
                       resampling = resample_method,
                       store_models = FALSE)


rf_ranger_lrn$train(task)


classes <- terra::predict(leipzig_raster, rf_ranger_lrn, na.rm = TRUE)
pbengou
  • 13
  • 3

1 Answers1

0

Firstly, is it possible to add an additional "unknown" class, when the probability of any one class is very low?

You should do this in post-processing with the finished raster stack.

And, secondly, how can I return the probability values for each cell for each class as a layer?

You can do it like this. However, this method requires to load the complete raster into memory. mlr3spatial currently does not support this.

library(mlr3verse)
library(mlr3spatial)
library(mlr3misc)
library(sf)
library(terra)

leipzig = read_sf(system.file("extdata", "leipzig_points.gpkg", package = "mlr3spatial"), stringsAsFactors = TRUE)

leipzig_raster = rast(system.file("extdata", "leipzig_raster.tif", package = "mlr3spatial"))

task = as_task_classif_st(leipzig, 
                          id = "veg_class", 
                          target = "land_cover", 
                          backend = leipzig)

rf_ranger_lrn = lrn("classif.ranger", predict_type = "prob")

rf_ranger_lrn$train(task)

newdata = as.data.table(values(leipzig_raster))

pred = rf_ranger_lrn$predict_newdata(newdata, task)

walk(task$class_names, function(class) {
  raster = rast(
    ext(leipzig_raster), 
    resolution = res(leipzig_raster), 
    crs = crs(leipzig_raster), 
    vals = pred$data$prob[, class],
    names = class)
   add(leipzig_raster) = raster 
})

plot(leipzig_raster)
be-marc
  • 1,276
  • 5
  • 5