-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmeans++.py
121 lines (95 loc) · 3.49 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
from math import sqrt
import sys
import random
import functools
import numpy as np
import matplotlib.pyplot as plt
def readFile(localPathToFile):
f = open(localPathToFile, 'r')
listOfCordinates = []
for line in f.readlines():
listOfCordinates.append([float(i) for i in line.split()])
return listOfCordinates
def plotPoints(points):
for point in points:
plt.scatter(point[0], point[1], marker='x', color='red')
plt.show()
def get_cmap(n, name='hsv'):
return plt.cm.get_cmap(name, n)
def plotRes(clusters, centroids, k):
colors = plt.cm.rainbow(np.linspace(0, 1, k))
for i, sublist in enumerate(clusters):
x_vals = [point[0] for point in sublist]
y_vals = [point[1] for point in sublist]
plt.scatter(x_vals, y_vals, color=colors[i], label=f"Set {i+1}")
plt.scatter(centroids[i][0], centroids[i][1],
color=colors[i], marker='^', s=150)
plt.show()
def euclideanDistance(point1, point2):
distance = 0.0
for i in range(len(point1)):
distance += (point1[i] - point2[i]) ** 2
distance = sqrt(distance)
return distance
def funcD(pointX, centroids):
return min(map(lambda mu: sqrt((pointX[0] - mu[0])**2 + (pointX[1] - mu[1])**2), centroids))
def initiateCentroids(points, k):
centroids = []
centroids.append(random.choice(points))
for _ in range(1, k):
sumD = sum([funcD(y, centroids)**2 for y in points])
prob = [funcD(x, centroids)**2 / sumD for x in points]
cumulative_prob = np.cumsum(prob)
rand_val = np.random.rand()
for j, p in enumerate(cumulative_prob):
if rand_val < p:
centroids.append(points[j])
break
return centroids
def calcRSS(clusters):
rss = 0
for cluster in clusters:
muI = list(map(lambda x: x / len(cluster),
functools.reduce(lambda x,
y: [x[0] + y[0], x[1] + y[1]], cluster, [0, 0])))
rss += sum([sqrt((x[0] - muI[0])**2 + (x[1] - muI[1])**2)
for x in cluster])
return rss
def buildClusters(points, centroids, k):
clusters = [[] for _ in range(k)]
for point in points:
closestCentroid = list(map(lambda mu: euclideanDistance(point, mu), centroids)).index(
min(map(lambda mu: euclideanDistance(point, mu), centroids)))
clusters[closestCentroid].append(point)
return clusters
def sumListOfPoints(points):
return functools.reduce(lambda x, y: [x[0] + y[0], x[1] + y[1]], points, [0, 0])
def updateCentroids(clusters):
centroids = list(map(lambda cluster: list(
map(lambda x: x / len(cluster), sumListOfPoints(cluster))), clusters))
return centroids
def solve(points, k):
rssOld = 0
rss = 1
centroids = initiateCentroids(points, k)
while (rss != rssOld):
rssOld = rss
clusters = buildClusters(points, centroids, k)
centroids = updateCentroids(clusters)
rss = calcRSS(clusters)
return clusters, centroids, rss
points = readFile(sys.argv[1])
k = int(sys.argv[2])
clustersFinal, centroidsFinal, rssFinal = solve(points, k)
if(len(sys.argv) < 4):
restarts = 0
else:
restarts = int(sys.argv[3])
for _ in range(restarts):
clusters, centroids, rss = solve(points, k)
if (rss < rssFinal):
clustersFinal = clusters
centroidsFinal = centroids
rssFinal = rss
colors = plt.cm.rainbow(np.linspace(0, 1, k))
plotRes(clustersFinal, centroidsFinal, k)