Unsupervised Learning: Clustering
Tags: machinelearning, unsupervisedlearning
Published: 2019-03-21 07:12:22

---
title: "Unsupervised Learning: Clustering"
published: true
description: Single Linkage, K-Means, Soft Clustering, and Kleinberg Impossibility
tags: Machine Learning, Unsupervised Learning
---

*This is the 14th in a series of class notes as I go through the [Georgia Tech/Udacity Machine Learning course](https://www.udacity.com/course/machine-learning--ud262). The class textbook is [Machine Learning by Tom Mitchell](https://www.cs.ubbcluj.ro/~gabis/ml/ml-books/McGrawHill%20-%20Machine%20Learning%20-Tom%20Mitchell.pdf).*

![https://upload.wikimedia.org/wikipedia/commons/thumb/0/09/ClusterAnalysis_Mouse.svg/450px-ClusterAnalysis_Mouse.svg.png](https://upload.wikimedia.org/wikipedia/commons/thumb/0/09/ClusterAnalysis_Mouse.svg/450px-ClusterAnalysis_Mouse.svg.png)

## Defining the basic Clustering Problem

We want to take a set of objects and put them into groups.

Formally,

```
Given:
- set of objects X
- inter object distances D(x,y)

Output:
- Partition Pd(x) = Pd(y)
  if x & y in same cluster
```

`D`, The distance algorithm, can be any similarity metric you choose depending on your domain knowledge.

## Single Linkage Clustering

The simplest clustering algorithm is Single Linkage.

- start by considering each object a cluster (so you have n objects and n clusters)
- define intercluster distance as the distance between the closest 2 points in the two clusters
- merge the two closest clusters
- repeat `n-k` times to arrive at `k` clusters.

This helps you build a Hierarchical Agglomerative Cluster Structure. By cutting off the tree at `k` levels from the top you can get a predetermined number of clusters, which is handy.

Instead of the _closest_ 2 points, you can also modify this algorithm to use the _furthest_ 2 points or the average (mean, median) point of each cluster.

SLC is deterministic, and also generates a minimum spanning tree. The running time is `O(n^3)`. However it generates some nonintuitive clusters because of the "closest points" rule. So we need something better.

## K-Means Clustering

Try:

- Pick k "centers" at random
- Each center "claims" all its closest points
- recompute centers by averaging the clustered points
- repeat until converged (recomputing doesn't have any change)

Centers do not have to be any of the points, its just the "center" of the cluster.

K-Means can be viewed as an optimization problem, in that we are always trying to get better and better answers with each round. To use optimization terminology, we are saying that we can **score** each set of **configurations** (the centers), and we try to move from **neighborhood** to neighborhood trying to improve.

If we assume we know the "true" partitions, `P(x)`, we can sum the squared errors between `center of P(x) - x` for an Error score to minimize. "Neighborhood" is a bit less intuitive - its the set of possible moves at the end of a round, basically, you can either move the partition, or you can move the centers.

You can prove that K-means Clustering:

- always improves or statys the same at each step (when you move partitions, you only move if error goes down, and when you move centers, by recomputing the center you immediately jump to the least error possible given the partitions) aka "monotonically non-increasing in Error"
- converges (because you have a finite number of configurations) as long as you have a way to do consistent tie breaking

If you recall the Randomized Optimization chapter, this sounds a lot like hill-climbing, where you pick and move to neighbors based on their score. This also means it can get stuck in local optima (bad clustering based on the random starting point), so the only fix is random restarts.

There's also an edge case where if a point is somehow equidistant between two perfectly converged clusters, it would nondeterministically be either part of one or the other. To deal with that we'll use an algorithm where sharing is allowed...

## Soft Clustering (Best!) also known as Expectation Maximization or Gaussian Mixing

The main trick here is to do like we did with MIMIC and assume points come from a probability distribution. Try:

- Assume the data was generated by 1 of `k` possible Gaussians with known variance
- Sample Xi from that Gaussian
- Repeat n times

The task is to find a hypothesis `h=<u1, u2,... uk>` of "k means" that maximizes the probability of the data (aka Maximum Likelihood). The Max Likelihood mean of the gaussian is just the mean of the data, so there's nothing really new here. However you can use `k` Hidden Variables (`[0,1]` membership in Partition 1 to `k`) run the algorithm in a similar way to k-Means Clustering. Instead of always improving in the Error metric, you're improving in the probabilistic metric. Running the algorithm is a process of expectation maximization.

Properties of Expectation Maximization:

- monotonically non-decreasing likelihood
- theoretically might not converge (because Gaussian has an infinite extent, but in practice it does)
- cannot diverge
- can get stuck
- works with any distribution

## Practical issue: Picking `k`: Elbows, Silhouettes, and more

Although you can lean on domain knowledge to pick `k`, there can algorithms to help pick [the "Elbow", the "best" point given a tradeoff between number of clusters vs explanatory power](https://en.wikipedia.org/wiki/Elbow_method_%28clustering%29).

![https://upload.wikimedia.org/wikipedia/commons/c/cd/DataClustering_ElbowCriterion.JPG](https://upload.wikimedia.org/wikipedia/commons/c/cd/DataClustering_ElbowCriterion.JPG)

You can choose any number of "good" metrics to quantify cluster quality: [adj. Mutual Information](https://en.wikipedia.org/wiki/Adjusted_mutual_information), Homogeneity, Completeness, and [V_Measure](http://scikit-learn.org/stable/modules/generated/sklearn.metrics.v_measure_score.html) ([see original paper](http://www.aclweb.org/anthology/D07-1043)).

Many ways to do this exist, see [the Wikipedia article for more](https://en.wikipedia.org/wiki/Determining_the_number_of_clusters_in_a_data_set).

[Silhouette profiles](https://en.wikipedia.org/wiki/Silhouette_(clustering)) can help visually inspect the appropriateness of clusters.

## Practical issue: Visualizing Clusters

Using `t-SNE` is very helpful:

{% youtube NEaUSP4YerM %}

A better alternative may have emerged in Feb 2018: [UMAP](https://github.com/lmcinnes/umap), and [the benefits are worth considering](https://github.com/lmcinnes/umap#benefits-of-umap). Here is the author explaining the idea:

{% youtube nq6iPZVUxZU %}

## (theoretical aside) Kleinberg Impossibility Theorem

There are three desirable properties of clustering algorithms:

- Richness (can describe any clustering as long as we tweak variables)
- Scale invariance (works the same regardless of units)
- Consistency (clusters the same every time)

SLC is consistent and but not Rich.
You can modify SLC's stopping point to make it richer, but you will lose scale invariance.

So on and so forth. This is a proven theorem, that these three properties are mutually contradictory.

Jon Kleinberg's original paper is called [An impossibility theorem for clustering](https://www.cs.cornell.edu/home/kleinber/nips15.pdf).

## Next in our series

Further notes on this topic:

- [The Expectation Maximization Algorithm](https://www.cc.gatech.edu/~dellaert/em-paper.pdf)
- [K-Means vs Gaussian Mixture Models](https://davidrosenberg.github.io/ml2015/docs/13.mixture-models.pdf) - a nice slide deck
- [Supervised Learning of Gaussian Mixture Models for
Visual Vocabulary Generation](https://basurafernando.github.io/papers/pr1.pdf) - a useful application

Hopefully that was a good introduction to Clustering. I am planning more primers and would love your feedback and questions on:


- [Overview](https://dev.to/swyx/machine-learning-an-overview-216n)
- Supervised Learning
    - [Decision Trees](https://dev.to/swyx/machine-learning-classification-learning--decision-trees-1mbh)
    - [Regression](https://dev.to/swyx/supervised-learning-regression-4d17)
    - [Neural Networks](https://dev.to/swyx/supervised-learning-neural-networks-mpo)
    - [Instance Based Learning (K Nearest Neighbors)](https://dev.to/swyx/supervised-learning-instance-based-learning-and-k-nearest-neighbors-kge)
    - [Ensemble Learning (AdaBoost)](https://dev.to/swyx/supervised-learning-ensemble-learning-lim)
    - [Kernel Methods & SVMs](https://dev.to/swyx/supervised-learning-support-vector-machines-3mgk)
    - [Computational Learning Theory](https://dev.to/swyx/supervised-learning-computational-learning-theory-160h)
    - [VC Dimensions](https://dev.to/swyx/supervised-learning-vc-dimensions-10b)
    - [Bayesian Learning](https://dev.to/swyx/supervised-learning-bayesian-learning-403l)
    - [Bayesian Inference](https://dev.to/swyx/supervised-learning-bayesian-inference-4l72)
- Unsupervised Learning
    - [Randomized Optimization](https://dev.to/swyx/unsupervised-learning-randomized-optimization-4c1i)
    - [Information Theory](https://dev.to/swyx/unsupervised-learning-information-theory-recap-4iem)
    - [Clustering](https://dev.to/swyx/unsupervised-learning-clustering-42mi)
    - [Feature Selection](https://dev.to/swyx/unsupervised-learning-feature-selection-84f)
    - Feature Transformation - week of Mar 11
- Reinforcement Learning
    - Markov Decision Processes - week of Mar 25
    - "True" RL - week of Apr 1
    - Game Theory - week of Apr 15