# Comparing two clustering methods with adjusted Rand index - How to make labels match

I have a set of data (countries), and I use a clustering method (Kmeans) to create two partitions on them based on different data (diet and covid stats). So I get two groups of partitions, and I want to compare them in order to know if a clustering method will aggregate the same countries on the two sets of data.

I want to use the adjusted Rand index in order to do compute this similarity.

The problem is, when you run two clustering algorithms, you have no guarantee whatsoever that the partition labels will be the same. My two KMeans could produce the exact same partition but, for example, the first KMeans could label the cluster containing the US as "A", while the other KMeans could label it as "B". To my understanding, the Rand index needs these labels to be the same.

I cannot find an existing and reliable method in order to "normalize" group labels. Such as if I find the US in group "A" in the first clustering result, I want the US in group A in the second clustering result as well. I think I could write the algorithm myself, but it would be better to use a method that is tested and approved, if such one even exists.

Is there such a method ?

Thanks.

• See here for an example on how to compare different partitions produced by different clustering algorithms.
– chl
Commented Dec 4, 2020 at 18:17
• Thank you for the link. I don't really understand how to apply it to my problem, though. I'm not so much interested about how the produced clusters are meaningful, but rather about how close to each other they are. Commented Dec 7, 2020 at 9:01
• "(...) compute the "closeness" of the resulting partitions, as measured by Jaccard similarities. In short, it allows to estimate the frequency with which similar clusters were recovered in the data."
– chl
Commented Dec 7, 2020 at 9:09
• An overview of external clustering criteria you may need stats.stackexchange.com/q/586470/3277 Commented Aug 31, 2023 at 20:26

I wonder why the OP said the two clustering methods couldn't be compared, if they have different labels for clustered observations. Note that Rand indices can even be used to compare partitions with different numbers of clusters. The Rand index is a function of pairs of elements belonging or not to the same cluster in the estimated partitions. If the clusters assignment vectors for clustering method 1 and clustering method 2 have the observations following the same order, there is no need to worry about the labels.

Numeric lables does not seem to matter.

Here is an example of using the adjustedRandIndex function in mclust package in R to check.

data <- structure(list(x = c(1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3), y = c(3, 3, 3, 1, 1, 1, 1, 2, 2, 2, 2, 2), y1 = c(2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 1, 1), y2 = c(1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3), y3 = c(1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3), y4 = c(3, 3, 3, 3, 1, 1, 1, 2, 2, 2, 2, 2)), row.names = c(NA, -12L), class = "data.frame")

library(mclust)
adjustedRandIndex(data$$x, data$$y)
adjustedRandIndex(data$$x, data$$y1)
adjustedRandIndex(data$$x, data$$y2)
adjustedRandIndex(data$$x, data$$y3)
adjustedRandIndex(data$$x, data$$y4)


This returns:

> adjustedRandIndex(data$$x, data$$y)
[1] 1
> adjustedRandIndex(data$$x, data$$y1)
[1] 1
> adjustedRandIndex(data$$x, data$$y2)
[1] 1
> adjustedRandIndex(data$$x, data$$y3)
[1] 0.7782755
> adjustedRandIndex(data$$x, data$$y4)
[1] 0.7782755


Note that columns y to y2 pertain to the same clusters as referred to by x, with the differences just being the numeric values of cluster lables. ARI is 1 in these conditions. On the other hand, columns y3 and y4 the same partitioning which is different from the previous one. So adjustedRandIndex(data$x, data$y3) and adjustedRandIndex(data$x, data$y4) is different from 1 but both are the same.

More generally, it seems that when comparing cluster partitions, the numeric cluster lables always do not matter. As an example, here are results for normalized mutual information:

library(aricode)
NMI(data$$x, data$$y)
NMI(data$$x, data$$y1)
NMI(data$$x, data$$y2)
NMI(data$$x, data$$y3)
NMI(data$$x, data$$y4)


Here are the results:

> NMI(data$$x, data$$y)
[1] 1
> NMI(data$$x, data$$y1)
[1] 1
> NMI(data$$x, data$$y2)
[1] 1
> NMI(data$$x, data$$y3)
[1] 0.8260462
> NMI(data$$x, data$$y4)
[1] 0.8260462


Again, the first 3 outputs are all 1, and the last 2 are different from 1 and are the same.

This is different from calculating Pearson correlation using cor, where the numeric values matter. Here for partitions, the algorthm has been developed to be clever enough to focus on the underlying partitions, without regard to numeric values for the cluster lables. Please correct me if I am wrong.