-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.py
51 lines (42 loc) · 1.52 KB
/
sim.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
#!/usr/bin/env python
import argparse
from scipy import spatial
import os
import numpy as np
from sklearn.cluster import OPTICS
parser = argparse.ArgumentParser(description='vector file.')
parser.add_argument('-vector_file', type=str, help='vector_file file', default="alllog/templates.txt.vec")
args = parser.parse_args()
vector_file = args.vector_file
basedir = os.path.dirname(vector_file)
vecs = []
with open(vector_file) as f:
for line in f:
vec = tuple(map(float, line.strip().split()))
vecs.append(vec)
# X = np.vstack((vecs[:1000]))
# clust = OPTICS(min_samples=2, xi=.05, min_cluster_size=2, metric='cosine')
# clust.fit(X)
# print(clust.labels_)
# with open(vector_file+".cluster", 'w') as f:
# f.write("\n".join(map(str, clust.labels_)))
v1 = vecs[0]
with open(vector_file+".sim", "w") as f:
for i in range(len(vecs)):
sim = []
for j in range(len(vecs)):
cos = spatial.distance.cosine(vecs[i], vecs[j])
cosine_similarity = 1 - cos
sim.append(cosine_similarity)
idx=np.argsort(sim)[-11:-1]
num=np.sort(sim)[-11:-1]
# for similarity >= 0.99, we treat them as the same template
# for all temps which have sim >= 0.99, we use the smallest idx as the idx of the group
small = i
for j in range(10):
if num[j] > 0.985:
small = np.min(idx[j:])
if small > i:
small = i
s=["{:.2f}".format(x) for x in num]
f.write("{}; {}; {}, {}\n".format(i, small, idx, s))