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:

Thermal Drone Data Acquisitions: CalVal project with Informatics

Thermal Drone Data Acquisitions: CalVal project with Informatics

Thermal cameras on drones look very good - visually. They show us temperature of our study objects be it stressed trees, warm patches in a city or active fires. But we do not yet know for sure how far we can trust the temperature values they deliver especially when...

Würzburger Förderpreis awarded to our URBAN:Pulse project

Würzburger Förderpreis awarded to our URBAN:Pulse project

“A remote sensing scientist, a social geographer, an informatics researcher, a sports researcher, a psychologist, a linguist and an economist meet ... what sounds like the start of a joke, was the start of a great interdisciplinary research project.” That’s how Hannes...

Annual Meeting of the NetCDA consortium

Annual Meeting of the NetCDA consortium

On November 3-4, 2026, the NetCDA II (European Academic Network for Capacity Development in Climate Change Adaptions in Africa) project network of 12 German academic partners met at the EORC for their annual meeting. We welcomed our new German research institutions on...

Mainpost reported on Stefan Dech’s Bavarian State Medal

Mainpost reported on Stefan Dech’s Bavarian State Medal

We’re happy to see that Mainpost, our local newspaper, also reported on the Bavarian State Medal for Outstanding Services to the Environment. Stefan Dech received it on October 21 at the Munich Residenz, and Environment Minister Thorsten Glauber gave the laudatory...

UrbanPulse in the Mainpost, and invitation to the festivities

UrbanPulse in the Mainpost, and invitation to the festivities

If you open the printed Mainpost today, keep an eye out for the Universitätsbund Würzburg’s invitation to the Festkonzert. Among the topics on the page, there’s one we’re rather proud of: our newly funded project URBAN:Pulse, which will be officially celebrated at the...

bzgl. Art. 50 Verordnung (EU) 2024/1689 (AI Act):

Überwiegend werden eigene originäre Texte und Bilder des EORC genutzt, jedoch sind einige Inhalte von blog post Texten teilweise mit Hilfe von KI überarbeitet worden und einige Bilder sind ganz mit KI erstellt, die jedoch deutlich keine photorealistische Darstellungen abbilden. Aussnahmen sind KI generierte fernerkundliche Datensätze, die explizit für Forschungszwecke mit KI erstellt wurden, hier wird aber durch den assoziierten Text der wissenschaftliche Grund der KI generierten Bilder erläutert.

Share This