text: import numpy as np
  import matplotlib.pyplot as plt
  import pandas as pd
  import random as rd
  
  class Kmeans:
      def __init__(self,X,K):
          self.X=X
          self.Output={}
          self.Centroids=np.array([]).reshape(self.X.shape[1],0)
          self.K=K
          self.m=self.X.shape[0]
          
      def kmeanspp(self,X,K):
          i=rd.randint(0,X.shape[0])
          Centroid_temp=np.array([X[i]])
          for k in range(1,K):
              D=np.array([]) 
              for x in X:
                  D=np.append(D,np.min(np.sum((x-Centroid_temp)**2)))
              prob=D/np.sum(D)
              cummulative_prob=np.cumsum(prob)
              r=rd.random()
              i=0
              for j,p in enumerate(cummulative_prob):
                  if r<p:
                      i=j
                      break
              Centroid_temp=np.append(Centroid_temp,[X[i]],axis=0)
          return Centroid_temp.T
      
      def fit(self,n_iter):
          #randomly Initialize the centroids
          self.Centroids=self.kmeanspp(self.X,self.K)
          
          """for i in range(self.K):
              rand=rd.randint(0,self.m-1)
              self.Centroids=np.c_[self.Centroids,self.X[rand]]"""
          
          #compute euclidian distances and assign clusters
          for n in range(n_iter):
              EuclidianDistance=np.array([]).reshape(self.m,0)
              for k in range(self.K):
                  tempDist=np.sum((self.X-self.Centroids[:,k])**2,axis=1)
                  EuclidianDistance=np.c_[EuclidianDistance,tempDist]
              C=np.argmin(EuclidianDistance,axis=1)+1
              #adjust the centroids
              Y={}
              for k in range(self.K):
                  Y[k+1]=np.array([]).reshape(2,0)
              for i in range(self.m):
                  Y[C[i]]=np.c_[Y[C[i]],self.X[i]]
          
              for k in range(self.K):
                  Y[k+1]=Y[k+1].T
              for k in range(self.K):
                  self.Centroids[:,k]=np.mean(Y[k+1],axis=0)
                  
              self.Output=Y
              
      
      def predict(self):
          return self.Output,self.Centroids.T
  
  
  dataset=pd.read_csv('Mall_Customers.csv')
  X = dataset.iloc[:, [2,4]].values
  m=X.shape[0]
  n_iter=100
  K=5
  kmeans=Kmeans(X,K)
  kmeans.fit(n_iter)
  Output,Centroids=kmeans.predict()
  color=['red','blue','green','cyan','magenta']
  labels=['cluster1','cluster2','cluster3','cluster4','cluster5']
  for k in range(K):
      plt.scatter(Output[k+1][:,0],Output[k+1][:,1],c=color[k],label=labels[k])
  plt.scatter(Centroids[:,0],Centroids[:,1],s=300,c='yellow',label='Centroids')
  plt.title('Clusters of customers')
  plt.xlabel('Age')
  plt.ylabel('Spending Score (1-100)')
  plt.legend()
  plt.show()
  