SwadhyAI Notes

All Python notes

K-means Clustering 🎯

Finding Groups

K-Means is an unsupervised algorithm that groups similar data points together. It tries to separate the data into K groups (clusters).

✨ How it Works

  1. Choose K (number of clusters).
  2. Randomly place K centroids.
  3. Assign each data point to the nearest centroid.
  4. Move the centroid to the center (mean) of its assigned points.
  5. Repeat steps 3-4 until centroids stop moving.

💪 The Elbow Method

How do we pick the best K? We use the Elbow Method.

We calculate the Inertia (sum of squared distances from points to their centroid) for different K values. As K increases, inertia decreases. We look for the "elbow" point where the decrease slows down.

python
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

data = list(zip([4, 5, 10, 4, 3, 11, 14 , 6, 10, 12], [21, 19, 24, 17, 16, 25, 24, 22, 21, 21]))
inertias = []

for i in range(1, 11):
    kmeans = KMeans(n_clusters=i)
    kmeans.fit(data)
    inertias.append(kmeans.inertia_)

plt.plot(range(1, 11), inertias, marker='o')
plt.title('Elbow Method')
plt.xlabel('Number of clusters')
plt.ylabel('Inertia')
plt.show()

✨ Pro Tip

K-Means is fast and scalable, but it assumes clusters are spherical and of similar size. It struggles with complex shapes.