AI engineering glossary · Classical ML algorithms

What is k-means?

K-means (also called k-means clustering, Lloyd's algorithm): A clustering algorithm that splits data into k groups by alternating two steps: assign each point to its nearest centre, then move each centre to the average of its points. It repeats until assignments stop changing. It is fast but assumes roundish clusters of similar size and needs k in advance.

Level: beginnerTopic: Classical ML algorithms

K-means: an example

Grouping 50,000 shoppers into k=4 segments using annual spend and visit frequency.

K-means in Python

A short example you can run (Python 3.11, NumPy, scikit-learn). The output below is what it really prints.

import numpy as np
from sklearn.cluster import KMeans

spend = np.array([[200, 2], [220, 3], [250, 2], [5000, 30], [5200, 28], [4800, 35]])  # rupees, orders
km = KMeans(n_clusters=2, n_init=10, random_state=0).fit(spend)
print(km.labels_)
print(km.cluster_centers_.round(0))

Output

[1 1 1 0 0 0]
[[5.00e+03 3.10e+01]
 [2.23e+02 2.00e+00]]

Related terms

More Classical ML algorithms terms

Where you learn it

K-means is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.

Before you go

Take the cheat sheet with you

One free PDF: the key AI-engineering terms, formulas and Python idioms. No spam; unsubscribe any time.