Concepts / k-Means and Other Cost Minimization Clusterings

k-Means and Other Cost Minimization Clusterings

The objective function and the iterative algorithm are related but not identical ideas.

  • Programming

Two Meanings of k-Means

The phrase k-means clustering can refer to two related but different ideas. One idea is an objective function: a way to assign a cost to a proposed clustering. The other is an iterative procedure commonly used in practice to produce a clustering. Keeping these ideas separate prevents a common mistake: assuming that the procedure is guaranteed to find the clustering with the best possible objective value.

From Assignments to Cost

A k-means clustering assigns data points to clusters and gives each cluster a mean, also called its centroid. The objective function evaluates that arrangement by adding the squared Euclidean distances from the data points to the centroids of their assigned clusters. A smaller total means that the assigned points are, in aggregate, closer to their centroids according to this measure.

are assignedselect centroidprovide referencesumData pointsassigned vectorsCluster assignmentspoint to centroidCentroidscluster meansSquared distancesone per assigned pointTotal costsum of squared distances
How do assignments to centroids and squared distances combine into one value that k-means tries to minimize?

The objective function is useful because it turns a qualitative goal, keeping points close to the centers of their clusters, into one quantity that can be compared across proposed clusterings.

Distance and Assignment

Euclidean distance supplies the notion of closeness in this k-means formulation. For a data point, the relevant distance is the distance to the centroid of the cluster to which the point is assigned. That distance is then squared before it contributes to the objective's total cost. Thus, Euclidean distance plays two connected roles: it helps determine which centroid is closest during the iterative procedure, and it supplies the distances whose squared values are accumulated by the objective.

Euclidean distanceEuclidean distancecomparedcomparedsquare chosen distanceData pointone vectorCentroid Acandidate centerCentroid Bcandidate centerNearest centroidchosen assignmentSquared distancecost contribution
How does Euclidean distance determine an assignment and its contribution to cost?

Finding a Cluster Mean

Once a cluster has been specified, its mean is computed component by component. For each coordinate, add the values from all vectors in the cluster and divide by the number of vectors. The resulting vector is not just a descriptive label: it is the value that minimizes the cluster's sum of squared Euclidean distances.

A three-vector cluster

Find the mean of the vectors (1, 1), (3, 1), and (2, 4).

First coordinate: Average the first coordinates: (1 + 3 + 2) divided by 3 gives 2.

Second coordinate: Average the second coordinates: (1 + 1 + 4) divided by 3 gives 2.

Assemble the vector: Place the coordinate averages in their original order.

The cluster mean is (2, 2).

average coordinatesaverage coordinatesaverage coordinates(1, 1)vector(3, 1)vector(2, 4)vector(2, 2)component-wise mean
How are several vectors combined to locate the centroid that minimizes the cluster's sum of squared distances?

Do not average whole vectors as indivisible objects. Match corresponding coordinates, average each coordinate separately, and then assemble the resulting coordinates into the mean vector.

The Iterative Procedure

In the commonly used iterative approach, points are reassigned to the nearest centroid, and the centroids are recomputed from the resulting clusters. These two operations form a repeating cycle. Reassignment changes which points belong to each cluster; recomputation changes the location of each cluster mean based on its current members. The procedure therefore produces a clustering through repeated state changes rather than by directly solving every possible clustering arrangement.

compare distancesform clustersreplace centerscontinueiterateCurrent centroidsstarting centersNearest assignmentspoints select centroidsRecomputed meansaverage current membersUpdated centroidsnew centersNext iterationrepeat the cycle
What changes at each iteration as points are assigned to centroids and the centroids are recomputed?

What do you think happens?

Suppose the current cluster membership changes after points are reassigned. What should happen to that cluster's centroid?

  • It stays fixed because a centroid is chosen only once
  • It is recomputed as the component-by-component mean of the cluster's current vectors
  • It is replaced by the farthest point in the cluster
Reveal answer

Answer: It is recomputed as the component-by-component mean of the cluster's current vectors.

The iterative procedure recomputes centroids from the current cluster memberships, and the mean is the point that minimizes the cluster's sum of squared distances.

Optimal Versus Obtained

IdeaMeaningKey caution
Optimal k-means solutionA clustering that achieves the best possible value of the k-means objectiveFinding this solution is computationally difficult.
Iterative algorithm outcomeThe clustering produced by repeatedly assigning points and recomputing meansIt is a practical procedure and is not guaranteed here to be the exact optimizer.

It is natural to interpret k-means clustering as the clustering with the best possible objective value, but that interpretation is too strong. The optimization problem is NP-hard, and it is also NP-hard to approximate within some constant. In practice, a simpler iterative algorithm is often used instead. Its result can be locally optimal even when another clustering has a lower overall cost.

evaluated byevaluated bymay be greater thanIterative resultlocally optimalBest solutionglobally optimalHigher costobjective valueLower costobjective value
How can the iterative result be locally optimal even when another clustering has a lower overall cost?

Common Reasoning Errors

  • Treating the iterative algorithm as a guaranteed exact optimizer

    The optimization problem is computationally difficult, and the practical iterative procedure is not guaranteed here to find the exact optimum.

    Fix: Describe the result as the outcome of the iterative procedure unless optimality has been established separately.

  • Using an arbitrary notion of closeness

    Euclidean distance supplies the notion of closeness in this formulation.

    Fix: Use Euclidean distance when describing assignments and the corresponding squared distance when describing objective cost.

  • Computing a vector mean without matching coordinates

    A cluster mean is found by averaging vectors component by component.

    Fix: Average all first coordinates together, all second coordinates together, and continue this way for every coordinate.

  • Confusing an evaluation quantity with an algorithm

    The objective function evaluates a proposed clustering; reassignment and centroid recomputation describe the iterative procedure.

    Fix: Use objective function for the cost definition and iterative algorithm for the process that produces a clustering.

Practice the Separation

MEDIUM

A proposed clustering assigns every vector to a centroid and has a total cost formed by summing squared Euclidean distances. Explain which part of the description is the objective function and which part would belong to the iterative algorithm. Then find the mean of the vectors (2, 0), (4, 2), and (0, 4) by averaging each coordinate separately.

Hints
  • The objective function evaluates a completed assignment; it does not describe the sequence of reassignment and recomputation steps.
  • For the mean, average the first coordinates together and then average the second coordinates together.

Checking the practice result

Find the mean of (2, 0), (4, 2), and (0, 4).

First coordinate: Average 2, 4, and 0 to obtain 2.

Second coordinate: Average 0, 2, and 4 to obtain 2.

The mean is (2, 2). The objective evaluates assignments using squared Euclidean distances, while the iterative algorithm repeatedly changes assignments and recomputes means.

Key Takeaways

  1. The k-means objective evaluates a clustering by summing squared Euclidean distances from points to their assigned centroids.
  2. Euclidean distance defines closeness for assignments, while its squared values contribute to the total cost.
  3. A cluster mean is computed component by component and minimizes the cluster's sum of squared distances.
  4. The commonly used iterative algorithm is a practical procedure, not the same thing as the globally optimal solution.
  5. A locally optimal iterative result can have a higher cost than another possible clustering.

Key Takeaways

  • The objective function gives one cost for a proposed clustering.
  • Euclidean distance determines closeness, and squared distances are accumulated in the cost.
  • Cluster means are calculated by averaging corresponding vector coordinates.
  • The iterative algorithm alternates between nearest-centroid assignments and recomputed means.
  • The iterative result should not automatically be treated as the globally optimal k-means solution.