Skip to content

Commit 55e183c

Browse files
authored
Update kMeans.py
1 parent ef9b25b commit 55e183c

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

Clustering/kMeans - Standard/kMeans.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,15 @@ def UpdateMean(n,mean,item):
7272

7373
return mean;
7474

75-
def FindClusters(k,items,belongsTo):
76-
clusters = [[] for i in range(k)]; #Init clusters
75+
def FindClusters(means,items):
76+
clusters = [[] for i in range(len(means))]; #Init clusters
7777

78-
for i in range(len(items)):
79-
item = items[i];
80-
classification = belongsTo[i];
78+
for item in items:
79+
#Classify item into a cluster
80+
index = Classify(means,item);
8181

82-
clusters[classification].append(item);
82+
#Add item to cluster
83+
clusters[index].append(item);
8384

8485
return clusters;
8586

@@ -110,7 +111,7 @@ def CalculateMeans(k,items,maxIterations=100000):
110111

111112
#Initialize clusters, the array to hold
112113
#the number of items in a class
113-
clusters = [0 for i in range(len(means))];
114+
clusterLengths = [0 for i in range(len(means))];
114115

115116
#An array to hold the cluster an item is in
116117
belongsTo = [0 for i in range(len(items))];
@@ -119,16 +120,15 @@ def CalculateMeans(k,items,maxIterations=100000):
119120
for e in range(maxIterations):
120121
#If no change of cluster occurs, halt
121122
noChange = True;
122-
clusters = [0 for i in range(len(means))];
123123
for i in range(len(items)):
124124
item = items[i];
125125
#Classify item into a cluster and update the
126126
#corresponding means.
127127

128128
index = Classify(means,item);
129129

130-
clusters[index] += 1;
131-
means[index] = UpdateMean(clusters[index],means[index],item);
130+
clusterLengths[index] += 1;
131+
means[index] = UpdateMean(clusterLengths[index],means[index],item);
132132

133133
#Item changed cluster
134134
if(index != belongsTo[i]):
@@ -140,9 +140,7 @@ def CalculateMeans(k,items,maxIterations=100000):
140140
if(noChange):
141141
break;
142142

143-
clusters = FindClusters(k,items,belongsTo);
144-
145-
return means, clusters;
143+
return means;
146144

147145

148146
###_Main_###
@@ -151,7 +149,8 @@ def main():
151149

152150
k = 3;
153151

154-
means, clusters = CalculateMeans(k,items);
152+
means = CalculateMeans(k,items);
153+
clusters = FindClusters(means,items);
155154
print means;
156155
print clusters;
157156

0 commit comments

Comments
 (0)