Concepts / Fitting and Inspecting Clustering Models

Fitting and Inspecting Clustering Models

Each training point receives a cluster label, and the fitted model exposes these through the labels_ attribute.

  • Programming

A List of Numbers That Looks Like Answers

What do you think happens?

A k-Means model has grouped your data into three clusters and returns the labels 0, 1 and 2. Which statement is true?

  • Cluster 0 is the group with the smallest values

  • Cluster 2 is the largest or most important group

  • The numbers are arbitrary names, and only the membership of each group means something

Reveal answer
Answer:

The numbers are arbitrary names, and only the membership of each group means something.

Clustering has no ground truth. The algorithm names each group it finds, so a label number carries no inherent meaning. The count of labels tells you how many groups exist, but the particular numbers say nothing about size, order or quality.

Once a k-Means model has run, it hands you a list of numbers, one per data point. The natural first move is to read those numbers as if they were answers: point 0 belongs to group 0, group 0 must be the small-values group, and so on. This feels reasonable because numbers in a list usually mean something. In clustering, that instinct leads you astray. This article follows the output step by step: first finding the labels the model stores, then understanding why the numbers are only bookkeeping, then labeling new points with predict, and finally correcting the most common misreadings.

Finding the Labels on a Fitted Model

During fitting, every training data point is given a cluster label. You do not compute these yourself. The fitted model stores them in an attribute called labels_, and you read it through the model object the same way you would read any other attribute. The labels_ attribute is the first place to look after fitting, because it records the outcome of the fit for every row you trained on.

python
Notes on the code
  • X holds five training points with two features each; this toy data is illustrative only
  • n_clusters=3 asks for three groups, so the labels will be numbered 0 to 2
  • fit runs the clustering; it does not return the labels
  • labels_ is read as an attribute of the fitted model object

The labels_ attribute is a list with one entry per training row, and the entries line up by position. The entry at position i is the label of the i-th training point. Position is the only link between the two, so the order of the rows you fitted on matters when you match labels back to your data. The diagram below shows this alignment for five illustrative points.

row 0label 1row 1label 0row 2label 2row 3label 2row 4label 1
The i-th entry of labels_ is the cluster label of the i-th training point. Here the illustrative labels for five rows are 1, 0, 2, 2 and 1.
Worked example

Reading one point's cluster from labels_

Using the illustrative labels above, which cluster does the training point at position 2 belong to, and which points share that cluster?

  1. 1
    Locate position 2

    Positions count from 0, so position 2 is the third training row, which is [8, 8].

  2. 2
    Read the entry

    The entry at position 2 of labels_ is 2, so that point belongs to cluster 2.

  3. 3
    Find the other members

    Positions 2 and 3 both hold 2, so the rows [8, 8] and [9, 9] share cluster 2.

Result

Point [8, 8] belongs to cluster 2 together with [9, 9]. The label 2 is simply the name for that group.

Why the Numbers Carry No Meaning

Clustering looks like classification: in both, each item ends up with a label. The crucial difference is that clustering has no ground truth. Nobody told the algorithm which group each point belongs to, so the labels have no pre-agreed meaning. Label 1 is not a predefined category. It is simply the name the algorithm gave to one of the groups it found.

In the real world

The source describes grouping a set of photos of faces. One resulting cluster, labeled 3, turns out to contain only pictures of your friend Bela. You could learn that only by looking at the pictures yourself. The algorithm contributes exactly one piece of information: every face labeled 3 is similar to the others in that group. The number 3 is arbitrary and could just as easily have been any other label.

The same reasoning applies to the small two-dimensional toy dataset. If one group came out labeled 0 and another labeled 1, you should read nothing into that ordering. The smaller number does not mean the group sits closer to the origin, has fewer members, or is more important. The label only distinguishes one group from another.

Because the names are arbitrary, they can change between runs. Random initialization can produce the same grouping of points with its labels permuted. The diagram below shows one grouping that receives two different sets of names.

points Alabel 0points Alabel 2points Blabel 1points Blabel 0points Clabel 2points Clabel 1
Two runs produce identical groups of points, but the labels attached to those groups are permuted. The membership is unchanged while the numbers are different.
Key point

Read a cluster through its member points and the features those points share. Do not read it through the number attached to it.

Labeling New Points with predict

The labels_ attribute covers only the points the model was trained on. When a new point arrives, use the predict method. Each new point is assigned to its closest cluster center, using the centers the model already learned during fitting. The diagram below traces that path from raw input to a label.

