I’ve been looking for ways to compare clustering results and through my searching I came across something called the Rand index. In this short post, I explain how this index is calculated.
From the Wikipedia page you can see that the Rand index, R, is calculated by:
Ignoring the numerator for now, notice that the denominator is a binomial coefficient. is the number of unordered pairs in a set of n elements. For example, if you have set of 4 elements {a, b, c, d}, there are 6 unordered pairs: {a, b}, {a, c}, {a, d}, {b, c}, {b, d}, and {c, d}. The in the formula refers to the number of times a pair of elements belongs to a same cluster across two different clustering results and the refers to the number of times a pair of elements are in different clusters across two different clustering results. It will be easier to understand the Rand index with a simple example.
Say we have a set of six elements: {a, b, c, d, e, f}. Clustering method 1 (CM1) forms three clusters; the first two items are in group 1, the third and fourth are in group 2, and the fifth and sixth are in group 3: {1, 1, 2, 2, 3, 3}. Clustering method 2 (CM2) forms two clusters; the first three items are in group 1 and the last three items are in group 2: {1, 1, 1, 2, 2, 2}. What’s the Rand index of these two clustering results?
To manually calculate the Rand index, we need to go through every unordered pair to work out and ; let’s work out first. There are 15 unordered pairs in a set of six elements: {a, b}, {a, c}, {a, d}, {a, e}, {a, f}, {b, c}, {b, d}, {b, e}, {b, f}, {c, d}, {c, e}, {c, f}, {d, e}, {d, f}, and {e, f}. is every time a pair of elements is grouped together by the two clustering methods. {a, b} and {e, f} are clustered together by CM1 and CM2, so = 2. is every time a pair of elements is not grouped together by the two clustering methods. {a, d}, {a, e}, {a, f}, {b, d}, {b, e}, {b, f}, {c, e}, and {c, f} are not clustered together by CM1 and CM2, so = 8. and are the times that both clustering methods agree. Therefore, the Rand index is:
Using the fossil package and the rand.index() function, we can work out the Rand index in R.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Let’s take a look at the rand.index() function.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Let’s go through the code step by step to see what it is doing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
Each row and column number in x and y correspond to the element number in group1 and group2, respectively. A zero indicates a match, i.e. that a particular element is in the same cluster as another element, and a one indicates that they belong to different clusters. The x and y matrices therefore list all the pairs (twice) and whether or not they belong to the same cluster.
Now the function can compare the two different groups of clusters to get the number of disagreements. Finally to get the Rand Index, which reflects the number of agreements, we subtract the fraction of disagreements from 1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Assessing clustering results
We can use the Rand index to assess a clustering approach. I’ll illustrate using k-means clustering using the famous iris dataset.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
From the Wikipedia article:
The Rand index has a value between 0 and 1, with 0 indicating that the two data clusterings do not agree on any pair of points and 1 indicating that the data clusterings are exactly the same.
The Rand index suggests that the k-means clustering of the iris data using sepal and petal measurements is similar to the real “clustering” of the data.
Transfer from link: https://davetang.org/muse/2017/09/21/the-rand-index/