-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_graph.py
59 lines (45 loc) · 1.66 KB
/
get_graph.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
import numpy as np
from scipy.spatial.distance import pdist, squareform
from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
# https://stackoverflow.com/questions/26576524/how-do-i-transform-a-scipy-sparse-matrix-to-a-numpy-matrix
def sim_gaussian(x, sigma):
inds = x.nonzero()
d = np.exp(-(x.A)**2 / (2 * sigma ** 2))
knndist = coo_matrix((d[inds], inds), shape=(x.shape[0], x.shape[1]))
return knndist
def get_graph(X, k, graph_type='mutual_knn'):
dist = squareform(pdist(X))
n = dist.shape[0]
isnn = np.zeros((n, n), dtype=bool)
# Create directed neighbor graph
for iRow in range(n):
idx = np.argsort(dist[iRow, :])
isnn[iRow, idx[0:k+1]] = True
knndist = csr_matrix((dist[isnn], isnn.nonzero()), shape=(n, n))
if graph_type == 'mutual_knn':
knndist = knndist.minimum(knndist.transpose())
elif graph_type == 'knn':
knndist = knndist.maximum(knndist.transpose())
sigma = np.median(knndist[isnn].A.ravel()) # Gaussian parameter
A = sim_gaussian(knndist, sigma)
return A
if __name__ == '__main__':
# test data
k = 2
x = np.array(
[[0.101840, -0.599612, -0.047494, -0.722723, 0.183573, 0.274240, 0.084235, -0.953521, -0.190030, -0.360554],
[1.360070, -1.081047, 1.109594, -0.488779, -0.035228, 0.072544, 0.824734, 1.534444, -0.396467, -1.771091]]
)
x = x.transpose()
A = get_graph(x, k=k)
print(A.A.ravel())
# matlab
# A = [5, 2, 1, 3, 4, 5, 2, 1, 2, 3, 4, 5];
# k = 2;
# A(1: k + 1);
# result: [5, 2, 1]
# python
A = np.array([5, 2, 1, 3, 4, 5, 2, 1, 2, 3, 4, 5])
k = 2
print(A[0:k+1])
# [5, 2, 1]