Skip to content

feat: add probability prediction to predict_spatial()#111

Merged
be-marc merged 3 commits into
mainfrom
feat/predict-spatial-prob
Jul 14, 2026
Merged

feat: add probability prediction to predict_spatial()#111
be-marc merged 3 commits into
mainfrom
feat/predict-spatial-prob

Conversation

@be-marc

@be-marc be-marc commented Jul 14, 2026

Copy link
Copy Markdown
Member

Closes #88
Closes #89

Summary

Reimplementation of #89 on current main. predict_spatial() now supports probability predictions. Instead of adding a predict_type argument, the prediction type is taken from the $predict_type of the learner, so a mismatch between argument and learner state is impossible.

  • LearnerClassifSpatial mirrors the wrapped learner's $predict_type and fills a full-size probability matrix with NaN rows for incomplete observations. Only the column names are carried over, which fixes the dimension error with masked rasters reported in Support for plotting probabilities in predict_spatial #88.
  • For rasters, the target raster is initialized with one layer per class and terra::writeValues() receives the full probability matrix, so multiclass tasks are supported. Layers are named after the class levels, and the categorical-labels step is skipped for probability output.
  • For sf vectors, one probability column per class is returned. The vector branch now uses $predict_newdata() instead of $predict() because predicting probabilities directly on an unsupervised task fails mlr3's prediction-data check.
  • The internal spatial learner wrappers now use the id of the wrapped learner instead of the hardcoded classif.ranger / regr.ranger.

Verification

library(mlr3spatial)
library(terra, exclude = "resample")

task_train = tsk("leipzig")
learner = lrn("classif.rpart", predict_type = "prob")
learner$train(task_train)

stack = rast(system.file("extdata", "leipzig_raster.tif", package = "mlr3spatial"))
pred = predict_spatial(stack, learner, chunksize = 1L)
pred # SpatRaster with layers forest, pasture, urban, water
plot(pred)

🤖 Generated with Claude Code

be-marc and others added 2 commits July 13, 2026 13:16
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The prediction type is taken from the predict_type of the learner.
For classification learners with predict_type "prob",
predict_spatial() returns one raster layer or one vector column per class.
Works with multiclass tasks and rasters containing missing values.

Closes #88
Closes #89

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@be-marc
be-marc merged commit 5a2ed58 into main Jul 14, 2026
5 checks passed
@be-marc
be-marc deleted the feat/predict-spatial-prob branch July 14, 2026 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for plotting probabilities in predict_spatial

1 participant