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
- Choose K (number of clusters).
- Randomly place K centroids.
- Assign each data point to the nearest centroid.
- Move the centroid to the center (mean) of its assigned points.
- 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.