If you do have reference labels, you can ask how well a clustering agrees with them. Cluster ids are arbitrary names, so comparing them directly is meaningless; instead compare how both partitions treat every pair of samples: together in both, or apart in both, counts as agreement.
Implement rand_index(labels_a, labels_b), the fraction of sample pairs on which the two partitions agree.
labels_a and labels_b are one-dimensional arrays of equal length; ids can be any integers.float in [0, 1]; return 1.0 when there are fewer than two samples (no pairs).Open one at a time. Each gives away a little more than the last.
Build an n x n boolean matrix per partition saying whether two samples share a cluster: a[:, None] == a[None, :].
Compare the two matrices on the upper triangle only (np.triu_indices(n, k=1)) so each pair counts once and no sample is paired with itself.
No results yet
Run your code to check it against the examples, or submit to run every test.