K-Means (Unsupervised Learning)

How an algorithm finds groups without anyone telling it what to look for.

🧬 Interactive Genome Analysis

Welcome to my lab! Let's analyze these genomes.

An interactive scatter plot shows data points as blue circles and cluster centers as large colored circles. Keyboard: the arrow keys move a marker across the field, with Shift in bigger steps. Enter or Space places a point there. When the marker is on a point or centre, Enter grabs it, the arrow keys move it, and Enter drops it again. Delete or Backspace removes the point under the marker. Mouse or finger: a tap places a point, and points and centres can be dragged. Currently 250 data points on canvas
Clustering Controls
Run
Starts automatic grouping of data points based on current K valueStops the running execution of the K-Means clustering algorithm
Iterations0Inertia0.0Convergence0 %
Data Management
Parameters
K sets how many groups the points are split into. There is no universally "correct" K — the elbow method helps you choose.
Number of clusters to find
Pace of the auto-run. Slower is better for watching the assign/move phases unfold.
1× = slow enough to follow, 100× = almost instant

Elbow Method The elbow method tries several K values and shows where adding more clusters barely helps anymore — the "bend" marks a good K.

Determine the optimal number of clusters automatically

Updates automatically...

What is the Elbow Method?

The elbow method helps determine the optimal number of clusters (K).

It measures inertia (sum of squared distances) for different K values and looks for the 'elbow' - the point where improvement slows down.

The elbow shows the best balance between clustering quality and complexity.

What happens on the field

This demo sorts points into groups — with no labels given, only by how close they lie to each other. Here is what you see on screen.

What you see
A dark field with points; the small pictures in the control panel change their shape. The large centre markers all start in one corner. Once the clustering runs, the points take on group colours, and each centre leaves a trail behind it.
What happens
On every step the centre markers slide to the middle of their coloured group. Then single points switch colour whenever a different centre suddenly sits closer. The borders between the colour patches shift step by step, until nothing moves any more.
What you can do
Click on the field to place your own points, or have points scattered for you. You can move points and centres with mouse or finger; drag a point out of the field and it disappears. Set the number of groups, then run the process automatically or walk through it step by step.
What to watch for
Nobody tells the AI where the groups are. It finds them entirely on its own — purely from how close the points sit to each other.

Definition

What is K-Means Clustering?

Analogy

Imagine you're organizing a large party and want to optimally group guests around standing tables. K-Means works like an intelligent party planner.

You know the number of tables (K), but not where the tables should be placed. Dr. Elana Vasquiz, our genome researcher, faces a similar problem: she has many genome samples and wants to organize them into meaningful groups.

The K-Means algorithm works as follows:

  • Randomly place tables (initialize centroids)
  • Each guest goes to the nearest table (assign data points)
  • Move tables to the center of their guest groups (recalculate centroids)
  • Repeat until everyone is satisfied (reach convergence)

In the end, you have perfectly distributed groups - just like Elana finds her genome clusters!

This interactive demonstration shows the K-Means algorithm in action. Dr. Elana Vasquiz guides you through genome analysis and explains every step.

Interaction with the Demo
  • Mouse click to add: Click on the black field to add new genome samples
  • Set K parameter: Use the slider to change the number of desired clusters
  • Control algorithm: Start, stop, or reset the K-Means algorithm as desired
Visualization Elements

The demo shows various aspects of the algorithm in real-time:

  • Blue circles: Represent genome samples to be grouped
  • Large colored circles: Cluster centers that move until they find optimal positions
  • Colored regions: Show mathematical boundaries between clusters (Voronoi diagram)
  • Elbow diagram: Helps determine the optimal number of clusters

TheoryPseudo-CodeStep by StepFlow Diagram

The K-Means Algorithm in Detail

K-Means is an iterative algorithm that partitions data into k clusters. The goal is to group data points such that the variance within each cluster is minimal. Here are the four main steps:

  1. Initialization: Choose k random points as initial cluster centroids (or use K-Means++ for better results). These starting points significantly influence the final result.
  2. Assignment: Assign each data point to the nearest centroid (based on Euclidean distance). This is done by calculating the Euclidean distance to each centroid.
  3. Update: Calculate new centroids as the mean of all assigned data points for each cluster. The centroids move to the center of mass of their group.
  4. Repetition: Repeat steps 2-3 until centroids no longer move (convergence reached) or only move minimally.

The algorithm guarantees convergence to a local optimum, but not necessarily to the global optimum. Therefore, it is often run multiple times with different initializations.

Understanding the Elbow Method

The elbow method is a heuristic for determining the optimal number of clusters in a dataset. It is based on analyzing the inertia (Within-Cluster Sum of Squares).

It calculates the Within-Cluster Sum of Squares (WCSS) for different k values and looks for the 'elbow' - the point where the rate of WCSS reduction dramatically slows. This point represents a good balance between model complexity and explanatory power.

Limitations and Constraints of K-Means

  • Spherical Clusters: Works best with spherical, similarly sized clusters. With complex shapes (e.g., half-moons, nested circles), K-Means often fails.
  • K Must Be Chosen in Advance: The choice of k must be made in advance. This requires domain knowledge or methods like Elbow or Silhouette analysis.
  • Depends on Initialization: Results can vary depending on initialization. K-Means++ improves this, but the problem persists.
  • Sensitivity to Outliers: Sensitive to outliers and noise in the data, as they can strongly influence the centroids. Median-based variants (K-Medians) can be more robust.

Practical Applications

  • Customer Segmentation: Grouping customers based on purchasing behavior and demographics for targeted marketing.
  • Bioinformatics: Classification of genes or proteins based on expression patterns or structural properties.
  • Image Processing: Color quantization (palette reduction) and image segmentation (division into regions).
  • Data Mining: Exploratory data analysis and pattern recognition in large datasets.

Try the demo! Experiment with different k values and initializations. Observe how the centroids move and the clusters form.

Check

K-Means Understanding Quiz

Not completed

What is the first step in the K-Means algorithm?

Select one answer

Answer Key: 1) B · 2) C · 3) A · 4) B

Plain Language

This preview is in Plain Language. The whole portal is also available in Plain Language. You can change the language at the top. Or use the button below. Our Plain Language texts are AI-assisted and editorially reviewed. They follow the core principles of easy-to-read language, but they are not certified Easy Read: a review panel of people with learning difficulties does not test them.

Preview: Sorting points into groups

What is this?

This demo shows K-means clustering. K-means clustering means: a computer sorts points into groups. Nearby points go into one group. You see this on a diagram.

How you use the demo

Click on the drawing area. A new point appears. Then you choose K. K is the number of groups. Then you press start. The computer sorts the points. The groups form on the screen.

What you learn

You learn: how many groups are there? The elbow method helps you with this. Elbow method means: a diagram shows the best K value. This is how you find the right number of groups.