-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClustering streaming kmeans
189 lines (166 loc) · 5.23 KB
/
Clustering streaming kmeans
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
import numpy as np
import math
dataset=[]
for i in range(0,50):
theta=np.random.uniform(0,2*(math.pi))
phi=np.random.uniform(-math.pi/1,math.pi/2)
radius=np.random.uniform(0,9)
x=radius*math.cos(phi)*math.cos(theta)
y=radius*math.cos(phi)*math.sin(theta)
z=radius*math.sin(phi)
dataset.append([x,y,z])
for i in range(50,100):
theta=np.random.uniform(0,2*(math.pi))
phi=np.random.uniform(-math.pi/1,math.pi/2)
radius=np.random.uniform(0,9)
x=radius*math.cos(phi)*math.cos(theta)
y=5+radius*math.cos(phi)*math.sin(theta)
z=radius*math.sin(phi)
dataset.append([x,y,z])
#dataset
len(dataset)
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
c1=dataset[np.random.randint(100)]
c2=dataset[np.random.randint(100)]
dataset=np.array(dataset)
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.scatter(dataset[:50,0], dataset[:50,1], dataset[:50,2], color = 'red', marker = 'o')
ax.scatter(dataset[50:,0], dataset[50:,1], dataset[50:,2], color = 'blue', marker = 'o')
ax.scatter(c1[0],c1[1],c1[2],color='red',marker='*')
ax.scatter(c2[0],c2[1],c2[2],color='blue',marker='*')
plt.show()
def euclidian_distance(point1,point2):
return (point2[0]-point1[0])**2+(point2[1]-point1[1])**2+(point2[2]-point1[2])**2
def get_new_center(Cluster):
x=0
y=0
z=0
for i in range(len(Cluster)):
x+=dataset[Cluster[i]][0]
y+=dataset[Cluster[i]][1]
z+=dataset[Cluster[i]][2]
x=x/len(Cluster)
y=y/len(Cluster)
z=z/len(Cluster)
c=(x,y,z)
return c
def get_objective():
obj=0
for item in Cluster1:
obj+=euclidian_distance(dataset[item],c1)
for item in Cluster2:
obj+=euclidian_distance(dataset[item],c2)
return obj
def affect_cluster():
for i in range(0,100):
a=euclidian_distance(dataset[i],c1)
b=euclidian_distance(dataset[i],c2)
if a<b:
Cluster1.append(i)
else:
Cluster2.append(i)
Counter_iteration=[]
objective_after_iteration=[]
objective=0
#initialization (iteration 0)
c1=dataset[np.random.randint(100)] # random first center
c2=dataset[np.random.randint(100)] # random first center
Cluster1=[]
Cluster2=[]
affect_cluster()
c1=get_new_center(Cluster1)
c2=get_new_center(Cluster2)
Counter_iteration.append(0)
objective_after_iteration.append(get_objective())
for i in range(1,10):
Cluster1=[]
Cluster2=[]
affect_cluster()
c1=get_new_center(Cluster1)
c2=get_new_center(Cluster2)
objective=get_objective()
if i<5 or objective<objective_after_iteration[len(objective_after_iteration)-1]:
Counter_iteration.append(i)
objective_after_iteration.append(get_objective())
att6_count=Counter_iteration
att6_objec=objective_after_iteration
plt.scatter(att3_count,att3_objec)
plt.plot(att3_count,att3_objec)
plt.scatter(att1_count,att1_objec)
plt.plot(att1_count,att1_objec)
plt.scatter(att2_count,att2_objec)
plt.plot(att2_count,att2_objec)
plt.scatter(att4_count,att4_objec)
plt.plot(att4_count,att4_objec)
plt.scatter(att5_count,att5_objec)
plt.plot(att5_count,att5_objec)
plt.scatter(att6_count,att6_objec)
plt.plot(att6_count,att6_objec)
plt.show()
dataset=[]
for i in range(0,50):
theta=np.random.uniform(0,2*(math.pi))
phi=np.random.uniform(-math.pi/1,math.pi/2)
radius=np.random.uniform(0,9)
x=radius*math.cos(phi)*math.cos(theta)
y=radius*math.cos(phi)*math.sin(theta)
z=radius*math.sin(phi)
dataset.append([x,y,z])
for i in range(50,100):
theta=np.random.uniform(0,2*(math.pi))
phi=np.random.uniform(-math.pi/1,math.pi/2)
radius=np.random.uniform(0,9)
x=radius*math.cos(phi)*math.cos(theta)
y=5+radius*math.cos(phi)*math.sin(theta)
z=radius*math.sin(phi)
dataset.append([x,y,z])
#dataset
len(dataset)
def probability():
total_distance=0
proba=[]
for i in range(100):
total_distance+=euclidian_distance(dataset[i],c1)
for i in range(100):
proba.append(euclidian_distance(dataset[i],c1)/total_distance)
return proba
Counter_iteration=[]
objective_after_iteration=[]
objective=0
#initialization (iteration 0)
c1=dataset[np.random.randint(100)] # random first center
c2=dataset[np.random.choice(list(range(100)),p=probability())]
Cluster1=[]
Cluster2=[]
affect_cluster()
c1=get_new_center(Cluster1)
c2=get_new_center(Cluster2)
Counter_iteration.append(0)
objective_after_iteration.append(get_objective())
for i in range(1,10):
Cluster1=[]
Cluster2=[]
affect_cluster()
c1=get_new_center(Cluster1)
c2=get_new_center(Cluster2)
objective=get_objective()
if i<5 or objective<objective_after_iteration[len(objective_after_iteration)-1]:
Counter_iteration.append(i)
objective_after_iteration.append(get_objective())
att6_count=Counter_iteration
att6_objec=objective_after_iteration
plt.scatter(att3_count,att3_objec)
plt.plot(att3_count,att3_objec)
plt.scatter(att1_count,att1_objec)
plt.plot(att1_count,att1_objec)
plt.scatter(att2_count,att2_objec)
plt.plot(att2_count,att2_objec)
plt.scatter(att4_count,att4_objec)
plt.plot(att4_count,att4_objec)
plt.scatter(att5_count,att5_objec)
plt.plot(att5_count,att5_objec)
plt.scatter(att6_count,att6_objec)
plt.plot(att6_count,att6_objec)
plt.show()