Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-149

Cluster Purity

Problem

Compute cluster purity: for each cluster count how many points belong to each true class, add the largest count, and divide by n.

Empty input returns 0.0.

Hint

Each cluster votes with its most frequent true class; sum those votes and normalize.

Test cases

case 1
in: [[0,0,1,1],[0,0,0,1]]
expected: 0.75
case 2
in: [[0,0,0,1,1,1],[0,1,0,1,1,2]]
expected: 0.6666666666666666
case 3
in: [[7,7,7],[1,2,3]]
expected: 0.3333333333333333
case 4
in: [[],[]]
expected: 0
Starter codePython
def cluster_purity(cluster_ids, true_labels):
    # Your code here
    pass

4 test cases · run your code against them in the editor