new pointraw inputfitted centerslearned during fitnearest centerclosest by distancecluster labelreturned by predictcompared toeach centerits cluster
A new point is compared against the fitted cluster centers, the nearest center is selected, and that center's cluster becomes the label returned by predict.
python
Notes on the code
  • predict takes new rows with the same number of features as the training data
  • predict returns one label per new point
  • Running predict on the training data should match labels_ for the same rows

What do you think happens?

After you call predict on new points, has the fitted model changed?

  • Yes, predict moves the cluster centers toward the new points

  • No, the existing cluster centers stay where they are and the model is unchanged

Reveal answer
Answer:

No, the existing cluster centers stay where they are and the model is unchanged.

The source is explicit that predict only assigns each new point to the closest existing center. Only calling fit again would recompute the model.

Questionlabels_predict(new_points)
Which points does it cover?Only the training pointsAny new points with the same features
How is the label obtained?Stored during fitComputed by finding the closest cluster center
Does it change the model?NoNo, the centers stay where they are
What reproduces it?The reference output of fittingpredict on the training set returns the same result

Two ways to get labels from a fitted k-Means model

Common Misreadings of k-Means Output

  • Common mistake

    Reading cluster 0 as the smallest or lowest-valued group

    What it looks like

    Seeing label 0 on a group and concluding it holds the smallest numbers in the dataset.

    Why it fails

    Label numbers are arbitrary names. The source states that a number like 0 or 3 carries no inherent meaning.

    Do this instead

    Compare the member points and their shared features to describe each cluster.

  • Common mistake

    Treating a higher label as a higher rank or a better group

    What it looks like

    Believing cluster 2 is more important than cluster 0 because 2 is bigger.

    Why it fails

    Clustering has no ground truth, so no label is ranked. The number only distinguishes one group from another.

    Do this instead

    Describe each group by its members, not by how large its label is.

  • Common mistake

    Assuming the same label numbers come back in every run

    What it looks like

    Saving the number 1 as the name of a particular group and expecting it to appear again after retraining.

    Why it fails

    Labels can change between runs because of random initialization, so the same grouping may receive permuted numbers.

    Do this instead

    Identify groups by their members and re-derive any naming after each fit.

  • Common mistake

    Using labels_ to label new points

    What it looks like

    Passing a new point's features and expecting labels_ to return its cluster.

    Why it fails

    labels_ covers only the training points the model was fit on.

    Do this instead

    Call predict on the new points to assign each one to its closest cluster center.

  • Common mistake

    Believing predict updates the model

    What it looks like

    Calling predict on new data and expecting the cluster centers to shift toward those points.

    Why it fails

    predict only looks up the nearest existing center and leaves the model unchanged.

    Do this instead

    Call fit again if you want the model recomputed from new training data.

  • Common mistake

    Assuming predict and labels_ can disagree on the same training data without a reason

Practice Reading the Output

EASY

A k-Means model with three clusters is fitted on 6 training rows, and labels_ returns [2, 2, 0, 1, 0, 1]. (a) Which cluster does the fourth training row belong to? (b) Someone says cluster 0 is the smallest group. What is the evidence for that claim? (c) You call predict on a new point. Does labels_ change afterward?

Hints
  • Positions count from 0, so the fourth row is at position 3.
  • Counting members of each label is a bookkeeping step; it does not reveal a ranking.
  • Recall the source's statement that predict leaves the existing model unchanged.
MEDIUM

Explain to a classmate, in two sentences, why the label 3 on a cluster of face photos tells you nothing until you look at the pictures, and what you would do with predict if a new photo arrived.

Hints
  • Focus on the lack of ground truth in clustering.
  • Mention that predict assigns the nearest existing center without refitting.

Key Takeaways

  • After fitting, a k-Means model stores one cluster label per training point in the labels_ attribute, aligned by position with the training rows.
  • Cluster labels are arbitrary names because clustering has no ground truth; the labels can change between runs, so read a cluster through its member points and shared features.
  • The predict method assigns each new point to its closest existing cluster center and leaves the fitted model unchanged.
  • Predicting on the training set reproduces labels_, which makes it a useful consistency check.
  • Avoid treating label numbers as rankings, sizes or importance, and avoid expecting labels_ or predict to refit the model.

Related concepts

More concepts starting with F