Cluster Analysis
Cluster analysis divides objects into groups (clusters) such that objects inside a cluster are very similar and objects of different clusters are very different. It is unsupervised and is used for customer segmentation, image analysis, document grouping and outlier detection. A good method scales to large data, handles different attribute types and noise, finds clusters of any shape and is not sensitive to the order of the input.
Types of Data
Clustering uses a data matrix (objects x attributes) or a dissimilarity matrix (distance between each pair of objects). The way of computing dissimilarity depends on the attribute type.
- Interval-scaled variables: first standardise them (z-score or mean absolute deviation), then use the Minkowski distance d = (Σ |xi - yi|^p)^(1/p). With p = 1 it is the Manhattan distance, with p = 2 the Euclidean distance and with p = infinity the maximum difference.
- Binary variables: use a 2 x 2 table with counts q (both 1), r (1, 0), s (0, 1) and t (both 0). Simple matching dissimilarity = (r + s) / (q + r + s + t) for symmetric variables; Jaccard dissimilarity = (r + s) / (q + r + s) for asymmetric variables, ignoring the 0-0 matches.
- Nominal variables: dissimilarity = (p - m) / p, where m is the number of matching variables and p the total.
- Ordinal variables: replace ranks by values from 0 to 1, then treat as interval-scaled.
- Ratio-scaled variables: apply a logarithm, then treat as interval-scaled.
- Mixed types: combine the dissimilarities of all variables by a weighted average.
Categorisation of Major Clustering Methods
- Partitioning methods: K-Means and K-Medoids.
- Hierarchical methods: agglomerative and divisive.
- Density-based methods: DBSCAN and OPTICS.
- Grid-based methods: STING and CLIQUE.
- Model-based methods: EM, COBWEB and SOM.
Definition: Dissimilarity. A number that shows how different two objects are; it is 0 for identical objects and grows as the objects become less alike.
Table 4.1: Dissimilarity by variable type
| Variable type | Measure | Note |
|---|---|---|
| Interval | Euclidean, Manhattan | Standardise first |
| Binary symmetric | Simple matching | Counts 0-0 |
| Binary asymmetric | Jaccard | Ignores 0-0 |
| Nominal | Fraction not matching | (p - m) / p |
| Ordinal | Rank scaled to 0 to 1 | Then as interval |
Example 4.1: Binary dissimilarity and distances
Problem: Two patients have symptom vectors p = (1, 1, 0, 1, 0, 0) and q = (1, 0, 0, 1, 1, 0). Find the simple matching and Jaccard dissimilarity. Also find the Euclidean and Manhattan distance between the points (1, 2) and (4, 6).
Solution:
- Counts: q = 2 (positions 1 and 4), r = 1 (position 2), s = 1 (position 5), t = 2 (positions 3 and 6).
- Simple matching = (1 + 1) / 6 = 0.333.
- Jaccard = (1 + 1) / (2 + 1 + 1) = 0.5.
- Euclidean = √(3² + 4²) = 5 and Manhattan = 3 + 4 = 7.
Result: Simple matching 0.333, Jaccard 0.5, Euclidean distance 5 and Manhattan distance 7.
from math import dist
p = (1, 1, 0, 1, 0, 0)
q = (1, 0, 0, 1, 1, 0)
a = sum(x == 1 and y == 1 for x, y in zip(p, q))
r = sum(x == 1 and y == 0 for x, y in zip(p, q))
s = sum(x == 0 and y == 1 for x, y in zip(p, q))
t = sum(x == 0 and y == 0 for x, y in zip(p, q))
print("Simple matching:", round((r + s) / (a + r + s + t), 3))
print("Jaccard :", (r + s) / (a + r + s))
u, v = (1, 2), (4, 6)
print("Euclidean:", dist(u, v))
print("Manhattan:", sum(abs(x - y) for x, y in zip(u, v)))
Output
Simple matching: 0.333
Jaccard : 0.5
Euclidean: 5.0
Manhattan: 7
Common mistakes
- Clustering interval attributes without standardising them first.
- Using simple matching for asymmetric binary variables, so many 0-0 matches look like similarity.
- Applying Euclidean distance to nominal data.
- Expecting every method to find clusters of any shape; partitioning methods find round ones.
Key takeaways
- Dissimilarity depends on the variable type.
- Minkowski: p = 1 Manhattan, p = 2 Euclidean.
- Jaccard ignores 0-0 matches; simple matching counts them.
- Five families: partitioning, hierarchical, density, grid and model-based.
Practice questions
- When is the Jaccard coefficient used?
- What is the Manhattan distance?
- Name the major clustering method families.
Answers to the practice questions
- For asymmetric binary variables, where 0-0 matches carry no information.
- The sum of absolute differences of the coordinates.
- Partitioning, hierarchical, density-based, grid-based and model-based.
Sources
- Official BCA syllabus Data Warehousing and Mining, course code 25BCA406DS03, Unit IV
