External clustering evaluation
You have reference class assignments and want to assess an unsupervised clustering result.
Scikit-Learn · Function
Computes the Rand similarity between two clusterings and corrects it for chance. It is symmetric, returns 1.0 for identical partitions up to label permutation, and is near 0.0 for random labelings.
The function compares two assignments by examining whether sample pairs are grouped together or separately in each labeling. It adjusts the resulting Rand similarity against its expected value for random labelings, producing a symmetric score that accounts for chance and ignores cluster-name permutations.
sklearn.metrics.adjusted_rand_score(labels_true, labels_pred)| Name | Type | Description |
|---|---|---|
labels_true | array-like of shape (n_samples,) | Ground truth class labels to be used as a reference. |
labels_pred | array-like of shape (n_samples,) | Cluster labels to evaluate. |
ARIfloat similarity score between -0.5 and 1.0. Random labelings have an ARI close to 0.0; 1.0 stands for a perfect match.
from sklearn.metrics import adjusted_rand_score
true_labels = [0, 0, 1, 1]
predicted_labels = [1, 1, 0, 0]
print(adjusted_rand_score(true_labels, predicted_labels)) # 1.0Equivalent cluster IDs are treated as the same partition, so relabeling clusters does not reduce the score.
You have reference class assignments and want to assess an unsupervised clustering result.
Different algorithms or runs use different numeric cluster IDs for equivalent groups.
You need a chance-corrected score for comparing partitions with different cluster counts or sample sizes.
It computes similarity between two clusterings from pairwise agreement: whether each sample pair is placed in the same or different cluster in both labelings. The raw Rand score is normalized against expected random agreement, giving the adjusted Rand index.
Follow-up: Why can the score be negative? Because adjustment subtracts expected random agreement, sufficiently discordant assignments can fall below zero, down to the documented lower bound of -0.5.
Yes. Swapping the two arguments leaves the result unchanged, so either labeling can be supplied first. This symmetry follows from comparing pairwise agreements between the two assignments.
Follow-up: Does cluster-ID renaming matter? No; identical partitions receive 1.0 even when their numeric cluster labels are permuted.
The inputs are two array-like label sequences with one label per sample: labels_true is the reference assignment and labels_pred is the clustering assignment being evaluated. The function returns a floating-point similarity score.
Follow-up: What score indicates chance-level agreement? Random labelings are expected to produce an ARI close to 0.0.
adjusted_mutual_info_scorerand_score