-
Notifications
You must be signed in to change notification settings - Fork 1
/
kmeans.py
431 lines (339 loc) · 12.7 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
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
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from submission import Submission
class KmeansGrader(Submission):
# Random Test Cases
X = np.sin(np.arange(1, 166)).reshape(15, 11, order='F')
Z = np.cos(np.arange(1, 122)).reshape(11, 11, order='F')
C = Z[:5, :]
idx = np.arange(1, 16) % 3
def __init__(self):
part_names = ['Find Closest Centroids (k-Means)',
'Compute Centroid Means (k-Means)',
'PCA',
'Project Data (PCA)',
'Recover Data (PCA)']
super().__init__('k-means-clustering-and-pca', part_names)
def __iter__(self):
for part_id in range(1, 6):
try:
func = self.functions[part_id]
# Each part has different expected arguments/different function
if part_id == 1:
res = 1 + func(self.X, self.C)
elif part_id == 2:
res = func(self.X, self.idx, 3)
elif part_id == 3:
U, S = func(self.X)
res = np.hstack([U.ravel('F'), np.diag(S).ravel('F')]).tolist()
elif part_id == 4:
res = func(self.X, self.Z, 5)
elif part_id == 5:
res = func(self.X[:, :5], self.Z, 5)
else:
raise KeyError
yield part_id, res
except KeyError:
yield part_id, 0
def run_kmeans(X,
centroids,
find_closest_centroids,
compute_centroids,
max_iters=10,
plot_progress=False):
"""
Runs the K-means algorithm.
Parameters
----------
X : array_like
The data set of size (m, n). Each row of X is a single example of n dimensions. The
data set is a total of m examples.
centroids : array_like
Initial centroid location for each clusters. This is a matrix of size (K, n). K is the total
number of clusters and n is the dimensions of each data point.
find_closest_centroids : func
A function (implemented by student) reference which computes the cluster assignment for
each example.
compute_centroids : func
A function(implemented by student) reference which computes the centroid of each cluster.
max_iters : int, optional
Specifies the total number of interactions of K-Means to execute.
plot_progress : bool, optional
A flag that indicates if the function should also plot its progress as the learning happens.
This is set to false by default.
Returns
-------
centroids : array_like
A (K x n) matrix of the computed (updated) centroids.
idx : array_like
A vector of size (m,) for cluster assignment for each example in the dataset. Each entry
in idx is within the range [0 ... K-1].
anim : FuncAnimation, optional
A matplotlib animation object which can be used to embed a video within the jupyter
notebook. This is only returned if `plot_progress` is `True`.
"""
K = centroids.shape[0]
idx = None
idx_history = []
centroid_history = []
for i in range(max_iters):
idx = find_closest_centroids(X, centroids)
if plot_progress:
idx_history.append(idx)
centroid_history.append(centroids)
centroids = compute_centroids(X, idx, K)
if plot_progress:
fig = plt.figure()
anim = FuncAnimation(fig, plot_progress_kmeans,
frames=max_iters,
interval=500,
repeat_delay=2,
fargs=(X, centroid_history, idx_history))
return centroids, idx, anim
return centroids, idx
def plot_progress_kmeans(i, X, centroid_history, idx_history):
"""
A helper function that displays the progress of k-Means as it is running. It is intended for use
only with 2D data. It plots data points with colors assigned to each centroid. With the
previous centroids, it also plots a line between the previous locations and current locations
of the centroids.
Parameters
----------
i : int
Current iteration number of k-means. Used for matplotlib animation function.
X : array_like
The dataset, which is a matrix (m x n). Note since the plot only supports 2D data, n should
be equal to 2.
centroid_history : list
A list of computed centroids for all iteration.
idx_history : list
A list of computed assigned indices for all iterations.
"""
K = centroid_history[0].shape[0]
plt.gcf().clf()
cmap = plt.cm.rainbow
norm = mpl.colors.Normalize(vmin=0, vmax=2)
for k in range(K):
current = np.stack([c[k, :] for c in centroid_history[:i + 1]], axis=0)
plt.plot(current[:, 0], current[:, 1],
'-Xk',
mec='k',
lw=2,
ms=10,
mfc=cmap(norm(k)),
mew=2)
plt.scatter(X[:, 0], X[:, 1],
c=idx_history[i],
cmap=cmap,
marker='o',
s=8 ** 2,
linewidths=1, )
plt.grid(False)
plt.title('Iteration number %d' % (i + 1))
def square_distance(x1, x2):
return np.sum(np.square(x1 - x2))
def find_closest_centroids(X, centroids):
"""
Computes the centroid memberships for every example.
Parameters
----------
X : array_like
The dataset of size (m, n) where each row is a single example.
That is, we have m examples each of n dimensions.
centroids : array_like
The k-means centroids of size (K, n). K is the number
of clusters, and n is the the data dimension.
Returns
-------
idx : array_like
A vector of size (m, ) which holds the centroids assignment for each
example (row) in the dataset X.
Instructions
------------
Go over every example, find its closest centroid, and store
the index inside `idx` at the appropriate location.
Concretely, idx[i] should contain the index of the centroid
closest to example i. Hence, it should be a value in the
range 0..K-1
Note
----
You can use a for-loop over the examples to compute this.
"""
# Set K
K = centroids.shape[0]
# You need to return the following variables correctly.
idx = np.zeros(X.shape[0], dtype=int)
# ====================== YOUR CODE HERE ======================
# =============================================================
return idx
def compute_centroids(X, idx, K):
"""
Returns the new centroids by computing the means of the data points
assigned to each centroid.
Parameters
----------
X : array_like
The datset where each row is a single data point. That is, it
is a matrix of size (m, n) where there are m datapoints each
having n dimensions.
idx : array_like
A vector (size m) of centroid assignments (i.e. each entry in range [0 ... K-1])
for each example.
K : int
Number of clusters
Returns
-------
centroids : array_like
A matrix of size (K, n) where each row is the mean of the data
points assigned to it.
Instructions
------------
Go over every centroid and compute mean of all points that
belong to it. Concretely, the row vector centroids[i, :]
should contain the mean of the data points assigned to
cluster i.
Note:
-----
You can use a for-loop over the centroids to compute this.
"""
# Useful variables
m, n = X.shape
# You need to return the following variables correctly.
centroids = np.zeros((K, n))
# ====================== YOUR CODE HERE ======================
# =============================================================
return centroids
def kmeans_init_centroids(X, K):
"""
This function initializes K centroids that are to be used in K-means on the dataset x.
Parameters
----------
X : array_like
The dataset of size (m x n).
K : int
The number of clusters.
Returns
-------
centroids : array_like
Centroids of the clusters. This is a matrix of size (K x n).
Instructions
------------
You should set centroids to randomly chosen examples from the dataset X.
"""
m, n = X.shape
# You should return this values correctly
centroids = np.zeros((K, n))
# ====================== YOUR CODE HERE ======================
# =============================================================
return centroids
def pca(X):
"""
Run principal component analysis.
Parameters
----------
X : array_like
The dataset to be used for computing PCA. It has dimensions (m x n)
where m is the number of examples (observations) and n is
the number of features.
Returns
-------
U : array_like
The eigenvectors, representing the computed principal components
of X. U has dimensions (n x n) where each column is a single
principal component.
S : array_like
A vector of size n, contaning the singular values for each
principal component. Note this is the diagonal of the matrix we
mentioned in class.
Instructions
------------
You should first compute the covariance matrix. Then, you
should use the "svd" function to compute the eigenvectors
and eigenvalues of the covariance matrix.
Notes
-----
When computing the covariance matrix, remember to divide by m (the
number of examples).
"""
# Useful values
m, n = X.shape
# You need to return the following variables correctly.
U = np.zeros(n)
S = np.zeros(n)
# ====================== YOUR CODE HERE ======================
# ============================================================
return U, S
def project_data(X, U, K):
"""
Computes the reduced data representation when projecting only
on to the top K eigenvectors.
Parameters
----------
X : array_like
The input dataset of shape (m x n). The dataset is assumed to be
normalized.
U : array_like
The computed eigenvectors using PCA. This is a matrix of
shape (n x n). Each column in the matrix represents a single
eigenvector (or a single principal component).
K : int
Number of dimensions to project onto. Must be smaller than n.
Returns
-------
Z : array_like
The projects of the dataset onto the top K eigenvectors.
This will be a matrix of shape (m x k).
Instructions
------------
Compute the projection of the data using only the top K
eigenvectors in U (first K columns).
For the i-th example X[i,:], the projection on to the k-th
eigenvector is given as follows:
x = X[i, :]
projection_k = np.dot(x, U[:, k])
"""
# You need to return the following variables correctly.
Z = np.zeros((X.shape[0], K))
# ====================== YOUR CODE HERE ======================
# =============================================================
return Z
def recover_data(Z, U, K):
"""
Recovers an approximation of the original data when using the
projected data.
Parameters
----------
Z : array_like
The reduced data after applying PCA. This is a matrix
of shape (m x K).
U : array_like
The eigenvectors (principal components) computed by PCA.
This is a matrix of shape (n x n) where each column represents
a single eigenvector.
K : int
The number of principal components retained
(should be less than n).
Returns
-------
X_rec : array_like
The recovered data after transformation back to the original
dataset space. This is a matrix of shape (m x n), where m is
the number of examples and n is the dimensions (number of
features) of original datatset.
Instructions
------------
Compute the approximation of the data by projecting back
onto the original space using the top K eigenvectors in U.
For the i-th example Z[i,:], the (approximate)
recovered data for dimension j is given as follows:
v = Z[i, :]
recovered_j = np.dot(v, U[j, :K])
Notice that U[j, :K] is a vector of size K.
"""
# You need to return the following variables correctly.
X_rec = np.zeros((Z.shape[0], U.shape[0]))
# ====================== YOUR CODE HERE ======================
# =============================================================
return X_rec