-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvector_clustering.py
More file actions
54 lines (43 loc) · 1.6 KB
/
vector_clustering.py
File metadata and controls
54 lines (43 loc) · 1.6 KB
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
from keras_model import Tweet2Vec
from time import time
import pandas as pd
from utils import savePickle
from utils import loadPickle
from sklearn.metrics.pairwise import rbf_kernel
from sklearn.cluster import spectral_clustering
from sklearn.cluster import k_means
model_file = './models/161205_sgd_lab/latest_model.keras'
source_file = './data/trump_sample.csv'
def get_vecs():
t0 = time()
tweet2vec = Tweet2Vec(model_file, char=False, chrd=True, word=True)
print("Loading model took {}s".format(time() - t0))
source = pd.read_csv(source_file, header=None, sep=chr(1))
text = source[0]
t0 = time()
M = tweet2vec[text]
print(M)
print(M.shape)
print("Grabbing {} vectors took {}s".format(len(text), time() - t0))
savePickle(M, './models/trump_sample_vectors.pickle')
def get_affinity():
t0 = time()
A = rbf_kernel(loadPickle('./models/trump_sample_vectors.pickle'))
savePickle(A, './models/trump_sample_affinity.pickle')
print(A.shape)
print("Spectral clustering took {}s".format(time() - t0))
def spectral_cluster():
t0 = time()
S = spectral_clustering(loadPickle('./models/trump_sample_affinity.pickle'), n_clusters=100)
savePickle(S, './models/trump_sample_spectral.pickle')
print(S)
print("Spectral clustering took {}s".format(time() - t0))
def kmeans():
t0 = time()
K = k_means(loadPickle('./models/trump_sample_vectors.pickle'), n_clusters=100, n_jobs=-1)
savePickle(K, './models/trump_sample_kmeans.pickle')
print(K)
print("K-means took {}s".format(time() - t0))
if __name__ == '__main__':
get_vecs()
kmeans()