-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSeqKM.py
53 lines (34 loc) · 947 Bytes
/
SeqKM.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
import random as rd
import math
import Kmeans
def euclidean_distance(img_a, img_b):
count = 0
for i in range(0, len(img_a)):
temp = img_a[i] - img_b[i]
count = (temp ** 2) + count
count = math.sqrt(count)
return count
def seqkm(k, Images, SampleSize):
print("SeqKM start")
v = []
PredictedLabels = []
f = k
while f > 0:
v.append(100)
f = f - 1
if SampleSize<len(Images) :
M = rd.choices(Images, k=SampleSize)
else :
M = Images
# print("choose " + str(k) + " centroid with kmeans++")
centers,label = Kmeans.KMeansPlusplus(M, k)
print("SeqKM done")
return v, PredictedLabels, centers
from sklearn.cluster import KMeans
def seqKM(k , A):
print("SeqKM start")
print("Kmeans++ done")
kmeans = KMeans(n_clusters=k,init='k-means++')
kmeans.fit(A)
print("SeqKM done")
return kmeans.labels_,kmeans.labels_