-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmeans.py
93 lines (71 loc) · 2.89 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
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(42)
def euclidean_distance(x1, x2):
return np.sqrt(np.dot(x1 - x2, x1 - x2))
class KMeans:
def __init__(self, K=5, max_iters=100, plot_steps=False):
self.K = K
self.max_iters = max_iters
self.plot_steps = plot_steps
# list of sample indices for each cluster
self.clusters = [[] for _ in range(self.K)]
# Mean feature vector for each cluster
self.centroids = []
def predict(self, X):
self.X = X
self.num_samples, self.num_features = X.shape
# initialize our centroids
random_sample_indices = np.random.choice(
self.num_samples, self.K, replace=False
)
self.centroids = [self.X[idx] for idx in random_sample_indices]
# optimization
for _ in range(self.max_iters):
# update clusters
self.clusters = self._create_clusters(self.centroids)
if self.plot_steps: self.plot()
# update centroids
centroids_old = self.centroids
self.centroids = self._get_centroids(self.clusters)
if self.plot_steps: self.plot()
# check convergence
if self._is_converged(centroids_old, self.centroids):
break
return self._get_cluster_labels(self.clusters)
def _get_cluster_labels(self, clusters):
labels = np.empty(self.num_samples)
for cluster_idx, cluster in enumerate(clusters):
for sample_idx in cluster:
labels[sample_idx] = cluster_idx
return labels
def _create_clusters(self, centroids):
clusters = [[] for _ in range(self.K)]
for idx, sample in enumerate(self.X):
centroid_idx = self._closest_centroid(sample, centroids)
clusters[centroid_idx].append(idx)
return clusters
def _closest_centroid(self, sample, centroids):
distances = [euclidean_distance(sample, point) for point in centroids]
closest_idx = np.argmin(distances)
return closest_idx
def _get_centroids(self, clusters):
centroids = np.zeros((self.K, self.num_features))
for cluster_idx, cluster in enumerate(clusters):
cluster_mean = np.mean(self.X[cluster], axis=0)
centroids[cluster_idx] = cluster_mean
return centroids
def _is_converged(self, old_centroids, new_centroids):
distances = [
euclidean_distance(old_centroids[i], new_centroids[i])
for i in range(self.K)
]
return sum(distances) == 0
def plot(self):
fig, ax = plt.subplots(figsize=(12,8))
for i, index in enumerate(self.clusters):
point = self.X[index].T
ax.scatter(*point)
for point in self.centroids:
ax.scatter(*point, marker='x', color='black', linewidths=2)
plt.show()