Instantly share code, notes, and snippets.

Embed
What would you like to do?
##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

This comment has been minimized.

RIRABED commented Feb 18, 2017

Thank you for this post

@riadhft

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

huynguyen1139 commented Nov 14, 2018

Why it does not specify the number of trees?

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