-
Notifications
You must be signed in to change notification settings - Fork 33
/
functions.py
251 lines (186 loc) · 7.56 KB
/
functions.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import nltk
from nltk.cluster.kmeans import KMeansClusterer
from google.cloud import bigquery
client = bigquery.Client()
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import cluster
from sklearn.metrics import silhouette_score, silhouette_samples
from sklearn.preprocessing import StandardScaler, PowerTransformer, FunctionTransformer
from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline
import random
from sklearn.cluster import KMeans
import matplotlib.cm as cm
from mpl_toolkits.mplot3d import Axes3D
import time
from sklearn.manifold import TSNE
def load_data_from_bigquery(table, label_table):
table = 'eth-tokens.test.clean_avg_with_balances_tokens'
label_table = 'eth-tokens.alldata.etherscan_labelcloud'
#all data
sql = '''
SELECT * FROM `{}`
'''.format(table)
df = client.query(sql).to_dataframe()
#labelled data
sql = '''
SELECT es.label,es.category, a.* FROM `{}` a
INNER JOIN `{}` es
ON a.address = es.address
WHERE es.label IS NOT NULL
'''.format(table, label_table)
dflabel = client.query(sql).to_dataframe()
return df, dflabel
def data_pipeline(df):
#strip address column
data = df.iloc[:,1:]
log = FunctionTransformer(func=np.log1p, inverse_func=np.expm1, validate=True)
scale = StandardScaler()
pca =PCA(n_components=data.shape[1])
#build pipeline
pipe = Pipeline([('log', log ),
('scale', scale ),
('PCA', pca)])
results = pipe.fit_transform(data)
return pipe, results
def cluster(results, n_clusters):
cl = KMeans(n_clusters, n_init=20, max_iter=500,n_jobs=-1, verbose=0)
return cl.fit(results)
def assign_cluster_to_data(df, dflabel, cl):
lbls = []
addrs = list(df['address'].values)
for i, row in dflabel.iterrows():
addr = row['address']
if row['address'] in addrs:
lbls.append(addrs.index(row['address']))
else:
lbls.append(False)
dflabel['cluster'] = [cl.labels_[i] for i in lbls]
return None
def calc_tsne(results, n_components=2, perplexity=20, n_iter=300,verbose=1):
'''
Calculated tsne for dataset'''
time_start = time.time()
tsne = TSNE(n_components=n_components, perplexity=perplexity, n_iter=n_iter,verbose=verbose,learning_rate=100)
tsne_results = tsne.fit_transform(results)
print('t-SNE done! Time elapsed: {} seconds'.format(time.time()-time_start))
return tsne_results
def plot_tsne(cl, tsne_results ):
'''
plot'''
NUM_COLORS = cl.n_clusters
cm = plt.get_cmap('nipy_spectral')
fig = plt.figure(figsize=(15,12))
ax = fig.add_subplot(111)
ax.set_color_cycle([cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)])
for c in np.unique(cl.labels_):
mask = cl.labels_==c
if np.sum(mask) <1:
lbl = '_nolegend_'
else:
lbl = c
plt.scatter(tsne_results[mask][:,0], tsne_results[mask][:,1], s=20, alpha=.4,label=lbl)
leg = plt.legend(bbox_to_anchor=(1, 1))
for lh in leg.legendHandles:
lh.set_alpha(1)
plt.title('T-SNE', fontsize=20)
plt.xlabel('first principal component')
plt.ylabel('second principal component')
plt.show()
def find_category_of_cluster(cl,dflabel, category="Exchange"):
#assign cluster number with the most exchanges
type_cluster = 0
num_of_type = 0
lbl_density=0
print(category)
for clust in np.unique(cl.labels_):
size_of_cluster = np.sum(cl.labels_==clust)
mask = dflabel['cluster']==clust
d = dflabel[mask]
num = np.sum(d['category']==category)
density = num / size_of_cluster
if num > num_of_type:
lbl_density=density
num_of_type = num
type_cluster = clust
print('cluster number {} number of type found: {} cluster size: {} label density: {}'.format(clust,num,size_of_cluster,density))
return type_cluster
def plot_tsne_with_labels(tsne_results,df, dflabel,categs,colors):
#need to mask df based on which results were kept from the reclustering
labeled_addresses = dflabel['address'].values
labelmask = np.array([addr in labeled_addresses for addr in df['address'] ] )
#helper function for category mask
def cat(addr, labeled_addresses, dflabel):
if addr not in labeled_addresses:
return False
else:
idx = int(np.where(labeled_addresses==addr)[0][0])
return dflabel['category'][idx]
subset, not_subset = tsne_results[labelmask] , tsne_results[~labelmask]
fig = plt.figure(figsize=(15,12))
#not labelled points
plt.scatter(not_subset[:,0], not_subset[:,1], s=20, c='gray', alpha=.3)
#categories
cats = np.array([cat(addr, labeled_addresses, dflabel) for addr in df['address']])#[address_mask] ]) #added address mask for all clusters
# # #labelled points
# ax = fig.add_subplot(111)
# ax.set_color_cycle([cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)])
for c in list(dflabel['category'].unique()):
mask = dflabel['category']==c
if np.sum(mask) <1:
lbl = '_nolegend_'
else:
lbl = c
#category mask
catmask = cats == c
if c in categs:
idx=categs.index(c)
color = colors[idx]
plt.scatter(tsne_results[(labelmask & catmask)][:,0], tsne_results[(labelmask & catmask)][:,1], s=100,c=color, alpha=1,label=lbl)
leg = plt.legend(bbox_to_anchor=(1, 1))
for lh in leg.legendHandles:
lh.set_alpha(1)
plt.title('T-SNE', fontsize=20)
plt.xlabel('first principal component')
plt.ylabel('second principal component')
plt.show()
def plot_tsne_with_labeled_clusters(tsne_results, cl, clusters, categs, colors):
fig = plt.figure(figsize=(15,12))
ax = fig.add_subplot(111)
for c in np.unique(cl.labels_):
mask = cl.labels_==c
if c in clusters:
idx = clusters.index(c)
lbl = categs[idx]
color = colors[idx]
plt.scatter(tsne_results[mask][:,0], tsne_results[mask][:,1], s=100,c=color,alpha=.4,label=('Cluster {} - "{}" '.format(c,lbl) ))
else:
plt.scatter(tsne_results[mask][:,0], tsne_results[mask][:,1], c='gray',s=20, alpha=.3)
leg = plt.legend(bbox_to_anchor=(1, 1))
for lh in leg.legendHandles:
lh.set_alpha(1)
plt.title('T-SNE', fontsize=20)
plt.xlabel('first principal component')
plt.ylabel('second principal component')
plt.show()
def recluster(df, cl, clusters, n_clusters):
lbls = cl.labels_
mask = np.array([False for i in range(len(lbls))])
for c in clusters:
mask |= lbls==c
subpipe, results = data_pipeline(df[mask])
##use cosine similarity! NLTK clustering implementation
#KMeans cluster object as carrier for consistency
subcl = cluster(results, n_clusters)
kclusterer = KMeansClusterer(n_clusters, distance=nltk.cluster.util.cosine_distance, repeats=50)
assigned_clusters = kclusterer.cluster(results, assign_clusters=True)
#assign new cluster labels and cluster centroids
subcl.labels_ = np.array(assigned_clusters)
subcl.cluster_centers_ = np.array(kclusterer.means())
return subpipe, subcl, results, df[mask]
def plot_all(tsne_results,cl,df,dflabel,clusters,categs,colors ):
plot_tsne(cl, tsne_results)
plot_tsne_with_labeled_clusters(tsne_results, cl, clusters, categs, colors)
plot_tsne_with_labels(tsne_results,df, dflabel,categs,colors)