(Y, R)
| 51 | |
| 52 | |
| 53 | def purity(Y, R): |
| 54 | # maximum purity is 1, higher is better |
| 55 | N, K = R.shape |
| 56 | p = 0 |
| 57 | for k in range(K): |
| 58 | best_target = -1 # we don't strictly need to store this |
| 59 | max_intersection = 0 |
| 60 | for j in range(K): |
| 61 | intersection = R[Y==j, k].sum() |
| 62 | if intersection > max_intersection: |
| 63 | max_intersection = intersection |
| 64 | best_target = j |
| 65 | p += max_intersection |
| 66 | return p / N |
| 67 | |
| 68 | |
| 69 | # hard labels |