Ejemplo n.º 1
0
    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
        count = len(cluster)
        if count > 0:
            new_means.append(summed / Vec(count, count))
        else:
            new_means.append(means[key])
    
    # If the new means are the same as the old, the algorithm has converged
    convergence = means == new_means
    means = new_means