K-means Clustering

K-means Clustering Concept

  • Partition data into K clusters based on feature similarity.
  • Use Case: Customer segmentation, Recommendation Systems etc.
  • Algorithm Steps
    1. Initialize K cluster centroids randomly.
    2. Assign each data point to the nearest centroid to form clusters.
    3. Update centroids by calculating the mean of points in each cluster.
    4. Repeat 2nd & 3rd steps 2 and 3 until convergence.

K-means Concept Visually: Step 1

K-means Algorithm
K-means Algorithm: Initialization.

K-means Concept Visually: Step 2

K-means Algorithm
K-means Algorithm: Centroid Initialization.

K-means Concept Visually: Step 3

K-means Algorithm
K-means Algorithm: Member Assignment.

K-means Concept Visually: Step 4

K-means Algorithm
K-means Algorithm: Centroid Update.

K-means Concept Visually: Step 5

K-means Algorithm
K-means Algorithm: Re-assignment.

K-means Concept Visually: Step 6

K-means Algorithm
K-means Algorithm: Calculate Centroid.

K-means Concept Visually: Step 7

K-means Algorithm
K-means Algorithm: Re-assignment.

K-means Concept Visually: Step 8

K-means Algorithm
K-means Algorithm: Convergence.

Choosing K with Elbow Method

  • Run K-means for a range of K values (e.g. 1 to 10).
  • Plot inertia/wcss (sum of squared distances to its centroid) vs K.
  • Look for "elbow" point where inertia reduction slows down.
  • Elbow point suggests optimal K balancing fit and complexity.
K-means Elbow Method
K-means Elbow Method: Plotting inertia vs K to find optimal number of clusters.