-
-
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 |
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?
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.
Why it does not specify the number of trees?
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?
Thank you for this post