human
Write me a Python code to compute new means and repeat given the following:
I am working on the file "Clustering.ipynb".
The first task is:
k - means

Randomly select starting locations for k points
Assign data points to closest k point
If no data changed its cluster membership stop
If there was a change, compute new means and repeat
Can you write Python code for it?
assistant
#Here is Python code to compute new means and repeat
.

class KMeans:
    """k-means algo"""
    
    def __init__(self, k):
        self.k = k # number of clusters
        self.means = None # means of clusters
        
    def classify(self, input):
        """return the index of the cluster to closest to input"""
        return min(range(self.k),
                  key = lambda i: squared_distance(input, self.means[i]))
    
    def train(self, inputs):
        # choose k rand points as initials
        self.means = random.sample(inputs, self.k)
        assignments = None
        
        while True:
            # Find new assignments
            new_assignments = map(self.classify, inputs)
            
            # If nothing changed we're good to go
            if assignments == new_assignments:
                return
            
            # otherwise keep
            assignments = new_assignments
            
            # And compute new means based on assigments
            for i in range(self.k):
                # get points in cluster
                i_points = [p for p,a in zip(inputs, assignments) if a == i]
                
                # check for membership
                if i_points:
                    self.means[i] = vector_mean(i_points)

inputs = [[-14,-5],[13,13],[20,23],[-19,-11],[-9,-16],[21,27],[-49,15],[26,13],[-46,5],[-34,-1],[11,15],[-49,0],[-22,-16],[19,28],[-12,-8],[-13,-19],[-41,8],[-11,-6],[-25,-9],[-18,-3]]

random.seed(0)
clusterer = KMeans(2)
clusterer.train(inputs)

clusterer.means