-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
66 lines (55 loc) · 1.75 KB
/
utils.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import numpy as np
import scipy as sc
from scipy.spatial.distance import cdist
def diffusion_map(A, K_n):
thr_inv = np.vectorize(lambda x: x if x > 1e-9 else 1.0)
Dinv = sc.sparse.diags(1.0/thr_inv(np.array(A.sum(1))).flatten(),0)
n = A.shape[0]
K = sc.sparse.csc_matrix((n, n))
for k in range(K_n):
if k == 0:
Pk = Dinv.dot(A)
else:
Pk = Pk.dot(Dinv.dot(A))
K = 0.5 * (Pk + Pk.T)
DK = sc.sparse.csr_matrix(np.diag(K.diagonal()**(-0.5)))
K = DK.dot(K.dot(DK))
return K
def efficient_rank(pi_prev):
try:
_, lam, _=np.linalg.svd(pi_prev)
S = lam.sum()
res = np.exp(np.sum(-np.log(lam/S)*lam/S))
except:
print("SVD did not converge")
res = np.nan
return res
def modularity(A, labels):
A2 = copy.deepcopy(A)
np.fill_diagonal(A2,0)
m = float(A2.sum())
Q = 1.0
k = A2.sum(1)
B = np.zeros(A2.shape)
for c in np.unique(labels):
index_c = np.where(labels == c)
for cc in index_c[0]:
B[cc, (labels == c)]+=1
np.fill_diagonal(B,0)
#Q += - float(k.T.dot(B.dot(k))/(2.0*m)**2)
Q = 1.0/(2*m) *np.multiply( A2-k.reshape([-1,1]).dot(k.reshape([1,-1]))/(2*m), B).sum()
return Q
def elbow_method(X, labels):
D = 0
for c in np.unique(labels):
index_c = np.where(labels == c)[0]
mu_c = X[index_c,:].mean(0)
D += (cdist(X[index_c,:], mu_c) **2).sum()
return D
def elbow_variance_method(X, labels):
D = 0
for c in np.unique(labels):
index_c = np.where(labels == c)[0]
index_c_perp = np.setdiff1d(range(len(labels)), index_c)
D += (cdist(X[index_c,:], X[index_c_perp,:])**2).sum()
return D/ (cdist(X, X)**2).sum()