🎯 K-Means Clustering Interactive

Watch the K-Means algorithm cluster data points in real-time! Click to add points or generate random clusters.

Click "Run Clustering" to start
3
150

Data Points

0

Clusters (K)

3

Iterations

0

Inertia

0

What is K-Means Clustering?

K-Means is one of the most popular unsupervised machine learning algorithms. It partitions data into K distinct, non-overlapping clusters where each data point belongs to the cluster with the nearest mean (centroid).

How the Algorithm Works

Step 1: Initialize Randomly place K centroids in the data space. These are the initial cluster centers.
Step 2: Assignment Assign each data point to the nearest centroid based on Euclidean distance.
Step 3: Update Recalculate centroids as the mean of all points assigned to that cluster.
Step 4: Repeat Repeat steps 2-3 until centroids no longer move significantly or max iterations reached.

Key Concepts

K (Number of Clusters): The number of groups you want to divide your data into. This must be specified beforehand.

Centroid: The center point of each cluster, calculated as the mean of all points in that cluster.

Inertia: Sum of squared distances of samples to their closest cluster center. Lower is better.

Convergence: When centroids stop moving between iterations, the algorithm has converged.

Real-World Applications

• Customer Segmentation: Group customers by purchasing behavior
• Image Compression: Reduce color palette in images
• Document Classification: Organize documents by topics
• Anomaly Detection: Identify unusual patterns in data
• Recommendation Systems: Group users or items with similar characteristics
• Market Research: Identify distinct market segments

Choosing the Right K

Use the Elbow Method: Plot inertia vs. K and look for the "elbow" point where adding more clusters doesn't significantly reduce inertia. You can experiment with different K values using the slider above!