-
Notifications
You must be signed in to change notification settings - Fork 2
/
kmeans.py
380 lines (342 loc) · 12.5 KB
/
kmeans.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
import math
import random
import time
import tkinter as tk
from tkinter import *
from nimblenet.cost_functions import sum_squared_error
######################################################################
# This section contains functions for loading CSV (comma separated values)
# files and convert them to a dataset of instances.
# Each instance is a tuple of attributes. The entire dataset is a list
# of tuples.
######################################################################
# Loads a CSV files into a list of tuples.
# Ignores the first row of the file (header).
# Numeric attributes are converted to floats, nominal attributes
# are represented with strings.
# Parameters:
# fileName: name of the CSV file to be read
# Returns: a list of tuples
def loadCSV(fileName):
fileHandler = open(fileName, "rt")
lines = fileHandler.readlines()
fileHandler.close()
del lines[0] # remove the header
dataset = []
for line in lines:
instance = lineToTuple(line)
dataset.append(instance)
# print(dataset)
return dataset
def sum_squared_error( outputs, targets, derivative = False ):
if derivative:
return outputs - targets
else:
return 0.5 * np.mean(np.sum( np.power(outputs - targets,2), axis = 1 ))
# Converts a comma separated string into a tuple
# Parameters
# line: a string
# Returns: a tuple
def lineToTuple(line):
# remove leading/trailing witespace and newlines
cleanLine = line.strip()
# get rid of quotes
cleanLine = cleanLine.replace('"s', '')
# separate the fields
lineList = cleanLine.split(",")
# convert strings into numbers
stringsToNumbers(lineList)
lineTuple = tuple(lineList)
return lineTuple
# Destructively converts all the string elements representing numbers
# to floating point numbers.
# Parameters:
# myList: a list of strings
# Returns None
def stringsToNumbers(myList):
for i in range(len(myList)):
if (isValidNumberString(myList[i])):
myList[i] = float(myList[i])
# Checks if a given string can be safely converted into a positive float.
# Parameters:
# s: the string to be checked
# Returns: True if the string represents a positive float, False otherwise
def isValidNumberString(s):
if len(s) == 0:
return False
if len(s) > 1 and s[0] == "-":
s = s[1:]
for c in s:
if c not in "0123456789.":
return False
return True
######################################################################
# This section contains functions for clustering a dataset
# using the k-means algorithm.
######################################################################
def distance(instance1, instance2):
if instance1 == None or instance2 == None:
return float("inf")
sumOfSquares = 0
for i in range(1, len(instance1)):
sumOfSquares += (instance1[i] - instance2[i])**2
return sumOfSquares
import numpy as np
# def distance(instance1, instance2):
# x,y=instance1,instance2
# covariance_xy = np.cov(x,y, rowvar=0)
# inv_covariance_xy = np.linalg.inv(covariance_xy)
# xy_mean = np.mean(x),np.mean(y)
# x_diff = np.array([x_i - xy_mean[0] for x_i in x])
# y_diff = np.array([y_i - xy_mean[1] for y_i in y])
# diff_xy = np.transpose([x_diff, y_diff])
#
# md = []
# for i in range(len(diff_xy)):
# md.append(np.sqrt(np.dot(np.dot(np.transpose(diff_xy[i]),inv_covariance_xy),diff_xy[i])))
# return md
def meanInstance(name, instanceList):
numInstances = len(instanceList)
if (numInstances == 0):
return
numAttributes = len(instanceList[0])
means = [name] + [0] * (numAttributes-1)
for instance in instanceList:
for i in range(1, numAttributes):
means[i] += instance[i]
for i in range(1, numAttributes):
means[i] /= float(numInstances)
return tuple(means)
def assign(instance, centroids):
minDistance = distance(instance, centroids[0])
minDistanceIndex = 0
for i in range(1, len(centroids)):
d = distance(instance, centroids[i])
if (d < minDistance):
minDistance = d
minDistanceIndex = i
return minDistanceIndex
def createEmptyListOfLists(numSubLists):
myList = []
for i in range(numSubLists):
myList.append([])
return myList
def assignAll(instances, centroids):
clusters = createEmptyListOfLists(len(centroids))
for instance in instances:
clusterIndex = assign(instance, centroids)
clusters[clusterIndex].append(instance)
return clusters
def computeCentroids(clusters):
centroids = []
for i in range(len(clusters)):
name = "\nCentroid" + str(i+1)
centroid = meanInstance(name, clusters[i])
centroids.append(centroid)
return centroids
def kmeans(instances, k, animation=False, initCentroids=None):
result = {}
if (initCentroids == None or len(initCentroids) < k):
# randomly select k initial centroids
random.seed(time.time())
centroids = random.sample(instances, k)
else:
centroids = initCentroids
prevCentroids = []
if animation:
delay = 1.0 # seconds
canvas = prepareWindow(instances)
clusters = createEmptyListOfLists(k)
clusters[0] = instances
paintClusters2D(canvas, clusters, centroids, "Initial centroids")
time.sleep(delay)
iteration = 0
while (centroids != prevCentroids):
iteration += 1
clusters = assignAll(instances, centroids)
if animation:
paintClusters2D(canvas, clusters, centroids, "Assign %d" % iteration)
time.sleep(delay)
prevCentroids = centroids
centroids = computeCentroids(clusters)
withinss = computeWithinss(clusters, centroids)
if animation:
paintClusters2D(canvas, clusters, centroids,
"Update %d, withinss %.1f" % (iteration, withinss))
time.sleep(delay)
result["clusters"] = clusters
result["centroids"] = centroids
result["withinss"] = withinss
result["sse"]=withinss
return result
def computeWithinss(clusters, centroids):
result = 0
for i in range(len(centroids)):
centroid = centroids[i]
cluster = clusters[i]
for instance in cluster:
result += distance(centroid, instance)
return result
# Repeats k-means clustering n times, and returns the clustering
# with the smallest withinss
def repeatedKMeans(instances, k, n):
bestClustering = {}
bestClustering["withinss"] = float("inf")
for i in range(1, n+1):
print ("k-means trial %d," % i)
trialClustering = kmeans(instances, k)
print ("withinss: %.1f" % trialClustering["withinss"])
if trialClustering["withinss"] < bestClustering["withinss"]:
bestClustering = trialClustering
minWithinssTrial = i
print ("Trial with minimum withinss:", minWithinssTrial)
return bestClustering
######################################################################
# This section contains functions for visualizing datasets and
# clustered datasets.
######################################################################
def printTable(instances):
for instance in instances:
if instance != None:
line = instance[0] + "\t"
for i in range(1, len(instance)):
line += "%.2f " % instance[i]
print (line)
def extractAttribute(instances, index):
result = []
for instance in instances:
result.append(instance[index])
return result
def paintCircle(canvas, xc, yc, r, color):
canvas.create_oval(xc-r, yc-r, xc+r, yc+r, outline=color)
def paintSquare(canvas, xc, yc, r, color):
canvas.create_rectangle(xc-r, yc-r, xc+r, yc+r, fill=color)
def drawPoints(canvas, instances, color, shape):
random.seed(0)
width = canvas.winfo_reqwidth()
height = canvas.winfo_reqheight()
margin = canvas.data["margin"]
minX = canvas.data["minX"]
minY = canvas.data["minY"]
maxX = canvas.data["maxX"]
maxY = canvas.data["maxY"]
scaleX = float(width - 2*margin) / (maxX - minX)
scaleY = float(height - 2*margin) / (maxY - minY)
for instance in instances:
x = 5*(random.random()-0.5)+margin+(instance[1]-minX)*scaleX
y = 5*(random.random()-0.5)+height-margin-(instance[2]-minY)*scaleY
if (shape == "square"):
paintSquare(canvas, x, y, 5, color)
else:
paintCircle(canvas, x, y, 5, color)
canvas.update()
def connectPoints(canvas, instances1, instances2, color):
width = canvas.winfo_reqwidth()
height = canvas.winfo_reqheight()
margin = canvas.data["margin"]
minX = canvas.data["minX"]
minY = canvas.data["minY"]
maxX = canvas.data["maxX"]
maxY = canvas.data["maxY"]
scaleX = float(width - 2*margin) / (maxX - minX)
scaleY = float(height - 2*margin) / (maxY - minY)
for p1 in instances1:
for p2 in instances2:
x1 = margin + (p1[1]-minX)*scaleX
y1 = height - margin - (p1[2]-minY)*scaleY
x2 = margin + (p2[1]-minX)*scaleX
y2 = height - margin - (p2[2]-minY)*scaleY
canvas.create_line(x1, y1, x2, y2, fill=color)
canvas.update()
def mergeClusters(clusters):
result = []
for cluster in clusters:
result.extend(cluster)
return result
def prepareWindow(instances):
width = 500
height = 500
margin = 50
root = Tk()
canvas = Canvas(root, width=width, height=height, background="white")
canvas.pack()
canvas.data = {}
canvas.data["margin"] = margin
setBounds2D(canvas, instances)
paintAxes(canvas)
canvas.update()
root.lift()
return canvas
def setBounds2D(canvas, instances):
attributeX = extractAttribute(instances, 1)
attributeY = extractAttribute(instances, 2)
canvas.data["minX"] = min(attributeX)
canvas.data["minY"] = min(attributeY)
canvas.data["maxX"] = max(attributeX)
canvas.data["maxY"] = max(attributeY)
def paintAxes(canvas):
width = canvas.winfo_reqwidth()
height = canvas.winfo_reqheight()
margin = canvas.data["margin"]
minX = canvas.data["minX"]
minY = canvas.data["minY"]
maxX = canvas.data["maxX"]
maxY = canvas.data["maxY"]
canvas.create_line(margin/2, height-margin/2, width-5, height-margin/2,
width=2, arrow=LAST)
canvas.create_text(margin, height-margin/4,
text=str(minX), font="Sans 11")
canvas.create_text(width-margin, height-margin/4,
text=str(maxX), font="Sans 11")
canvas.create_line(margin/2, height-margin/2, margin/2, 5,
width=2, arrow=LAST)
canvas.create_text(margin/4, height-margin,
text=str(minY), font="Sans 11", anchor=W)
canvas.create_text(margin/4, margin,
text=str(maxY), font="Sans 11", anchor=W)
canvas.update()
def showDataset2D(instances):
canvas = prepareWindow(instances)
paintDataset2D(canvas, instances)
def paintDataset2D(canvas, instances):
canvas.delete(ALL)
paintAxes(canvas)
drawPoints(canvas, instances, "blue", "circle")
canvas.update()
def showClusters2D(clusteringDictionary):
clusters = clusteringDictionary["clusters"]
centroids = clusteringDictionary["centroids"]
withinss = clusteringDictionary["withinss"]
canvas = prepareWindow(mergeClusters(clusters))
paintClusters2D(canvas, clusters, centroids,
"Withinss: %.1f" % withinss)
def paintClusters2D(canvas, clusters, centroids, title=""):
canvas.delete(ALL)
paintAxes(canvas)
colors = ["blue", "red", "green", "brown", "purple", "orange"]
for clusterIndex in range(len(clusters)):
color = colors[clusterIndex%len(colors)]
instances = clusters[clusterIndex]
centroid = centroids[clusterIndex]
drawPoints(canvas, instances, color, "circle")
if (centroid != None):
drawPoints(canvas, [centroid], color, "square")
connectPoints(canvas, [centroid], instances, color)
width = canvas.winfo_reqwidth()
canvas.create_text(width/2, 20, text=title, font="Sans 14")
canvas.update()
######################################################################
# Test code
######################################################################
fileName2='data.csv'
dataset = loadCSV(fileName2)
showDataset2D(dataset)
clustering = kmeans(dataset, 5)
printTable(clustering["centroids"])
def printT(i):
print("\nSum of Squared Error is: "+str(i))
printT(clustering["sse"])
a=clustering["sse"]
import csv
with open("Kmeans_SSE.csv", "w") as fp_out:
print(clustering["sse"],file=fp_out)