Я пытаюсь создать растр, который показывает, где предсказание случайного леса и предсказание нейронной сети согласуются и не согласуются. Каждый растр является предсказанием 6 классов земель, и я хочу создать новый растр, который представляет собой сравнение между двумя, где ячейки согласованных предсказаний зеленые, а ячейки несогласных предсказаний красные.
Вот мой код для двух методов прогнозирования и их растров:
trainD <- dataAll[dataAll$sampleType == "train",]
validD <- dataAll[dataAll$sampleType == "valid",]
# Random Forest
# use 'Caret' package to find the optimal parameter settings
tc <- trainControl(method = "repeatedcv",
number = 10, # number 10 fold
repeats = 10) # number of repeats
rf.grid <- expand.grid(mtry=1:sqrt(9)) # use 9 bc we have 9 bands
# train the random forest model to the Sentinel-2 data through Caret
trainD <- na.omit(trainD) #omit points in cloud areas from training points
rf_model <- caret::train(x = trainD[,c(5:13)], #digital number data
y = as.factor(trainD$landcID),
method = "rf",
metric="Accuracy",
trainControl = tc, #use parameter tuning
tuneGrid = rf.grid) #parameter tuning grid
#check output
rf_model
# Change name in raster stack to match training data
names(allbandsCloudf) <- c("B2","B3","B4","B5","B6","B7","B8","B11","B12")
# Apply the random forest model to the Sentinel-2 data
rf_prediction <- raster::predict(allbandsCloudf, model=rf_model)
#view predictions
plot(rf_prediction)
# landcover class names
landclass
# set up categorical colors for each class using hex codes
landclass$cols <-c("#a6d854","#8da0cb","#66c2a5",
"#fc8d62","#ffffb3","#ffd92f")
# make plot and hide legend
plot(rf_prediction, #random forest prediction
breaks=seq(0,6), #number of landclasses
col=landclass$cols ,
legend=FALSE, axes=FALSE) #hide legend
# Neural Networks
# set up grid
nnet.grid <- expand.grid(size = seq(from = 16, to = 28, by = 2),
decay = seq(from = 0.1, to = 0.6, by = 0.1))
# train the model
nnet_model <- caret::train(x = trainD[,c(5:13)],
y = as.factor(trainD$landcID),
method = "nnet",
metric= "Accuracy",
trainControl = tc,
tuneGrid = nnet.grid,
trace=FALSE)
# view the training summary
nnet_model
# apply the neural network model to the Sentinel-2 data
nnet_prediction <- raster::predict(allbandsCloudf, model=nnet_model)
# make plot and hide legend
plot(nnet_prediction, #plot the neural network predictions
breaks=seq(0,6), #number of landclasses
col=landclass$cols ,
legend=FALSE) #hide the legend