Skip to content

Instantly share code, notes, and snippets.

@amsantac
Last active August 20, 2022 21:14
Show Gist options
  • Save amsantac/5183c0c71a8dcbc27a4f to your computer and use it in GitHub Desktop.
Save amsantac/5183c0c71a8dcbc27a4f to your computer and use it in GitHub Desktop.
##img=raster
##trainData=vector
##responseCol=field trainData
##nsamples=number 1000
##output=output raster
library(sp)
library(raster)
library(caret)
names(img) <- paste0("B", c(1:5, 7))
dfAll = data.frame(matrix(vector(), 0, length(names(img)) + 1))
for (i in 1:length(unique(trainData[[responseCol]]))){
category <- unique(trainData[[responseCol]])[i]
categorymap <- trainData[trainData[[responseCol]] == category,]
dataSet <- extract(img, categorymap)
dataSet <- dataSet[!unlist(lapply(dataSet, is.null))]
dataSet <- lapply(dataSet, function(x){cbind(x, class = as.numeric(rep(category, nrow(x))))})
df <- do.call("rbind", dataSet)
dfAll <- rbind(dfAll, df)
}
sdfAll <- dfAll[sample(1:nrow(dfAll), nsamples), ]
modFit_rf <- train(as.factor(class) ~ B3 + B4 + B5, method = "rf", data = sdfAll)
beginCluster()
preds_rf <- clusterR(img, raster::predict, args = list(model = modFit_rf))
endCluster()
output = preds_rf
@RIRABED
Copy link

RIRABED commented Feb 18, 2017

Thank you for this post

@riadhft
Copy link

riadhft commented Apr 12, 2017

Hello, I am doing my M.Sc. thesis with Sentinel 2 dataset. I want to do random forest classification by running your code in QGIS. But the problem is I dont understand the meaning of this line:
c(paste0("B", 1:5, coll = ""), "B7").
Can you help me?

@riadhft
Copy link

riadhft commented Apr 12, 2017

I have just edit the code like following:
##img=raster
##trainData=vector
##responseCol=field trainData
##nsamples=number 1000
##output=output raster
library(sp)
library(raster)
library(caret)

names(img) <- c(paste0("B", 2:8, coll = ""), "B8A","B11", "B12")

dfAll = data.frame(matrix(vector(), 0, length(names(img)) + 1))

for (i in 1:length(unique(trainData[[responseCol]]))){
category <- unique(trainData[[responseCol]])[i]
categorymap <- trainData[trainData[[responseCol]] == category,]
dataSet <- extract(img, categorymap)
dataSet <- lapply(dataSet, function(x){cbind(x, class = as.numeric(rep(category, nrow(x))))})
df <- do.call("rbind", dataSet)
dfAll <- rbind(dfAll, df)
}

sdfAll <- subset(dfAll[sample(1:nrow(dfAll), nsamples), ])

modFit_rf <- train(as.factor(class) ~., method = "rf", data = sdfAll)

beginCluster()
preds_rf <- clusterR(img, raster::predict, args = list(model = modFit_rf))
endCluster()

output = preds_rf
But, after running I have a message the output file does not created correctly.

@huynguyen1139
Copy link

Why it does not specify the number of trees?

@liaduarte
Copy link

liaduarte commented Feb 20, 2019

Hello.

Sorry for the message but I see the error.

Now, I just don't understand why the input is a raster (multispectral) and then is interprets the bands, because in my case it gives an error in "names(img) <- paste0("B", c(1:5, 7))" and I put the number of bands that I have in the composite image. However I always get an error in this line. Is the name that is wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment