Lesson 51 of 60
pythonUnsupervised Learning – K‑Means Clustering
25 minutes
Course progress85%
K‑Means Clustering
K‑Means groups unlabeled data into k clusters by minimizing intra‑cluster variance.
Algorithm steps
- Randomly initialize
kcentroids. - Assign each point to the nearest centroid.
- Re‑compute centroids as the mean of assigned points.
- Iterate steps 2‑3 until convergence.
Using scikit‑learn
from sklearn.cluster KMeans
kmeans = KMeans(n_clusters=, random_state=)
clusters = kmeans.fit_predict(X_scaled)
df[] = clusters