TEC-Bridge Logo

K-Means Clustering Machine Learning Visualizer

STEM Interactive Visual Learning Program at TEC-Bridge AI

Setup Data

Clustering Controls

Visualization

Colored dots: Data points, Stars: Centroids

Algorithm Steps

Cluster Information

How to Use

  1. Setup: Enter x,y data pairs or click "Sample Data"
  2. K Value: Set the number of clusters (2-8)
  3. Start: Click "Start" to begin K-means clustering
  4. Step Through: Click "Next" to see each iteration or "Run Through" for automatic execution
  5. Observe: Watch centroids move and clusters form
  6. Reset: Click "Reset" to start over

K-Means Clustering

K-Means Clustering partitions data into k clusters by minimizing within-cluster sum of squares.

How it works:

  • Initialize k centroids randomly
  • Assign each point to nearest centroid
  • Update centroids to cluster means
  • Repeat until convergence
  • Distance metric: Euclidean distance
  • Minimizes intra-cluster variance

Time Complexity: O(n×k×i) where n=points, k=clusters, i=iterations

Use Cases

  • Customer Segmentation: Grouping customers by purchase behavior, demographics, and preferences for targeted marketing
  • Image Compression: Reducing colors in images by clustering similar pixel colors to representative centroids
  • Document Clustering: Organizing documents into topics for information retrieval and recommendation systems
  • Genetic Sequencing: Grouping similar genetic sequences to identify patterns and similarities in DNA analysis
  • Medical Diagnosis: Classifying patient data into disease groups based on symptoms and test results
  • Network Analysis: Identifying groups of related computers or users in network traffic data
  • Anomaly Detection: Finding outliers by identifying points far from any cluster centroid
  • Recommendation Systems: Clustering users or products to provide personalized recommendations

Real-World Example

Scenario: An e-commerce company uses K-Means clustering to segment their customers based on annual spending, purchase frequency, and product preferences to create targeted marketing campaigns.

Input: Customer data with 3 features (spending, frequency, product diversity)
Process: Initialize 3 centroids (e.g., budget, regular, premium customers) and iteratively assign customers to nearest cluster
Output: Clustered segments: Budget (10,000 customers), Regular (25,000 customers), Premium (5,000 customers)

K-Means is ideal for customer segmentation because it efficiently partitions large datasets into meaningful groups. Unlike supervised learning, it discovers natural groupings without labeled training data. The algorithm scales well with customer databases containing millions of records, and results are interpretable—each cluster represents a customer segment with distinct characteristics. The centroids represent average customer profiles, making it easy to understand and act on the clusters.

Benefits: Fast convergence, scalable, interpretable clusters, no labels needed, practical for business applications

K-Means Clustering Code Implementation

© 2025 TEC-Bridge AI. All rights reserved. | stemists.com@gmail.com | https://stemists.com