Hi
My code is working fine. But every time when I run the code from starting the clusters color is not same as it was in previous time ,it’s sort of shuffled, so it’s fine ? I’m sharing with you my code please check if there is a problem in code:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
#data set preparation
X,y=make_blobs(n_samples=500,n_features=2,centers=5,random_state=3)
plt.figure(0)
plt.grid(True)
plt.scatter(X[:,0],X[:,-1])
plt.show()
#cluster initialization
k=5
color=[‘green’,‘red’,‘blue’,‘yellow’,‘orange’]
clusters={}
for i in range(k):
center=10*(2*np.random.random((X.shape[1],))-1)
points=[]
cluster={
'center':center,
'points':points,
'color':color[i]
}
clusters[i]=cluster
def distance(v1,v2):
return np.sqrt(np.sum(v1-v2)**2)
def assignPointToClusters(clusters): #E-step(expectation)
for ix in range(X.shape[0]):
dist=[]
curr_x=X[ix]
for kx in range(k):
d=distance(curr_x,clusters[kx]['center'])
dist.append(d)
current_cluster=np.argmin(dist)
clusters[current_cluster]['points'].append(curr_x)
def updateClusters(clusters):
for kx in range(k):
pts=np.array(clusters[kx][‘points’])
if pts.shape[0]>0:
new_u=pts.mean(axis=0)
clusters[kx]['center']=new_u
clusters[kx]['points']=[] #clear the list
def plotClusters(clusters):
for kx in range(k):
pts=np.array(clusters[kx]['points'])
try:
plt.scatter(pts[:,0],pts[:,1],color=clusters[kx]['color'])
except:
pass
#plot the cluster center
uk=clusters[kx]['center']
plt.scatter(uk[0],uk[1],color='black',marker='*')
assignPointToClusters(clusters)
plotClusters(clusters)
#run following repeatedly till convergence
assignPointToClusters(clusters) #e-step
plotClusters(clusters)
updateClusters(clusters) #m-step