-
Notifications
You must be signed in to change notification settings - Fork 0
/
clustering.py
183 lines (116 loc) · 4.4 KB
/
clustering.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from adj_rand import *
from precision_recall import *
import hdbscan as h
import rw
import numpy as np
def fit_hdbscan(g,min_cluster_size=2):
#name = 'learner_af_0.85_24000_A0.4'
#name = 'all_dev_utt_learner_af_HILLS_SHARED_0.8_A0.4'
#name = 'BEAGLE_HILLS_SHARED_0.5_A0.4'
#g = get_model(name)
g = rw.get_cluster(g,'animal:N')
#g.remove_vertex(g.vs.find(label='animal:N'))
d = g.shortest_paths_dijkstra(weights='distance')
d = np.array(d)
'''
##first way of
d = g.get_adjacency(attribute='distance')
d = np.matrix(d.data)
d[d == 0] = 1 #non-connected nodes have a distance of 1 #try something that is larger than longest path
##try using minimum paths
np.fill_diagonal(d,0)
names = None
#assert (d.transpose() == d).all()
'''
##also try using
c = h.HDBSCAN(metric='precomputed', min_cluster_size=min_cluster_size, gen_min_span_tree=True)
c.fit_predict(d)
return c
def get_hdbscan_clusters(g,c):
clusters = {}
for vid,cid in enumerate(c.labels_):
if g.vs[vid]['label'] == 'animal:N':
continue
if clusters.get(cid,None) is None:
clusters[cid] = [vid]
else:
clusters[cid].append(vid)
#uncategorized nodes
qq = clusters.pop(-1,None)
return clusters.values() + [[q] for q in qq] #comment to exclude singletons
def get_hdbscan_fscore_unweighted(g):
return get_hdbscan_fscore(g,weighted=False)
def get_hdbscan_fscore(g,weighted=True):
c = fit_hdbscan(g)
clusters = get_hdbscan_clusters(g,c)
p,r,c = precision_recall(g,
clusters
)
return calculate_fscore(p,r,c,weighted)
### these versions will include multiple categories multiple times ###
def get_hdbscan_fscore_unweighted_all(g):
return get_hdbscan_fscore(g,weighted=False)
def get_hdbscan_fscore_all(g,weighted=True):
c = fit_hdbscan(g)
clusters = get_hdbscan_clusters(g,c)
p,r,c = precision_recall_with_singletons(g,
clusters
)
return calculate_fscore(p,r,c,weighted)
def get_hdbscan_adj_rand_idx(g,weighted=False):
c = fit_hdbscan(g)
clusters = get_hdbscan_clusters(g,c)
cat_clusters = test_get_cluster_cat_labels(g,clusters)
return test_avg_adj_rand(cat_clusters)
if __name__ == "__main__":
names = ['gold_af_HILLS_SHARED']
names +=['animals_learner','BEAGLE_HILLS_SHARED']
names +=['all_dev_utt_learner']
#plot_omega_irt('animals_learner_af')
#itr = 5
for name in names:
generate_adj_rand_curve(name)
plt.show()
'''
name = 'learner_af_0.85_24000_A0.4'
name = 'all_dev_utt_learner_af_HILLS_SHARED_0.8_A0.4'
#name = 'BEAGLE_HILLS_SHARED_0.5_A0.4'
g = get_model(name)
g = get_cluster(g,'animal:N')
#g.remove_vertex(g.vs.find(label='animal:N'))
d = g.shortest_paths_dijkstra(weights='distance')
d = np.array(d)
##first way of
#d = g.get_adjacency(attribute='distance')
#d = np.matrix(d.data)
#d[d == 0] = 1 #non-connected nodes have a distance of 1 #try something that is larger than longest path
##try using minimum paths
#np.fill_diagonal(d,0)
#names = None
#assert (d.transpose() == d).all()
##also try using
c = h.HDBSCAN(metric='precomputed', min_cluster_size=2, gen_min_span_tree=True)
c.fit_predict(d)
#organize into clusters
clusters = {}
for vid,cid in enumerate(c.labels_):
if g.vs[vid]['label'] == 'animal:N':
continue
if clusters.get(cid,None) is None:
clusters[cid] = [vid]
else:
clusters[cid].append(vid)
#uncategorized nodes
qq = clusters.pop(-1,None)
name = 'BEAGLE_HILLS_SHARED_0.5_A0.4'
g = get_model(name)
print get_hdbscan_adj_rand_idx(g)
c = fit_hdbscan(g)
clusters = get_hdbscan_clusters(g,c)
#plot_graph(g,name+"_clustered",vertex_colors=c.labels_)
p,r,d = precision_recall(g,
clusters
)
print p, r, d
print calculate_fscore(p,r,d)
'''