mlr-org / mlr3spatial

Spatial objects within the mlr3 ecosystem
https://mlr3spatial.mlr-org.com
42 stars 6 forks source link

Support for plotting probabilities in predict_spatial #88

Closed jpconnel closed 1 year ago

jpconnel commented 1 year ago

Is it possible to plot probabilities instead of responses for classif tasks using predict_spatial?

be-marc commented 1 year ago

You can try the prob branch.

remotes::install_github("mlr-org/mlr3spatial@prob")

This works. Only binary tasks.

library(mlr3spatial)
library(sf)
library(terra)

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

# probability for forest
leipzig$forest = as.factor(ifelse(leipzig$land_cover == "forest", 1, 0))
leipzig$land_cover = NULL

task = as_task_classif_st(leipzig, target = "forest", positive = "1")
task

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

learner = lrn("classif.rpart", predict_type = "prob")
learner$train(task)

land_cover = predict_spatial(leipzig_raster, learner, predict_type = "prob")

plot(land_cover, type = "continuous")
jpconnel commented 1 year ago

Thank you! :)

jpconnel commented 1 year ago

Hello! Just wanted to let you know the probbranch was not working for me, I believe because my area of interest was masked (my masked area of interest produced correct results on the main branch for response).

I believe this is because the predictions lengths for responseand probare of different lengths (response prediction data includes NA's, while probdoes not). I modified the existing main branch for prob plotting in a forked repository by replacing non-na values in the responseprediction with results from the probprediction. However, I'm sure there is a better solution out there