-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClustering.py
More file actions
258 lines (192 loc) · 9.24 KB
/
Clustering.py
File metadata and controls
258 lines (192 loc) · 9.24 KB
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
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 24 00:01:37 2020
@author: Ruchika
"""
#####################################################################################################
#Clustering (Unsupervised Learning)
#####################################################################################################
from Vector_operations_on_data import Vector
def num_differences(v1: Vector, v2: Vector) -> int:
assert len(v1) == len(v2)
return len([x1 for x1, x2 in zip(v1,v2) if x1 != x2])
print(f'num_differences([1,2,3],[2,1,3]) = {num_differences([1,2,3],[2,1,3])}')
print(f'num_differences([1,2],[1,2]) = {num_differences([1,2],[1,2])}')
# Initialize cluster means
from typing import List
from Vector_operations_on_data import vector_mean
import random
def cluster_means(k: int,
imputs: List[Vector],
assignments: List[int]) -> List[Vector]:
# cluster i contains the inputs whose assignment is i
clusters = [[] for i in range(k)]
for input, assignment in zip(inputs, assignments):
clusters[assignment].append(input)
# if cluster is empty then just use a random point
return [vector_mean(cluster) if cluster else random.choice(inputs)
for cluster in clusters]
#####################################################################################################
# K-Means
#####################################################################################################
import itertools
import tqdm
from Vector_operations_on_data import squared_distance
class kMeans:
def __init__(self, k: int) -> None:
self.k = k # number of clusters
self.means = None
def classify(self, input: Vector)->int:
"""Return the index of the cluster closest to the input"""
return min(range(self.k),
key = lambda i: squared_distance(input,self.means[i]))
def train(self, inputs: List[Vector]) -> None:
# Start with random assignments
assignments = [random.randrange(self.k) for _ in inputs]
#print(inputs[:10])
#print(assignments[:10])
with tqdm.tqdm(itertools.count()) as t:
for _ in t:
# Compute means and find new assignments
self.means = cluster_means(self.k, inputs, assignments)
#print(self.means)
new_assignments = [self.classify(input) for input in inputs]
# Check how many assignments changed and if we are done
num_changed = num_differences(assignments, new_assignments)
if num_changed == 0:
return
# Otherwise keep the new assignments and compute new means
assignments = new_assignments
self.means = cluster_means(self.k, inputs, assignments)
t.set_description(f"changed: {num_changed}/{len(inputs)}")
# Example: meetups
inputs: List[List[float]] = [[-14,-5],[13,13],[20,23],[-19,-11],[-9,-16],[21,27],[-49,15],[26,13],[-46,5],[-34,-1],[11,15],[-49,0],[-22,-16],[19,28],[-12,-8],[-13,-19],[-41,8],[-11,-6],[-25,-9],[-18,-3]]
random.seed(12)
clusterer = kMeans(k = 3)
clusterer.train(inputs)
means = sorted(clusterer.means) # sort for the unit test
# Check that the means are close to what we expect
assert squared_distance(means[0], [-44, 5]) < 1
assert squared_distance(means[1], [-16, -10]) < 1
assert squared_distance(means[2], [18, 20]) < 1
random.seed(12)
clusterer = kMeans(k = 2)
clusterer.train(inputs)
means = sorted(clusterer.means)
assert len(means) == 2
assert squared_distance(means[0], [-26, -5]) < 1
assert squared_distance(means[1], [18, 20]) < 1
#####################################################################################################
# Finding optimal K
#####################################################################################################
from matplotlib import pyplot as plt
def squared_clustering_errors(inputs: List[Vector], k: int) -> float:
"""finds the total squared error from the k-means clustering of inputs"""
clusterer = kMeans(k)
clusterer.train(inputs)
means = clusterer.means
assignments = [clusterer.classify(input) for input in inputs]
return sum(squared_distance(input, means[cluster])
for input, cluster in zip(inputs, assignments))
ks = range(1, len(inputs) + 1)
errors = [squared_clustering_errors(inputs, k) for k in ks]
plt.plot(ks,errors)
plt.xticks(ks)
plt.xlabel("k")
plt.ylabel("total squared error")
plt.title("Total Error vs. # of clusters")
plt.show()
#####################################################################################################
# Bottom-up Hierarchical Clustering
#####################################################################################################
from typing import NamedTuple, Union, List
from Vector_operations_on_data import Vector
class Leaf(NamedTuple):
value: Vector
leaf1 = Leaf([10, 20])
leaf2 = Leaf([30, -15])
class Merged(NamedTuple):
children: tuple
order: int
merged = Merged((leaf1, leaf2), order = 1)
Cluster = Union[Leaf, Merged]
def get_values(cluster: Cluster) -> List[Vector]:
if isinstance(cluster, Leaf):
return [cluster.value]
else:
return [value
for child in cluster.children
for value in get_values(child)]
print(f"get_values(merged) = {get_values(merged)}")
from typing import Callable
from Vector_operations_on_data import distance
def cluster_distance(cluster1: Cluster,
cluster2: Cluster,
distance_agg: Callable = min) -> float:
"""Compute all the pairwise distances between cluster1 and cluster2 and apply the aggregation function
_distance_agg_ to the resulting list"""
return distance_agg([distance(v1,v2)]
for v1 in get_values(cluster1)
for v2 in get_values(cluster2))
def get_merge_order(cluster: Cluster) -> float:
if isinstance(cluster, Leaf):
return float('inf') # was never merged
else:
return cluster.order
from typing import Tuple
def get_children(cluster: Cluster):
if isinstance(cluster, Leaf):
return TypeError("Leaf has no children")
else:
return cluster.children
def bottom_up_cluster(inputs: List[Vector],
distance_agg: Callable = min) -> Cluster:
# Start with all leaves
clusters: List[Cluster] = [Leaf(input) for input in inputs]
def pair_distance(pair: Tuple[Cluster, Cluster]) -> float:
return cluster_distance(pair[0], pair[1], distance_agg)
# as long as we have more than one cluster left...
while len(clusters) > 1:
# find two closest clusters
c1, c2 = min(((cluster1, cluster2)
for i, cluster1 in enumerate(clusters)
for cluster2 in clusters[:i]),
key = pair_distance)
# remove them from the list of clusters
clusters = [c for c in clusters if c != c1 and c != c2]
# merge them using merge_order = # of clusters left
merged_cluster = Merged((c1,c2), order = len(clusters))
# and add their merge
clusters.append(merged_cluster)
# when there is only one cluster left, return it
return clusters[0]
inputs: List[List[float]] = [[-14,-5],[13,13],[20,23],[-19,-11],[-9,-16],[21,27],[-49,15],[26,13],[-46,5],[-34,-1],[11,15],[-49,0],[-22,-16],[19,28],[-12,-8],[-13,-19],[-41,8],[-11,-6],[-25,-9],[-18,-3]]
base_cluster = bottom_up_cluster(inputs)
def generate_clusters(base_cluster: Cluster,
num_clusters: int) -> List[Cluster]:
# start with a list with just the base cluster
clusters = [base_cluster]
# as long as we don't have enough clusters yet...
while len(clusters) < num_clusters:
# choose the last merged of our clusters
next_cluster = min(clusters, key = get_merge_order)
clusters = [c for c in clusters if c != next_cluster]
# and add its children to the list (i.e. unmerge it)
clusters.extend(get_children(next_cluster))
# once we have enough clusters, return those
return clusters
three_clusters = [get_values(cluster) for cluster in generate_clusters(base_cluster, 3)]
from matplotlib import pyplot as plt
for i, cluster, marker, color in zip([1,2,3],
three_clusters,
['D','o', '*'],
['r','g','b']):
xs, ys = zip(*cluster) # magic unzipping trick
plt.scatter(xs, ys, color = color, marker = marker)
# put a number at the mean of a cluster
x,y = vector_mean(cluster)
plt.plot(x,y, marker = '$' + str(i) + '$', color = 'black')
plt.title("User Locations -- 3 Bottom-up Clusters, Min")
plt.xlabel("blocks east of city center")
plt.ylabel("blocks north of city center")
plt.show()