I am trying to write a k means algorithm and at very basic stage right now.
The code is as follows to randomly select the centres for clustering:
import numpy as np
import random
X = [2,3,5,8,12,15,18]
C = 2
def rand_center(ip,C):
centers = {}
for i in range (C):
if i>0:
while centers[i] != centers[i-1]:
centers[i] = random.choice(X)
else:
centers[i] = random.choice(X)
return centers
print (centers)
rand_center(X,C)
When I run this, it gives me KeyError:1
Can anyone guide me resolve this error?