first-course-ml

Chapter 6: Clustering, a quick recap

Short revision notes. Exercises are not covered.

The problem

So far every task was supervised: each object \(\mathbf{x}_n\) came with a target \(t_n\). Now there are only the objects. Clustering groups them so that objects in the same group are similar and objects in different groups are not. What “similar” means is a modelling choice, for example squared distance for real-valued data. Typical uses: grouping customers (to base recommendations on similar customers) or genes with similar activity over time (to guess the function of an unknown gene from its known neighbours).

K-means

A cluster is a representative point \(\boldsymbol{\mu}_k\), the mean of the objects assigned to it. Let \(z_{nk}=1\) if object \(n\) is in cluster \(k\) (each object in exactly one cluster):

\[\boldsymbol{\mu}_k = \frac{\sum_n z_{nk}\mathbf{x}_n}{\sum_n z_{nk}} .\]

The means need the assignments and the assignments need the means, so alternate:

  1. Assign each object to its nearest mean, \((\mathbf{x}_n-\boldsymbol{\mu}_k)^T(\mathbf{x}_n-\boldsymbol{\mu}_k)\).
  2. If no assignment changed, stop. Otherwise recompute each mean and go back to step 1.

The procedure converges to a local minimum of the total squared distance \(D=\sum_n\sum_k z_{nk}(\mathbf{x}_n-\boldsymbol{\mu}_k)^T(\mathbf{x}_n-\boldsymbol{\mu}_k)\), not necessarily the global one. The result depends on the starting means, so run it from several random starts and keep the lowest \(D\).

Choosing \(K\). \(D\) keeps falling as \(K\) grows (it reaches 0 when every object is its own cluster), so it cannot choose \(K\), the same failure as likelihood for model order in Chapter 2. There is no clean rule. Look at the purpose, for example pick the \(K\) that gives the best recommendations or the best classification on validation data.

Where it fails. One point per cluster and squared distance mean clusters must be roughly round blobs of similar size: it cannot separate concentric rings (same centre), or clusters stretched so that some points are nearer the other cluster’s centre.

Kernel K-means

Use the kernel idea from Chapter 5. The distance to a cluster mean can be expanded so that the data appear only in inner products. Replace each inner product by a kernel \(k(\mathbf{x}_n,\mathbf{x}_m)\) and the algorithm works in an implicit feature space, with no need to compute the means. Initialise by the assignments \(z_{nk}\) (for example from ordinary K-means), then repeatedly compute kernelised distances and reassign. With a Gaussian kernel it separates the rings. It also lets you cluster any data type that has a kernel (text, graphs).

Mixture models

K-means is too crude a notion of cluster. A mixture model describes each cluster as a probability density, so it can carry shape (spread and orientation).

Generative story. To produce one object: (1) pick component \(k\) with probability \(\pi_k\) (\(\sum_k\pi_k=1\)), (2) draw \(\mathbf{x}_n\) from that component’s density, e.g. \(\mathcal{N}(\boldsymbol{\mu}_k,\boldsymbol{\Sigma}_k)\). The indicators \(z_{nk}\) are never observed. They are latent variables.

Likelihood. Marginalising out the component choice:

\[p(\mathbf{x}_n\mid\Lambda,\boldsymbol{\pi}) = \sum_{k=1}^{K}\pi_k\,p(\mathbf{x}_n\mid\Lambda_k), \qquad \mathcal{L} = \sum_n \log\sum_k \pi_k\,p(\mathbf{x}_n\mid\Lambda_k).\]

The sum inside the log makes direct maximisation hard.

The EM algorithm

Introduce a distribution \(q_{nk}\) over components for each object (\(q_{nk}>0\), \(\sum_kq_{nk}=1\)) and use Jensen’s inequality, \(\log\mathbb{E}\{f\}\ge\mathbb{E}\{\log f\}\), to get a lower bound \(\mathcal{B}\) on \(\mathcal{L}\). Maximising the bound (with Lagrange multipliers for the sum-to-one constraints) gives four updates, repeated until nothing changes:

\[q_{nk} = \frac{\pi_k\,p(\mathbf{x}_n\mid\boldsymbol{\mu}_k,\boldsymbol{\Sigma}_k)}{\sum_j \pi_j\,p(\mathbf{x}_n\mid\boldsymbol{\mu}_j,\boldsymbol{\Sigma}_j)} \qquad\text{(E step)}\] \[\pi_k=\frac1N\sum_n q_{nk},\qquad \boldsymbol{\mu}_k=\frac{\sum_n q_{nk}\mathbf{x}_n}{\sum_n q_{nk}},\qquad \boldsymbol{\Sigma}_k=\frac{\sum_n q_{nk}(\mathbf{x}_n-\boldsymbol{\mu}_k)(\mathbf{x}_n-\boldsymbol{\mu}_k)^T}{\sum_n q_{nk}} \qquad\text{(M step)}\]

Intuition.

Practical points.

Choosing the number of components

The log-likelihood rises with \(K\) (many small, tall densities fit the data points ever better), so it cannot choose \(K\) on the training data. But unlike \(D\) in K-means, the likelihood on held-out data works: use cross-validation and look for where it stops improving. It gives a rough range, not a sharp answer, which still beats K-means.

Other component distributions

The component density can be anything.

MAP versions and Bayesian mixtures

Take-aways

  1. Clustering is unsupervised: group similar objects, with “similar” being a modelling choice.
  2. K-means alternates assignment and mean update, finds a local optimum, and suits round, similar-sized clusters. Kernels extend it.
  3. Mixture models are a generative story with latent component labels. EM alternates soft assignment (E) and weighted parameter updates (M).
  4. Neither the K-means distance nor the training likelihood can choose the number of clusters. Held-out likelihood or the downstream task can.
  5. Both K-means and EM are local-optimum methods: restart from many random initialisations.