unsupervised classification with R

unsupervised classification with R

m

January 29, 2016

Here we see three simple ways to perform an unsupervised classification on a raster dataset in R. I will show these approaches, but first we need to load the relevant packages and the actual data. You could use the Landsat data used in the “Remote Sensing and GIS for Ecologists” book which can be downloaded here.

library("raster")  
library("cluster")
library("randomForest")

# loading the layerstack  
# here we use a subset of the Landsat dataset from "Remote Sensing and GIS for Ecologists" 
image <- stack("path/to/raster")
plotRGB(image, r=3,g=2,b=1,stretch="hist")

RGBimage

Now we will prepare the data for the classifications. First we convert the raster data in a matrix, then we remove the NA-values.

## returns the values of the raster dataset and write them in a matrix. 
v <- getValues(image)
i <- which(!is.na(v))
v <- na.omit(v)

The first classification method is the well-known k-means method. It separates n observations into  k clusters. Each observation belongs to the cluster with the nearest mean.

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

Kmeans

The second classification method is called clara (Clustering for Large Applications). It work by clustering only a sample of the dataset and then assigns all object in the dataset to the clusters.

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

clara

The third method uses a random Forest model to calculate proximity values. These values were clustered using k-means. The clusters are used to train another random Forest model for classification.

## 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, 12, iter.max = 100, nstart = 10)
rf <- randomForest(vx,as.factor(E_rf$cluster),ntree = 500)
rf_raster<- predict(image,rf)
plot(rf_raster)

randomForest

The three classifications are stacked into one layerstack and plotted for comparison.

class_stack <- stack(kmeans_raster,clara_raster,rf_raster)
names(class_stack) <- c("kmeans","clara","randomForest")

plot(class_stack)

Comparing the three classifications:

Looking at the different classifications we notice, that the kmeans and clara classifications have only minor differences.
The randomForest classification shows a different image.

 

want to read more about R and classifications? check out this book:

follow us and share it on:

you may also like:

A reply to the Programming Hero Syndrome post

A reply to the Programming Hero Syndrome post

A while back there was a post about the "Programming Hero Syndrome." This is a small extension to it, from a female perspective to provide a different view. When I started EAGLE, in those first weeks and months, I'd join internship talks or MSc defenses, listen to...

Getting Ready for 25 + 10

Getting Ready for 25 + 10

Next week's the big one. Twenty five years of EORC and ten years of EAGLE, all in one event, and we're expecting around 250 people through the door. Sabine Oppmann's been leading the prep meetings, and we've split things into clear teams: setup, catering, AV and...

Dr. Ariane Droin in front of the camera: RTL reports on HABITRACK

Dr. Ariane Droin in front of the camera: RTL reports on HABITRACK

A film crew from RTL showed up in the Oberpfalz this spring, and joint a field campaign by the Habitrack BMFTR funded project. Dr. Ariane Droin, who leads the Würzburg side of the MONID HABITRACK project at EORC, walked the RTL team through what the project is...

A Weekend together: Where the Institute Talks About Itself

A Weekend together: Where the Institute Talks About Itself

Once a year, or at least that's the idea, the Institute of Geography packs up and heads out to a remote place, this time Burg Rothenfels, an old castle not far from Würzburg, to spend a weekend doing something that almost never happens in the day to day grind:...

Graduation Day: EORC and EAGLE Celebrate at the Faculty Ceremony

Graduation Day: EORC and EAGLE Celebrate at the Faculty Ceremony

Last week the faculty held its formal graduation ceremony, and EORC and EAGLE were well represented on stage. Two of our PhD students, Dr. Ariane Droin and Dr. Johannes Mast, both joint the ceremony for their doctoral degrees, and it was great to see them walk across...

Share This