0Pricing
Data Science Academy · Lesson

k-Means and Choosing k

The elbow and silhouette methods.

k-Means and Choosing k is a free Data Science Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Go-To Clusterer

k-Means is the most popular clustering algorithm: it splits your data into k groups by grouping points around central anchors. 🎯

Centroids Are Anchors

Each cluster has a centroid, the mean position of its members. Points join the cluster whose centroid sits closest to them.

The Repeating Dance

k-Means alternates two steps: assign every point to its nearest centroid, then recompute each centroid from its new members.

It Settles Down

Those steps repeat until assignments stop changing. The algorithm has converged, and the clusters are final.

You Choose k First

k-Means cannot invent the number of groups. You must pick k up front, and the right value is rarely obvious.

Run It in One Line

scikit-learn makes fitting easy. Set n_clusters to your chosen k and call fit on your scaled data.

from sklearn.cluster import KMeans
model = KMeans(n_clusters=3).fit(X)

Inertia Measures Tightness

After fitting, inertia reports the total squared distance from points to their centroids. Lower means tighter clusters.

model.inertia_

The Elbow Method

Plot inertia for many k values. Where the curve bends sharply, the elbow, often marks a good number of clusters.

Diminishing Returns

Past the elbow, adding clusters barely lowers inertia. That flattening signals you are just splitting groups that already fit well.

The Silhouette Score

A second guide is the silhouette score: it rewards points that sit close to their own cluster and far from others.

from sklearn.metrics import silhouette_score

Start Smarter

Random starts can land badly, so scikit-learn defaults to k-means++, which spreads initial centroids out for steadier results.

Quick Check

Let us check how you would pick a sensible k.

Recap

k-Means groups points around centroids for a chosen k; the elbow and silhouette help you pick that k well. 🎯

Frequently asked questions

Is the “k-Means and Choosing k” lesson free?

Yes — the full text of “k-Means and Choosing k” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.

What will I learn in “k-Means and Choosing k”?

The elbow and silhouette methods. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Data Science Academy?

No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “k-Means and Choosing k” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Data Science Academy lesson?

Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Supervised vs Unsupervised
  2. k-Means and Choosing k
  3. Hierarchical and DBSCAN
  4. Profile and Name Your Clusters
← Back to Data Science Academy