-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn_models.py
57 lines (49 loc) · 1.38 KB
/
knn_models.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 matplotlib.pylab import *
import matplotlib.pyplot as plt
import networkx as nx
import os
os.chdir('C:/Kaige_Research/Graph Learning/graph_learning_code/')
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity, rbf_kernel
from sklearn.preprocessing import StandardScaler, Normalizer, MinMaxScaler
from utils import *
from synthetic_data import *
def KNN_graph(signals, node_num, k):
W=rbf_kernel(signals.T)
np.fill_diagonal(W,0)
V=np.ones((node_num, node_num))
for i in range(node_num):
rbf_row=W[i,:]
neighbors=np.argsort(rbf_row)[:node_num-k]
V[i,neighbors]=0
np.fill_diagonal(V,0)
U=np.fmax(V, V.T)
A=U*W
return A
def Mutual_KNN_graph(signals, node_num, k):
W=rbf_kernel(signals.T)
np.fill_diagonal(W,0)
V=np.ones((node_num, node_num))
for i in range(node_num):
rbf_row=W[i,:]
neighbors=np.argsort(rbf_row)[:node_num-k]
V[i,neighbors]=0
np.fill_diagonal(V,0)
U=np.fmin(V, V.T)
A=U*W
return A
def Centered_KNN_graph(signals, node_num, k):
old_W=rbf_kernel(signals.T)
ones=np.ones((node_num,1))
a=np.identity(node_num)-(1/node_num)*(np.dot(ones, ones.T))
W=a*old_W*a
V=np.ones((node_num, node_num))
for i in range(node_num):
rbf_row=W[i,:]
neighbors=np.argsort(rbf_row)[:node_num-k]
V[i,neighbors]=0
np.fill_diagonal(V,0)
U=np.fmax(V, V.T)
A=U*W
return A