unsupervised classification with R

unsupervised classification with R


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.


# 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")


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


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


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)


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")


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:

you may also like:

New staff member Luisa Pflumm

New staff member Luisa Pflumm

Luisa Pflumm joined the Earth Observation Research Cluster in May 2024 as part of the EcoGlob project and is working with the UAS team in the context of remote sensing for biodiversity and nature conservation. She received her Bachelor's degree in Geography from the...

New team member: Ása Dögg Adalsteinsdottir

New team member: Ása Dögg Adalsteinsdottir

Ása Dögg Adalsteinsdottir joined the Earth Observation Research Cluster in May 2024 as a member of the EO4CAM project team. After earning a bachelor's degree in geography from the University of Iceland, she moved to Germany to study in our EAGLE master's program. She...



Christian Schäfer joined the EO4CAM project in May 2024. He received his Master's degree in 2017 from Julius-Maximilians-Universität Würzburg (JMU), focusing on GIS-based synthesis of transboundary soil maps. During his work in the JMU BigData@Geo project, he enhanced...

GGW talk on geodata, mobility and social media

GGW talk on geodata, mobility and social media

On Monday the 13th of May our PhD students Ariane Droin and Johannes Mast were holding a talk at the Geographische Gesellschaft Würzburg organised by the Fachschaft Geographie about 'Geodaten, Mobilität und soziale Medien. Big data und die lokale Perspektive der...

NetCDA kick-off workshop

NetCDA kick-off workshop

Yesterday, on May 16th, the partners of the project "European Academic Network for Capacity Development in Climate Change Adaptations in Africa" (NetCDA) met to jointly and officially kick-off their project. The NetCDA team at the University of Würzburg invited all...