# k-means

k-means: How k-means Works, k-means with scikit-learn, How Many Groups?.

Updated: November 19, 2026

1. [How k-means Works](https://www.ask-codey.com/lessons/ml-how-k-means-works) (Journeyman): k-means finds k groups with a short loop you can run by hand, place k centers, give each example to its nearest center, move each center to the mean of its examples, and repeat until nothing moves, and because it only ever improves from where it starts, different starting centers can end in different groups.
2. [k-means with scikit-learn](https://www.ask-codey.com/lessons/ml-k-means-with-scikit-learn) (Journeyman): scikit-learn's KMeans runs the whole k-means loop, with fit_predict giving each example a group number and cluster_centers\_ holding the centers, but its distances weigh features by their units, so the features are scaled first in a pipeline, several starts are run (n_init) with a fixed random_state, and the result suits round groups of similar spread.
3. [How Many Groups?](https://www.ask-codey.com/lessons/ml-choosing-k) (Freelancer): With no answer key, the number of groups k is chosen with evidence and judgment, inertia (the within-cluster sum of squares) keeps falling as k grows so you look for the bend where it stops falling fast, the silhouette score from −1 to 1 says how well each example sits in its group, and in the end what the groups are for decides.

Source: https://www.ask-codey.com/courses/machine-learning-python/topics/ml-k-means-topic
