-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathclusterEmbedding.py
executable file
·53 lines (43 loc) · 1.36 KB
/
clusterEmbedding.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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Created on 2020-08-16
@author: mingo
@module:
'''
import os
import pickle
import numpy as np
from sklearn.cluster import KMeans
cluster_k = 2000
def clusterEmbedding():
with open('res/method_entity_embedding_TransE.pkl', 'rb') as f:
entity_embedding = pickle.load(f)
embeddings_id_entity_mapping = {}
embeddings = []
id = 0
for entity, embedding in entity_embedding.items():
embeddings_id_entity_mapping[id] = entity
embeddings.append(embedding)
id += 1
print('\n=========== clustering through k-means ============\n')
embeddings = np.array(embeddings)
y_pred = KMeans(n_clusters=cluster_k, random_state=0).fit_predict(embeddings)
cluster_method = {}
idx = 0
for y in y_pred:
method = embeddings_id_entity_mapping[idx]
if y not in cluster_method:
cluster_method[y] = [method]
else:
cluster_method[y].append(method)
idx += 1
print(len(cluster_method))
method_cluster_mapping = {}
for key in cluster_method:
for method in cluster_method[key]:
method_cluster_mapping[method] = key
with open('res/method_cluster_mapping_%d.pkl' % (cluster_k), 'wb') as f:
pickle.dump(method_cluster_mapping, f, protocol = 2)
if __name__ == "__main__":
clusterEmbedding()