K-Means (Unsupervised Learning)

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

What is K-Means Clustering?

AnalogyDefinition

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

K-Means Clustering Demo

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 scattered evenly across it. Once the clustering runs, the points take on several group colours, and each group gets a larger centre marker as its middle.
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. 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.

🧬 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 navigation: Enter or Space to add point at center, Delete/Backspace to remove last point, Ctrl+C to clear all data. Mouse: Click on canvas to add new data points. Currently {count} data points on canvas 12
0
Iterations
0.00
Inertia
0%
Convergence

Clustering Controls

Primary control actions for the algorithm
Starts automatic grouping of data points based on current K value
Stops the running execution of the K-Means clustering algorithm
Cluster Count: 3 Number of clusters to find
Speed: 2/s Iterations per second during auto-run

Data Management

Data generation actions

Elbow Method

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.

K-Means Explained

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.

K-Means Understanding Quiz

Question 1 / 4
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