Machine Learning Tutorial 0/98 lessons ~6 min read Lesson 48

    K-Means Clustering

    K-means partitions points into k groups by repeatedly assigning each point to the nearest centroid and recomputing centroids as cluster means.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    K-means partitions points into k groups by repeatedly assigning each point to the nearest centroid and recomputing centroids as cluster means. It works well when clusters are compact and roughly spherical.

    Understanding the topic

    Pick k Use domain knowledge, silhouette scores, or the elbow plot of inertia vs k.

    Scale first Distance-based clustering usually needs standardized features.

    • Pick k — Use domain knowledge, silhouette scores, or the elbow plot of inertia vs k.
    • Scale first — Distance-based clustering usually needs standardized features.

    Step-by-step explanation

    1. Pick k — Use domain knowledge, silhouette scores, or the elbow plot of inertia vs k.
    2. Scale first — Distance-based clustering usually needs standardized features.

    Informative example

    Python starter:

    python
    from sklearn.cluster import KMeans
    import numpy as np
    X = np.array([[1, 2], [1, 4], [10, 2], [10, 4]])
    labels = KMeans(n_clusters=2, random_state=0, n_init=10).fit_predict(X)
    print(labels.tolist())

    Output

    [1, 1, 0, 0]

    Execution workflow

    1K-Means Clustering — workflow
    1 / 2

    Pick k

    Use domain knowledge, silhouette scores, or the elbow plot of inertia vs k.

    Best practices

    • Hold out a test set before hyperparameter tuning.
    • Scale numeric columns for distance-based models.
    • Track multiple metrics — not accuracy alone on skewed labels.

    Common mistakes

    • Leaking test statistics into preprocessing fit on full data.
    • Training on the same rows you report as test performance.
    • Chasing complex models before a simple baseline.

    Hands-on exercise

    Practice:

    • Cluster a 2D scatter and color by label
    • Try k=2 vs k=4 on the same data

    Summary

    K-Means Clustering — Partition points around k centroids.

    Ready to mark this lesson complete?Track your journey across the entire course.