def getweight( city : Vec, node : Vec, net : List[Vec], k): #print(city) numerator = fi( math.fabs( city.distance( node ) ), k) denominator = 0 for each in net: denominator += fi( math.fabs( city.distance( each )), k) return numerator / denominator
clusters = {} # Count the number of iterations until convergence iterations = 0 # Will be used to determine if the algorithm is complete convergence = False # Initialize the means to random points in the general range of our dataset means = [Vec(randrange(minX, maxX), randrange(minY, maxY)) for x in range(k)] while convergence is False: iterations += 1 clusters = {i : [] for i in range(0,k)} for v in data: cluster = -1 distance = float('inf') for i in range(len(means)): d = Vec.distance(v, means[i]) if d < distance: distance = d cluster = i clusters[cluster].append(v) # Create a list to store the new means new_means = [] for key in clusters: # Sum the vectors in each cluster summed = Vec.zero() cluster = clusters[key] for v in cluster: summed += v # Find the mean of all the vectors in each cluster