-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKNSS.py
61 lines (38 loc) · 1.36 KB
/
KNSS.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
import itertools
import numpy as np
from heapq import *
def KNN_SS(K , subspace):# KNN for subspace
global PDS
K_near_dic={}
sub_names =list(subspace.columns)
for p in sub_names:
K_near_dic[p] =[]
for p, q in itertools.combinations(sub_names, 2):
#if(p != q):
nrm =np.linalg.norm(subspace[p]-subspace[q])
K_near_dic[p].append((q,round(nrm,2)))
K_near_dic[q].append((p,round(nrm,2)))
for p in subspace:
pnt_ind = K_near_dic[p]
K_near_dic[p] = nsmallest(K, pnt_ind, key=lambda x: x[1])
K_near_dic[p] = [x[0] for x in K_near_dic[p]]
#idx = np.argpartition(A, K)
#K_near_dic[p] = A[idx[:K]]
return K_near_dic
def KNN_Single_Point(K ,subspace, p):# KNN for subspace
global PDS
K_near_dic={}
#for p in subspace:
K_near_dic[p] =[]
#for p, q in itertools.combinations(subspace, 2):
for q in subspace:
if(p != q):
nrm =np.linalg.norm(subspace[p]-subspace[q])
K_near_dic[p].append((q,round(nrm,2)))
#K_near_dic[q].append((p,round(nrm,2)))
#for p in subspace:
pnt_ind = K_near_dic[p]
K_near_dic[p] = nsmallest(K, pnt_ind, key=lambda x: x[1])
#idx = np.argpartition(A, K)
#K_near_dic[p] = A[idx[:K]]
return K_near_dic