Image classification (raster stack) with random forest (packet relay)

I set a random forest using R-Ranger to classify a bitmap. The prediction function causes an error, and in the future I provide a reproducible example.

library(raster)
library(nnet)
library(ranger)
data(iris)

# put iris data into raster
r<-list()
for(i in 1:4){
  r[[i]]<-raster(nrows=10, ncols=15)
  r[[i]][]<-iris[,i]
}
r<-stack(r)
names(r)<-names(iris)[1:4]

# multinom (an example that works)
nn.model <- multinom(Species ~ ., data=iris, trace=F)
nn.pred<-predict(r,nn.model)

# ranger (doesn't work)
ranger.model<-ranger(Species ~ ., data=iris)   
ranger.pred<-predict(r,ranger.model)

Indicated error

Error in v [cells,] <- predv: wrong number of indices on the matrix

although the error is with my real data

Error in p [-naind,] <- predv: the number of elements to replace is not a multiplicity of the replacement length

The only thing that comes to my mind is that the ranger.prediction object includes several elements other than predictive interests. Anyway, how could a ranger be used to predict in the raster stack?

+7
3

, :

library(caret)
ranger.model<-train(Species ~ ., data=iris,method = "ranger")  
ranger.pred<-predict(r,ranger.model)

, , type = 'se'. , , :

https://cran.r-project.org/web/packages/raster/vignettes/functions.pdf

# Function to predict standard errors on a raster
predfun <- function(x, model, type, filename)
{
  out <- raster(x)
  bs <- blockSize(out)
  out <- writeStart(out, filename, overwrite=TRUE)
  for (i in 1:bs$n) {
    v <- getValues(x, row=bs$row[i], nrows=bs$nrows[i])
    nas<-apply(v,1,function(x) sum(is.na(x)))
    p<-numeric(length = nrow(v))
    p[nas > 0]<-NA
    p[nas == 0]<-predict(object = model,
                         v[nas == 0,],
                     type = 'se')$se
    out <- writeValues(out, p, bs$row[i])
  }
  out <- writeStop(out)
  return(out)
}

# New ranger model 
ranger.model<-ranger(Species ~ .
                     , data=iris
                     , probability=TRUE
                     , keep.inbag=TRUE
                     )
# Run predictions
se<-predfun(r
            , model = ranger.model
            , type = "se"
            , filename = paste0(getwd(),"/se.tif")
            )
+2

:

pacman::p_load(raster, nnet, ranger)

data(iris)

# put iris data into raster
r<-list()
for(i in 1:4){
  r[[i]]<-raster(nrows=10, ncols=15)
  r[[i]][]<-iris[,i]
}
r<-stack(r)
names(r)<-names(iris)[1:4]

# multinom (an example that works)
nn.model <- multinom(Species ~ ., data=iris, trace=F)
nn.pred <- predict(r,nn.model)  # predict(object, newdata, type = c("raw","class"), ...)

# ranger (doesn't work)
ranger.model <- ranger(Species ~ ., data=iris)   
ranger.pred <- predict(ranger.model, as.data.frame(as.matrix(r)))

as.data.frame(as.matrix(r)) !

: , , ...

identical(iris$Species, ranger.pred$predictions)
+4

randomForest ,

library(randomForest)
rf.model<-randomForest(Species ~ ., data=iris)   
rf.pred<-predict(r,rf.model)
+2

All Articles