forked from SleepyBag/Statistical-Learning-Methods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKMeans.py
62 lines (53 loc) · 2.11 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
import os
import numpy as np
from matplotlib import pyplot as plt
import sys
from pathlib import Path
sys.path.append(str(Path(os.path.abspath(__file__)).parent.parent))
from utils import euc_dis
class KMeans:
def __init__(self, k, max_iterations=1000):
self.k = k
self.max_iterations = max_iterations
def fit(self, X):
"""
X is a matrix shaped of [data_size, feature_size]
"""
data_size, feature_size = X.shape
self.centers = X[np.random.choice(data_size, self.k, replace=False)]
pre_centers = self.centers - 1
step = 0
while (pre_centers != self.centers).any():
pre_centers = self.centers.copy()
dis = euc_dis(X[:, None, :], self.centers[None, :, :])
cluster = dis.argmin(axis=-1)
for i in range(self.k):
self.centers[i] = X[cluster == i].mean(axis=0)
step += 1
if step == self.max_iterations:
break
def predict(self, X):
dis = euc_dis(X[:, None, :], self.centers[None, :, :])
return dis.argmin(axis=-1)
if __name__ == "__main__":
def demonstrate(X, k, desc):
k_means = KMeans(k=k)
k_means.fit(X)
pred = k_means.predict(X)
# plot
plt.scatter(X[:,0], X[:,1], c=pred, s=20)
plt.title(desc)
plt.show()
# -------------------------- Example 1 ----------------------------------------
X = np.array([[0, 0], [0, 1], [1, 0], [2, 2], [2, 1], [1, 2]]).astype(float)
demonstrate(X, 2, "Example 1")
# -------------------------- Example 2 ----------------------------------------
X = np.concatenate([
np.random.normal([0, 0], [.3, .3], [100, 2]),
np.random.normal([0, 1], [.3, .3], [100, 2]),
np.random.normal([1, 0], [.3, .3], [100, 2]),
]).astype(float)
demonstrate(X, 3, "Example 2")
# -------------------------- Example 3 ----------------------------------------
X = np.array([[0, 0], [0, 1], [0, 3]]).astype(float)
demonstrate(X, 2, "Example 3: K-Means doesn't always return the best answer. (try to run multiple times!)")