Skip to content

Commit

Permalink
Update Clustering.py
Browse files Browse the repository at this point in the history
  • Loading branch information
explorer12345 authored Dec 15, 2019
1 parent 445a641 commit 14e711d
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions Clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,24 @@ def classify_a_point(point, groups, k):
index = freq.index(max(freq))
return index

def cluster(data, no_of_clusters, k):
def cluster(data, no_of_clusters, k, max_iterations):
groups=initialize(data, no_of_clusters)
groups=[[element] for element in groups]
for i in range(data.shape[0]):
group_no = classify_a_point(data[i,:], groups,k)
groups[group_no].append(data[i,:])
for n in range(max_iterations):
for i in range(data.shape[0]):
group_no = classify_a_point(data[i,:], groups,k)
groups[group_no].append(data[i,:])
return groups

def plot_clusters(groups):
colors = cm.rainbow(np.linspace(0, 1, len(groups)))
for group in range(len(groups)):
plt.scatter(*zip(*groups[group]), color=colors)
plt.show()

plot_clusters(cluster(data, 4, 5,5))





Expand Down

0 comments on commit 14e711d

Please sign in to comment.