← All tutorials

GoCareerGo Tutorials

Machine Learning Tutorial

Supervised learning, classification, neural networks and core ML algorithms.

K-Means Clustering

An unsupervised algorithm that partitions data into K groups by repeatedly assigning points to the nearest centroid and recomputing centroids.

from sklearn.cluster import KMeans

model = KMeans(n_clusters=3)
labels = model.fit_predict(X)
  • Requires choosing K upfront — the 'elbow method' helps pick a good value
  • Sensitive to feature scale and initial centroid placement
  • Assumes roughly spherical, similarly-sized clusters