forked from Jingho/Deepstack-for-Bucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCluster_data.py
440 lines (375 loc) · 14.8 KB
/
Cluster_data.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# -*- coding:utf-8 -*-
import imp
import numpy as np
import random
import math
import time
import os
import ast
import argparse
from pyemd import emd
from calc_matrix import Cmatrix
from sklearn.cluster import KMeans
STREET = 'turn'
FILE_PATH = 'data/'
INITIAL_METHOD = 'kmean++'
IFSAVE = True
CLUSTER_K = 5
"""
Initialize the cluster center point using kmeans++
@param points_list list the cluster point
@param k int the number of clusters
@param EMDmatrix np.array the cluster center point distance matrix of the last round
return k initial seeds
@K_Means_Plus_Plus
"""
class K_Means_Plus_Plus(object):
def __init__(self, points_list, k, EMDmatrix):
self.centroid_count = 0
self.point_count = len(points_list)
self.cluster_count = k
self.matrix = EMDmatrix
self.points_list = list(points_list)
print('---K-means++ Cluster point initialization start---')
self.initialize_first_centroid()
self.init_centroids_list = self.initialize_other_centroids()
print('---K-means++ Cluster point initialization ends---\n')
"""
Picks a random point to serve as the first centroid
@initialize_first_centroid
"""
def initialize_first_centroid(self):
self.centroid_list = []
index = random.randint(0, len(self.points_list)-1)
self.centroid_list.append(self.remove_point(index))
self.centroid_count = 1
"""
Removes point associated with given index so it cannot be picked as a future centroid.
Returns list containing coordinates of newly removed centroid
@remove_point
"""
def remove_point(self, index):
new_centroid = self.points_list[index]
del self.points_list[index]
return new_centroid
"""
Finds the other k-1 centroids from the remaining lists of points
@initialize_other_centroids
"""
def initialize_other_centroids(self):
while not self.is_finished():
print('centroid count: {}'.format(self.centroid_count))
distances = self.find_smallest_distances()
chosen_index = self.choose_weighted(distances)
self.centroid_list.append(self.remove_point(chosen_index))
self.centroid_count += 1
return self.centroid_list
"""
Calculates distance from each point to its nearest cluster center. Then chooses new
center based on the weighted probability of these distances
@find_smallest_distances
"""
def find_smallest_distances(self):
distance_list = []
for point in self.points_list:
distance_list.append(self.find_nearest_centroid(point))
return distance_list
"""
Finds centroid nearest to the given point, and returns its distance
@find_nearest_centroid
"""
def find_nearest_centroid(self, point):
min_distance = math.inf
for values in self.centroid_list:
distance = self.get_distance(values, point)
if distance < min_distance:
min_distance = distance
return min_distance
"""
Chooses an index based on weighted probability
@choose_weighted
"""
def choose_weighted(self, distance_list):
distance_list = [x**2 for x in distance_list]
weighted_list = self.weight_values(distance_list)
indices = [i for i in range(len(distance_list))]
return np.random.choice(indices, p = weighted_list)
"""
Weights values from [0,1]
@weight_values
"""
def weight_values(self, list):
sum = np.sum(list)
return [x/sum for x in list]
"""
Computes N-d euclidean distance between two points represented as lists:
(x1, x2, ..., xn) and (y1, y2, ..., yn)
@get_distance
"""
def get_distance(self, point1, point2):
point_1 = np.array(point1, dtype=np.float64)
point_2 = np.array(point2, dtype=np.float64)
distance = emd(point_1, point_2, self.matrix)
return distance
"""
Checks to see if final condition has been satisfied (when K centroids have been created)
@is_finished
"""
def is_finished(self):
outcome = False
if self.centroid_count == self.cluster_count:
outcome = True
return outcome
"""
Returns final centroid values
@final_centroids
"""
def final_centroids(self):
return self.centroid_list
"""
Solves the problem of clustering a set of random data points into k clusters.
The process is iterative and visually shown how the clusters convergence on
the optimal solution.
@param points_list list the cluster point
@param k int the number of clusters
@param EMDmatrix np.array the cluster center point distance matrix of the last round
@param initialMethod string initialize cluster center point method
return Cluster center point
@Kmeans
"""
class Kmeans(object):
def __init__(self, points_list, k, EMDmatrix,
initialMethod='kmean++'):
assert initialMethod in ['kmean++','random'], 'initialMethod input error'
self.point_count = len(points_list)
self.k = k
self.matrix = EMDmatrix
self.points_list = list(points_list)
self.initialMethod = initialMethod
"""Takes the dataPoint and find the centroid index that it is closest too.
@param centroids The list of centroids
@param dataPoint The dataPoint that is going to be determined which centroid it is closest too
@points_best_cluster
"""
def points_best_cluster(self, centroids, dataPoint):
closestCentroid = None
leastDistance = None
for i in range(len(centroids)):
distance = emd(np.array(dataPoint),np.array(centroids[i]),self.matrix)
#print(distance)
if (leastDistance == None or distance < leastDistance ):
closestCentroid = i
leastDistance = distance
return closestCentroid
"""
Finds the new centroid location given the cluster of data points. The
mean of all the data points is the new location of the centroid.
@param cluster A single cluster of data points, used to find the new centroid
@new_centroid
"""
def new_centroid(self, cluster):
return np.mean(cluster, axis=0)
"""
Creates a new configuration of clusters for the given set of dataPoints
and centroids.
@param centroids The list of centroids
@param dataPoints The set of random data points to be clustered
return The set of new cluster configurations around the centroids
@configure_clusters
"""
def configure_clusters(self, centroids, dataPoints):
clusters = []
for i in range(len(centroids)):
cluster = []
clusters.append(cluster)
# For all the dataPoints, place them in initial clusters
for i in range(self.point_count):
idealCluster = self.points_best_cluster(centroids, dataPoints[i])
clusters[idealCluster].append(dataPoints[i])
#NOTE:it is dangerous
max = 0
max_index = 0
blank = []
for i in range(len(clusters)):
if len(clusters[i]) > max:
max = len(clusters[i])
max_index = i
if len(clusters[i]) == 0:
blank.append(i)
for i in range(len(blank)):
clusters[blank[i]].append(clusters[max_index].pop())
return clusters
"""
Calculates the cluster's Residual Sum of Squares (RSS)
@param cluster The list of data points of one cluster
@param centroid The centroid point of the corresponding cluster
@get_cluster_RSS
"""
def get_cluster_RSS(self, cluster, centroid):
sumRSS = 0
for i in range(len(cluster)):
sumRSS += pow(abs(emd(np.array(cluster[i]), np.array(centroid),self.matrix)), 2)
return sumRSS
"""
Iteratively clusters the dataPoints into the most appropriate cluster
based on the centroid's distance. Each centroid's position is updated to
the new mean of the cluster on each iteration. When the RSS doesn't change
anymore then the best cluster configuration is found.
@param dataPoints The set of random data points to be clustered
@param k The number of clusters
@solve
"""
def solve(self):
# Create the initial centroids and clusters
dataPoints = self.points_list
k = self.k
l = len(dataPoints[0])
if self.initialMethod == 'kmean++':
kpp = K_Means_Plus_Plus(self.points_list,self.k,self.matrix)
centroids = kpp.init_centroids_list
elif self.initialMethod == 'random':
centroids = random.sample(dataPoints, self.k)
print('------K-means Clustering start------')
clusters = self.configure_clusters(centroids, dataPoints)
# Loop till algorithm is done
allRSS = []
notDone = True
lastRSS = 0
while (notDone):
# Find Residual Sum of Squares of the clusters
clustersRSS = []
for i in range(len(clusters)):
clustersRSS.append(self.get_cluster_RSS(clusters[i], centroids[i]) / self.point_count)
currentRSS = sum(clustersRSS)
allRSS.append(currentRSS)
print("RSS", currentRSS)
# See if the kmean algorithm has converged
if (currentRSS == lastRSS):
notDone = False
else:
lastRSS = currentRSS
# Update each of the centroids to the new mean location
for i in range(len(centroids)):
centroids[i] = self.new_centroid(clusters[i])
# Reconfigure the clusters to the new centroids
clusters = self.configure_clusters(centroids, dataPoints)
print('------K-means Clustering ends------')
return centroids
def main(self):
if self.street == 'river':
print('clusters by sklearn:')
cluster = KMeans(n_clusters=self.k)
results = cluster.fit(self.points_list)
centroids = cluster.cluster_centers_.flatten()
centroids = np.sort(centroids).tolist()
else:
centroids = self.solve()
return centroids
"""
Load data and calculate cluster center points by class @{Kmeans}
@param street the round name
@param file_path the relative path of data
@param k int the number of clusters
@param initialMethod string initialize cluster center point method
@param ifsave whether to save the cluster center point
@Clustering
"""
class Clustering(Cmatrix):
def __init__(self,
street=None,
file_path='data/',
k=None,
initialMethod='kmean++',
ifsave=True,
):
super().__init__(street, file_path)
assert street in ['flop','turn','river'], \
'The street is None'
assert isinstance(k, int), 'The k is not a valid parameter'
self.street = street
self.file_path = file_path
self.initialMethod = initialMethod
self.k = k
self.ifsave = ifsave
self.points_list = self.get_data_set()
self.matrix = self.get_EHSmatrix()
self.savename = self.get_savename()
'''Cluster center point save file name'''
def get_savename(self):
savename = {"river": "river_cluster_k{}.csv".format(self.k),
"turn": "turn_cluster_k{}.csv".format(self.k),
"flop": "flop_cluster_k{}.csv".format(self.k)}
return self.file_path + savename.get(self.street)
'''load data'''
def get_data_set(self):
file_name = {"river": "river_data.csv",
"turn": "turn_data.csv",
"flop": "flop_data.csv"}
return self.load_data(file_name.get(self.street))
'''Calculate the last round of cluster center point distance matrix'''
def get_EHSmatrix(self):
if self.street == "river":
return
elif self.street == "turn":
river_cluster_name = 'river_cluster.csv'
river_cluster = self.load_data(river_cluster_name)
return self.get_Euclidean_Matrix(river_cluster)
elif self.street == "flop":
turn_cluster_name = 'turn_cluster.csv'
river_cluster_name = 'river_cluster.csv'
assert os.path.exists(self.file_path+river_cluster_name), river_cluster_name + ' is not exists'
assert os.path.exists(self.file_path+turn_cluster_name), turn_cluster_name + 'is not exists'
river_cluster = self.load_data(river_cluster_name)
turn_cluster = self.load_data(turn_cluster_name)
matrix_ = self.get_Euclidean_Matrix(river_cluster)
return self.get_NextEMD_Matrix(matrix_, turn_cluster)
'''Calculate the center point of the river round cluster'''
def skmeans(self):
print('clusters by sklearn:')
cluster = KMeans(n_clusters=self.k)
results = cluster.fit(self.points_list)
centroids = cluster.cluster_centers_.flatten()
centroids = np.sort(centroids)
return centroids
'''Save cluster center point'''
def savecentroids(self, centroids_list):
if self.street == 'river':
with open(self.savename,'w') as file:
for i in centroids_list:
temp_string = str(i.tolist())
file.write(temp_string+'\n')
else:
with open(self.savename,'w') as file:
for i in centroids_list:
temp_string = str(i.tolist()[0])
for j in range(1,len(i)):
temp_string = temp_string + ',' + str(i.tolist()[j])
file.write(temp_string+'\n')
print(self.street,'centroids saved in',self.savename)
'''Main program'''
def main(self):
print(self.street, 'clustering start')
if self.street == 'river':
centroids_list = self.skmeans()
else:
kmeans = Kmeans(self.points_list, self.k, self.matrix, self.initialMethod)
centroids_list = kmeans.solve()
print('final_centroids:')
for cent in centroids_list:print(cent.tolist())
if self.ifsave:
self.savecentroids(centroids_list)
return centroids_list
''' Get parameters from command line '''
def get_params():
parser = argparse.ArgumentParser()
parser.add_argument("--street", type=str, default='river')
parser.add_argument("--file_path", type=str, default='data/')
parser.add_argument("--k", type=int, default=5)
parser.add_argument("--initialMethod", type=str, default='kmean++')
parser.add_argument("--ifsave", type=ast.literal_eval, default=True)
args, _ = parser.parse_known_args()
return args
if __name__ == '__main__':
params = vars(get_params())
test = Clustering(street=params['street'], file_path=params['file_path'], k=params['k'], initialMethod=params['initialMethod'], ifsave=params['ifsave'])
centroid_list = test.main()