Machine Learning Basics·Machine learning from data to a responsible model

Clustering

Find groups without labels and evaluate whether the groups mean anything.

Intermediate 13 min read Lesson 8 of 300 Challenge included
What you will be able to do
  • Scale features before distance-based clustering.
  • Choose k with the elbow method and domain sense.
  • Inspect cluster centres in original units.
  • Validate clusters with a held-out sample or stakeholder review.

In plain English

Clustering groups similar rows. K-means needs a chosen number of clusters and scaled features. The result is a hypothesis, not a truth; inspect the cluster profiles before naming them.

cluster.pyPython
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler

scaled = StandardScaler().fit_transform(X)
model = KMeans(n_clusters=3, random_state=42, n_init='auto').fit(scaled)
print(model.labels_[:10])
print(model.cluster_centers_.shape)
Your turn

Fit K-means

Easy

Cluster scaled data into 3 groups with a fixed random state.

PYTHON · starter
KMeans()
Check your understanding before moving on.
Knowledge checkQuestion 1

Why scale features before K-means?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Pipelines
Lesson 8 of 300 · 0% of course