(Y, R)
| 33 | |
| 34 | # hard labels |
| 35 | def purity2(Y, R): |
| 36 | # maximum purity is 1, higher is better |
| 37 | C = np.argmax(R, axis=1) # cluster assignments |
| 38 | |
| 39 | N = len(Y) # number of data pts |
| 40 | K = len(set(Y)) # number of labels |
| 41 | |
| 42 | total = 0.0 |
| 43 | for k in range(K): |
| 44 | max_intersection = 0 |
| 45 | for j in range(K): |
| 46 | intersection = ((C == k) & (Y == j)).sum() |
| 47 | if intersection > max_intersection: |
| 48 | max_intersection = intersection |
| 49 | total += max_intersection |
| 50 | return total / N |
| 51 | |
| 52 | |
| 53 | def purity(Y, R): |