Unsupervised classification finds natural groupings in your data without labeled examples. The algorithm discovers clusters based on spectral similarity, and you assign meaning to them afterward.
Learning objectives
- Explain how unsupervised classification differs from supervised.
- Apply K-means clustering to a satellite image.
- Interpret and label resulting clusters.
- Know when unsupervised methods are appropriate.
Why it matters
When you don't have training data or don't know what classes exist, unsupervised classification reveals the spectral structure of your scene. It's exploratory—perfect for initial data investigation or when ground truth is unavailable.
Key vocabulary
- Cluster
- A group of spectrally similar pixels identified by the algorithm.
- K-means
- An algorithm that partitions data into K clusters by minimizing within-cluster variance.
- Spectral space
- A multi-dimensional space where each axis represents a band's reflectance values.
Quick win: K-means clustering
This example finds 5 spectral clusters in a Landsat image:
// Load and prepare image
var image = ee.Image('LANDSAT/LC08/C02/T1_L2/LC08_044034_20210623')
.multiply(0.0000275).add(-0.2)
.select(['SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7']);
// Define region for training
var roi = image.geometry();
// Sample the image to get training data
var training = image.sample({
region: roi,
scale: 30,
numPixels: 5000 // Random sample of 5000 pixels
});
// Create and train the clusterer
var clusterer = ee.Clusterer.wekaKMeans(5).train(training);
// Apply the clusterer to the image
var result = image.cluster(clusterer);
// Visualize results
Map.centerObject(image, 10);
Map.addLayer(image, {bands: ['SR_B4', 'SR_B3', 'SR_B2'], min: 0, max: 0.3}, 'True Color');
Map.addLayer(result.randomVisualizer(), {}, 'Clusters');
print('Number of clusters:', 5);
What you should see
A map with 5 distinct colored regions. Each color represents a cluster of spectrally similar pixels. You'll need to inspect each cluster to assign meaningful labels (water, vegetation, urban, etc.).
When to use unsupervised classification
| Use Unsupervised When... | Use Supervised When... |
|---|---|
| You don't have training data | You have labeled ground truth |
| You're exploring an unfamiliar area | You know exactly what classes you need |
| You want to discover spectral patterns | You need reproducible, defined classes |
| The classes are spectrally distinct | Classes may overlap spectrally |
The unsupervised workflow
- Sample the image - Extract pixel values for clustering.
- Choose number of clusters (K) - Start with more than you need.
- Train the clusterer - Algorithm finds natural groupings.
- Apply to image - Each pixel gets a cluster ID.
- Interpret clusters - View each cluster and assign labels.
- Merge similar clusters - Combine clusters that represent the same class.
Inspecting clusters
To understand what each cluster represents, visualize them individually:
// Show only cluster 0 (water?)
var cluster0 = result.eq(0);
Map.addLayer(cluster0.updateMask(cluster0), {palette: ['blue']}, 'Cluster 0');
// Show only cluster 1 (vegetation?)
var cluster1 = result.eq(1);
Map.addLayer(cluster1.updateMask(cluster1), {palette: ['green']}, 'Cluster 1');
// Compare with the true color image to interpret
Choosing the right K
How many clusters should you use?
- Start high: Try K = 10-15, then merge similar clusters.
- Consider your goal: If you need 4 land cover classes, start with K = 8-10.
- Iterate: Run multiple times with different K values and compare results.
Rule of thumb
Use 2-3× more clusters than your expected number of classes, then merge.
Try it: Change the number of clusters
- Run the K-means example with K = 3, then K = 10.
- Compare how the clusters change.
- Identify which clusters represent water, vegetation, and urban areas.
Common mistakes
- Using too few clusters (classes get merged together).
- Expecting clusters to match your desired classes exactly.
- Forgetting that cluster numbers are arbitrary (cluster 0 could be anything).
- Not sampling enough pixels for training (use at least 1000-5000).
- Applying to a different image than trained on.
Quick self-check
- What's the main difference between supervised and unsupervised classification?
- What does the K in K-means represent?
- Why might you use more clusters than your final number of classes?
- How do you figure out what each cluster represents?