-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkmeans.py
57 lines (51 loc) · 1.65 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
from util import *
import matplotlib.pyplot as plt
plt.ion()
def distmat(p, q):
"""Computes pair-wise L2-distance between columns of p and q."""
d, pn = p.shape
d, qn = q.shape
pmag = np.sum(p**2, axis=0).reshape(1, -1)
qmag = np.sum(q**2, axis=0).reshape(1, -1)
dist = qmag + pmag.T - 2 * np.dot(p.T, q)
dist = (dist >= 0) * dist # Avoid small negatives due to numerical errors.
return np.sqrt(dist)
def KMeans(x, K, iters):
"""Cluster x into K clusters using K-Means.
Inputs:
x: Data matrix, with one data vector per column.
K: Number of clusters.
iters: Number of iterations of K-Means to run.
Outputs:
means: Cluster centers, with one cluster center in each column.
"""
N = x.shape[1]
perm = np.arange(N)
np.random.shuffle(perm)
means = x[:, perm[:K]]
dist = np.zeros((K, N))
for ii in xrange(iters):
print('Kmeans iteration = %04d' % (ii+1))
for k in xrange(K):
dist[k, :] = distmat(means[:, k].reshape(-1, 1), x)
assigned_class = np.argmin(dist, axis=0)
for k in xrange(K):
means[:, k] = np.mean(x[:, (assigned_class == k).nonzero()[0]], axis=1)
return means
def ShowMeans(means, number=0):
"""Show the cluster centers as images."""
plt.figure(number)
plt.clf()
for i in xrange(means.shape[1]):
plt.subplot(1, means.shape[1], i+1)
plt.imshow(means[:, i].reshape(48, 48), cmap=plt.cm.gray)
plt.draw()
raw_input('Press Enter.')
def main():
K = 7
iters = 200
inputs_train, inputs_valid, inputs_test, target_train, target_valid, target_test = LoadData('../toronto_face.npz')
means = KMeans(inputs_train, K, iters)
ShowMeans(means, 0)
if __name__ == '__main__':
main()