-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo23.py
63 lines (53 loc) · 1.91 KB
/
demo23.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
from copy import deepcopy
import numpy as np
import matplotlib.pyplot as plt
X = np.r_[np.random.randn(50, 2) + [2, 2], np.random.randn(50, 2) + [0, -2],
np.random.randn(50, 2) + [-2, 2]]
print(X)
[plt.scatter(e[0], e[1], c='g', s=7) for e in X]
# find k (kmean, center point)
k = 3
C_x = np.zeros(k)
C_y = np.zeros(k)
counter = 0
for i in np.random.choice(range(0, 150), size=3):
C_x[counter] = X[i, 0]
C_y[counter] = X[i, 1]
counter += 1
# C_x = np.random.uniform(np.min(X[:, 0]), np.max(X[:, 0]), size=k)
# C_y = np.random.uniform(np.min(X[:, 1]), np.max(X[:, 1]), size=k)
# C_x = np.random.randint(np.min(X), np.max(X), size=k)
# C_y = np.random.randint(np.min(X), np.max(X), size=k)
C = np.array(list(zip(C_x, C_y)), dtype=np.float32)
plt.scatter(C_x, C_y, marker='*', c='#C02244')
plt.show()
def dist(a, b, axis=1):
return np.linalg.norm(a - b, axis=axis)
C_old = np.zeros(C.shape)
delta = dist(C, C_old, None)
clusters = np.zeros((len(X)))
def plot_kmean(current_cluster, delta):
colors = ['r', 'g', 'b', 'y', 'c', 'm']
fig, ax = plt.subplots()
for i in range(k):
points = np.array([X[j] for j in range(len(X)) if current_cluster[j] == i])
ax.scatter(points[:, 0], points[:, 1], s=7, c=colors[i])
plt.scatter(C[:, 0], C[:, 1], marker='*', c='#C02244')
plt.title('delta will be:%.4f' % delta)
plt.plot()
plt.show()
while delta != 0:
print('start a new interation')
# calculate each point distances and assign to new cluster
for i in range(len(X)):
distances = dist(X[i], C)
cluster = np.argmin(distances)
clusters[i] = cluster
C_old = deepcopy(C)
# move kmeans center to new place
for i in range(k):
points = [X[j] for j in range(len(X)) if clusters[j] == i]
C[i] = np.mean(points, axis=0)
# calculate distance from current clusters
delta = dist(C, C_old, None)
plot_kmean(clusters, delta)