Predict on Spatial Objects with mlr3 LearnersSource:
This function allows to directly predict mlr3 learners on various spatial objects.
stars::stars| sf::sf |
raster::RasterBrick). New data to predict on. All spatial data formats convertible by
as_data_backend()are supported e.g. terra::SpatRaster or sf::sf.
(Learner). Learner with trained model.
The chunksize determines in how many subparts the prediction task will be split into. The value can be roughly thought of as megabyte of a raster file on disk. For example, if a prediction on a 1 GB file would be carried out with
chunksize = 100L, the prediction would happen in 10 chunks.
The default of
chunksize = 1000Lmight be a good compromise between speed and memory usage. If you find yourself running out of memory, reduce this value.
Output class of the resulting object. Accepted values are
"terra"if the input is a raster. Note that when choosing something else than
"terra", the spatial object is converted into the respective format which might cause overhead both in runtime and memory allocation. For vector data only
Path where the spatial object should be written to.
library(terra, exclude = "resample") #> terra 1.7.29 # fit rpart on training points task_train = tsk("leipzig") learner = lrn("classif.rpart") learner$train(task_train) # load raster stack = rast(system.file("extdata", "leipzig_raster.tif", package = "mlr3spatial")) # predict land cover classes pred = predict_spatial(stack, learner, chunksize = 1L)