# Finding Groups with k-means

Not every question comes with answers to learn from. Clustering finds groups of similar examples on its own: k-means places centers, assigns each example to the nearest, moves the centers, and repeats. Work one round by hand, run it with scikit-learn, choose how many groups to look for, and describe each group without pretending it's a fact about people. The running example is a café's loyalty members.

Updated: November 19, 2026

## [Learning Without Labels](https://www.ask-codey.com/courses/machine-learning-python/topics/ml-learning-without-labels)

1. [Learning Without Labels](https://www.ask-codey.com/lessons/ml-unsupervised-learning) (Apprentice): When the data has no answer column to learn from, a model can still find groups of examples that are more like each other than like the rest, which is called clustering, a kind of unsupervised learning, and a cluster is a pattern in the numbers you chose, not a fact about the people behind them.

## [k-means](https://www.ask-codey.com/courses/machine-learning-python/topics/ml-k-means-topic)

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.

## [What the Groups Mean](https://www.ask-codey.com/courses/machine-learning-python/topics/ml-what-the-groups-mean)

1. [Grouping with groupby](https://www.ask-codey.com/lessons/pandas-groupby) (Apprentice): groupby sorts a table's rows into groups that share a value, such as a neighborhood, works out one number for each group (a sum, a mean, a count) and hands back one row per group, which answers "which one sold the most?" in a line instead of a filter for every group.
2. [Describing Each Group](https://www.ask-codey.com/lessons/ml-describing-clusters) (Journeyman): A clustering is only useful once each group is described, so you groupby the cluster label to get each group's means and size, compare them with the whole table's, name each group in plain words from what its numbers say, and state the limits, that other features or another k would give other groups and that a member isn't their group.

## [Exercise: Café Regulars](https://www.ask-codey.com/courses/machine-learning-python/topics/exercise-ml-cafe-regulars)

1. [Exercise: Café Regulars](https://www.ask-codey.com/lessons/exercise-ml-cafe-regulars) (Journeyman): Write four functions that run one round of k-means by hand in plain Python, group the Cozy Corner Café's 600 loyalty members with a StandardScaler and KMeans pipeline, and describe each group with groupby, and let pytest check the hand round, the pipeline, the labels and the summaries.

Source: https://www.ask-codey.com/courses/machine-learning-python/chapters/ml-finding-groups
