1

I followed the pseudo code for k-means clustering to write this code. This code gives different answers when initialized the clusters' centroids with different values and none of those answers are correct. Can you help me please? I tested with 15 nodes, tolerance = 0.00001 and iterations = 100000 Thanks in advance.

class kMeans:
  def __init__(self, coordinates, tolerance, iter, nof):
    self.grid = coordinates
    self.N = coordinates.shape[0]
    self.t = tolerance
    self.nof = nof    
    self.f = None

  def kMeans(self, nof):
      
        assign = [0]*self.N
        self.fac = np.empty([nof,2])
        for i in range(nof):
              for j in range(2):
                self.fac[i,j] = self.grid[i+10,j]

        for itr in range(iter):
          for n in range(self.N):
            distance = [0]*nof
            for f in range(nof):
              distance[f] = math.sqrt((self.grid[n,0]-self.fac[f,0])**2 + (self.grid[n,1]-self.fac[f,1])**2 )
            assign[n] = np.argmin(distance)

          for fa in range(nof):
            l = []
            x,y = 0,0
            for asg in range(self.N):
              if fa == assign[asg]:
                l.append(asg)
            x = np.mean(self.grid[l,0])
            y = np.mean(self.grid[l,1])

            if abs(x-self.fac[fa,0]) >= self.t:
              self.fac[fa,0] = x
            if abs(y-self.fac[fa,1]) >= self.t:
              self.fac[fa,1] = y
          continue
    
        print('dist:',distance)
        print('assign:',assign)
        print('fac:',self.fac)
        print('locate:', self.grid[l,1])
        self.f = self.fac
        return self.fac

'''
  • Consider providing a small example of the `coordinates` that can be used to test your code, together with any code other users would need to run the code above, and also the actual and expected results of running it. – Helder Jan 05 '21 at 10:22

0 Answers0