-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkMeans
164 lines (132 loc) · 5.27 KB
/
kMeans
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
import math
import random
import pylab
import imageio
import os
import re
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-
class Point(object):
def __init__(self, coords):
#COORDS is list of coordinates
self.coords = coords
self.X = coords[0]
self.Y = coords[1]
def __repr__(self):
return str(self.coords)
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-
class Cluster(object):
def __init__(self, points):
self.points = points
self.centroid = self.calculateCentroid()
def calculateCentroid(self):
numPoints = len(self.points)
xCoords, yCoords = [],[]
xSum, ySum = 0,0
for p in self.points:
xCoords.append(p.X)
xSum += p.X
yCoords.append(p.Y)
ySum += p.Y
centroid = Point([xSum/len(xCoords), ySum/len(yCoords)])
return centroid
def renew(self, newPoints):
old_centroid = self.centroid
self.points = newPoints
self.centroid = self.calculateCentroid()
change = getDistance(old_centroid, self.centroid)
return change
def __repr__(self):
return str(self.points)
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-
def importData(numPoints):
return [generateRandomPoint(0,200) for i in range(numPoints)]
def generateRandomPoint(minimum, maximum):
return Point([random.randrange(minimum, maximum), random.randrange(minimum, maximum)])
def getDistance(a, b):
dist = math.sqrt(((a.X - b.X)**2) + ((a.Y - b.Y)**2))
return dist
def kMeans(points, numClusters, cutoff):
initialCentroids = random.sample(points,numClusters) #make random centroids
############### plot initial points ######################
xCentroids, yCentroids = [], []
for i in initialCentroids:
xCentroids.append(i.X)
yCentroids.append(i.Y)
xVals, yVals = [],[]
for i in points:
xVals.append(i.X)
yVals.append(i.Y)
pylab.figure(0)
pylab.title("Initial Points & Centroid")
pylab.plot(xVals, yVals, "bo")
pylab.plot(xCentroids, yCentroids, "x", c = "r", ms = 8)
pylab.savefig('Iteration 0.png')
pylab.close()
############### plot initial points ######################
clusterArray = []
for centroid in initialCentroids:
clusterArray.append(Cluster([centroid])) # make clusters with just centroid point
iteration = 0
while True:
lists = [[] for _ in clusterArray] #list of lists to hold points in cluster
iteration += 1
for p in points: # for each point
smallestDistance = getDistance(p,clusterArray[0].centroid)
clusterIndex = 0
for i in range(numClusters - 1):
distance = getDistance(p, clusterArray[i+1].
centroid)
if distance < smallestDistance:
smallestDistance = distance
clusterIndex = i+1 # find what cluster it belongs to
lists[clusterIndex].append(p) #assign it
biggestShift = 0.0
for i in range(numClusters):
shift = clusterArray[i].renew(lists[i]) # update cluster with new points
biggestShift = max(biggestShift, shift)
plot(clusterArray, iteration)
if biggestShift < cutoff:
print("Done at %s iterations" % iteration)
print("WAIT UNTIL FINISHED. DO NOT CLOSE")
break
return clusterArray, iteration
def plot(clusters, iteration):
pylab.figure(iteration)
symbols = ["o", "v", "<", "1", "2", "3", "4", "s", "p", "*", "h", "H", "+", "x", "D", "d", "|"]
pylab.title("Iteration %s" % iteration)
print("Iteration %s" % iteration)
symbolCounter = -1
for i in clusters:
symbolCounter += 1
xVals, yVals = [],[]
for ii in i.points:
xVals.append(ii.X)
yVals.append(ii.Y)
pylab.plot(xVals, yVals, symbols[symbolCounter])
pylab.plot(i.centroid.X, i.centroid.Y, "x",c = "r", ms = 8)
print(i.centroid.X, i.centroid.Y)
print("-----------------------------------------------------------")
pylab.savefig('Iteration %s.png' % iteration)
pylab.close()
def pngToGif():
images = []
path = os.path.dirname(os.path.abspath(__file__))
dirArray = natural_sort(os.listdir(path))
for file in dirArray:
if file.endswith(".png"):
images.append(imageio.imread(file))
os.remove(file)
kargs = { 'duration': 0.3 }
imageio.mimsave("animation.gif", images,'GIF', **kargs)
def natural_sort(l):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(l, key = alphanum_key)
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-
cutoff = 0.2
print("", flush = True)
numPoints = int(input("How many random points do you want? "))
numClusters = int(input("How many clusters do you want? "))
points = importData(numPoints)
finalClusterArray, iterations = kMeans(points, numClusters, cutoff) # run Main code
pngToGif()