如何对R中的非监督和监督分类进行验证?

时间:2020-06-15 16:18:51

标签: r validation classification raster

我使用以下代码对栅格进行了有监督和无监督的分类:

#UNSUPERVISED CLASSIFICATION
library(raster)  
library(cluster)
library(randomForest)
library(rpart)
image <- stack('lt05.tif')
plotRGB(image, r=3,g=2,b=1,stretch="hist")

v <- getValues(image)
i <- which(!is.na(v))
v <- na.omit(v)
set.seed(99)

## kmeans classification 
E <- kmeans(v, 5, iter.max = 100, nstart = 5)
kmeans_raster <- raster(image)
kmeans_raster[i] <- E$cluster
plot(kmeans_raster)

## clara classification 
clus <- clara(v,5,samples=100,metric="manhattan",pamLike=T)
clara_raster <- raster(image)
clara_raster[i] <- clus$clustering
plot(clara_raster)

## unsupervised randomForest classification using kmeans
vx<-v[sample(nrow(v), 500),]
rf = randomForest(vx)
rf_prox <- randomForest(vx,ntree = 1000, proximity = TRUE)$proximity

E_rf <- kmeans(rf_prox, 5, iter.max = 100, nstart = 5)
rf <- randomForest(vx,as.factor(E_rf$cluster),ntree = 500)
rf_raster<- predict(image,rf)
plot(rf_raster)

#SUPERVISED CLASSIFICATION
library(RStoolbox)
# shapefile with training
shp<-shapefile("to5/to.shp")
## Fit classifier (splitting training into 70\% training data, 30\% validation data)
SC_rf <- superClass(landsat5, shp, responseCol = "MC_ID", nSamples = 1000, model = "rf", 
                    tuneLength = 5, kfold = 5, mode = "classification", predType = "raw")
SC_rf

SC_mlc <- superClass(landsat5, shp, responseCol = "MC_ID", nSamples = 1000, model = "mlc", 
                     tuneLength = 5, kfold = 5, mode = "classification", predType = "raw")
SC_mlc

SC_knn <- superClass(landsat5, shp, responseCol = "MC_ID", nSamples = 1000, model = "knn", 
                                tuneLength = 5, kfold = 5, mode = "classification", predType = "raw")
SC_knn

现在,我需要对分类栅格进行验证。我尝试使用以下代码执行此操作:

#UNSUPERVISED VALIDATION
kmeans_clara<- overlay(kmeans_raster, clara_raster, fun=function(r1, r2){return(r1-r2)})
clara_kmeans<- overlay(clara_raster, kmeans_raster, fun=function(r1, r2){return(r1-r2)})
rf_kmeans<- overlay(rf_raster, kmeans_raster, fun=function(r1, r2){return(r1-r2)})
kmeans_rf<- overlay(kmeans_raster, rf_raster, fun=function(r1, r2){return(r1-r2)})
clara_rf<- overlay(clara_raster, rf_raster, fun=function(r1, r2){return(r1-r2)})
rf_clara<- overlay(rf_raster, clara_raster, fun=function(r1, r2){return(r1-r2)})

reclass_df <- c(-4, 0, NA,
                0, 4, 1)
reclass_df
# reshape the object into a matrix with columns and rows
reclass_m <- matrix(reclass_df,
                    ncol = 3,
                    byrow = TRUE)

# reclassify the raster using the reclass object - reclass_m
kmeans_classified <- reclassify(kmeans_clara, reclass_m)

# view reclassified data
barplot(kmeans_classified,main = "Number of pixels in each class")

plot(kmeans_classified)
# assign all pixels that equal 0 to NA or no data value
kmeans_clara[kmeans_clara == 0] <- NA
# plot reclassified data
plot(kmeans_clara)

# reclassify the raster using the reclass object - reclass_m
clara_classified <- reclassify(clara_kmeans, reclass_m)

# view reclassified data
barplot(clara_classified,main = "Number of pixels in each class")

plot(clara_classified)
# assign all pixels that equal 0 to NA or no data value
clara_kmeans[clara_kmeans == 0] <- NA
# plot reclassified data
plot(clara_kmeans)

# reclassify the raster using the reclass object - reclass_m
rf_classified <- reclassify(rf_kmeans, reclass_m)

# view reclassified data
barplot(rf_classified,main = "Number of pixels in each class")

plot(rf_classified)
# assign all pixels that equal 0 to NA or no data value
rf_kmeans[rf_kmeans == 0] <- NA
# plot reclassified data
plot(rf_kmeans)

# reclassify the raster using the reclass object - reclass_m
kmeansrf_classified <- reclassify(kmeans_rf, reclass_m)

# view reclassified data
barplot(kmeansrf_classified,main = "Number of pixels in each class")

plot(kmeansrf_classified)
# assign all pixels that equal 0 to NA or no data value
kmeans_rf[kmeans_rf == 0] <- NA
# plot reclassified data
plot(kmeans_rf)


# reclassify the raster using the reclass object - reclass_m
clararf_classified <- reclassify(clara_rf, reclass_m)

# view reclassified data
barplot(clararf_classified,main = "Number of pixels in each class")

plot(clararf_classified)
# assign all pixels that equal 0 to NA or no data value
clara_rf[clara_rf == 0] <- NA
# plot reclassified data
plot(clara_rf)


# reclassify the raster using the reclass object - reclass_m
rfclara_classified <- reclassify(rf_clara, reclass_m)

# view reclassified data
barplot(rfclara_classified,main = "Number of pixels in each class")

plot(rfclara_classified)
# assign all pixels that equal 0 to NA or no data value
rf_clara[rf_clara == 0] <- NA
# plot reclassified data
plot(rf_clara)

a<-summary(kmeans_clara)
b<-summary(kmeans_rf)
c<-summary(clara_kmeans)
d<-summary(clara_rf)
e<-summary(rf_kmeans)
f<-summary(rf_clara)
s <- cbind(sum=c("Min.", "First Qu.", "Median", "Third Qu.", "Max.", "NA"), kmeans_clara=c(a),kmeans_rf=c(b),clara_kmeans=c(c),clara_rf=c(d),rf_kmeans=c(e),rf_clara=c(f)) 
s

#SUPERVISED CLASSIFICATION

## Cross-validation based 
a<-getValidation(SC_mlc, from = "cv")
b<-getValidation(SC_rf, from = "cv")
c<-getValidation(SC_knn, from = "cv")

s <- cbind(MLC=c(a),RF=c(b),KNN=c(c)) 
s
o<- data.frame(s)
o

首先,我不知道无监督验证是否可以...只是栅格减法,它可以显示将多少像素归为同一类别。

对于监督分类,它显示出不好的结果: enter image description here

因此,我正在寻求帮助以及有关如何正确执行此操作的任何想法。

0 个答案:

没有答案