forked from abhinav4192/sparse-subspace-clustering-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBestMap.py
34 lines (26 loc) · 891 Bytes
/
BestMap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# bestmap: permute labels of L2 to match L1 as good as possible
import numpy as np
import sys
from Hungarian import *
def BestMap(L1, L2):
L1 = L1.flatten(order='F').astype(float)
L2 = L2.flatten(order='F').astype(float)
if L1.size != L2.size:
sys.exit('size(L1) must == size(L2)')
Label1 = np.unique(L1)
nClass1 = Label1.size
Label2 = np.unique(L2)
nClass2 = Label2.size
nClass = max(nClass1, nClass2)
# For Hungarian - Label2 are Workers, Label1 are Tasks.
G = np.zeros([nClass, nClass]).astype(float)
for i in range(0, nClass2):
for j in range(0, nClass1):
G[i, j] = np.sum(np.logical_and(L2 == Label2[i], L1 == Label1[j]))
c = Hungarian(-G)
newL2 = np.zeros(L2.shape)
for i in range(0, nClass2):
newL2[L2 == Label2[i]] = Label1[c[i]]
return newL2
if __name__ == "__main__":
pass