MCPcopy Create free account
hub / github.com/Valdecy/pyCombinatorial / build_affinity_sparse

Function build_affinity_sparse

pyCombinatorial/algorithm/ssi.py:67–91  ·  view source on GitHub ↗
(D, k, sigma_mode, sigma_fixed)

Source from the content-addressed store, hash-verified

65
66# Function: Affinity
67def build_affinity_sparse(D, k, sigma_mode, sigma_fixed):
68 n = D.shape[0]
69 nbrs = knn_indices(D.astype(float), k)
70 if sigma_mode == 'adaptive':
71 sig = D[np.arange(n), nbrs[:, -1]].astype(float) + 1e-12
72 else:
73 sig = np.full(n, sigma_fixed, dtype = float)
74 rows, cols, vals = [], [], []
75 for i in range(n):
76 si = sig[i]
77 for j in nbrs[i]:
78 sj = sig[j]
79 dij = float(D[i, j])
80 if sigma_mode == 'adaptive':
81 w = np.exp(-(dij * dij) / (si * sj + 1e-12))
82 else:
83 w = np.exp(-(dij * dij) / (2.0 * sigma_fixed * sigma_fixed + 1e-12))
84 rows.append(i); cols.append(j); vals.append(w)
85 W = coo_matrix((vals, (rows, cols)), shape = (n, n)).tocsr()
86 W = (W + W.T).tocsr()
87 W = W.tolil()
88 W.setdiag(0.0)
89 W = W.tocsr()
90 W.eliminate_zeros()
91 return W
92
93# Function: Laplacian
94def laplacian_from_W(W):

Callers 1

Calls 1

knn_indicesFunction · 0.85

Tested by

no test coverage detected