<jupyter_start><jupyter_text>The centroid is just the average of all points in that cluster:<jupyter_code>def upadteCentroids(points,centroids,clusters):
    d = []
    for x in points:
        np.argmin(distance(x, centroids)))
    print(d)
upadteCentroids(np.array([[1,1],[3,3],[4,1]]), np.array([[0,0],[1,1]]), 2)
np.array([[1,1],[3,3],[4,1]])<jupyter_output><empty_output><jupyter_text>For each point, find the distance to all centroids and assign it to the closest one. The distance function is already available above.<jupyter_code>def assignPoints(points,centroids,clusters):
    clusters = []
    for point in points:
        l = []
        for centroid in centroids:
            l.append(distance(point, centroid))
        clusters.append(np.argmin(l))
    return clusters
assignPoints(np.random.rand(100,2), [[1,0],[0,1],[1,1]], 3)<jupyter_output><empty_output><jupyter_text>We have a points array of length 100. We create "clusters" to be a list of the same length. We can use it to assign each point a cluster number. We will have k "centroids" and we just assign the first k points to be the centroids.<jupyter_code>def KMeans(points,k=3):
    clusters = np.zeros(len(points))
    centroids = deepcopy(points[:k])
    while assignPoints(points,centroids,clusters):
        upadteCentroids(points,centroids,clusters)
    plt.scatter([p[0] for p in points],[p[1] for p in points],c=clusters)
    plt.show()
KMeans(3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13452299245024021, 'fraction_numerical': 0.024021962937542895, 'mean_word_length': 4.762845849802371, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '133421', 'license': 'MIT', 'n_tokens_mistral': 522, 'n_tokens_neox': 513, 'n_words': 131, 'path': 'pair_programming_exercises/Pair Programming - Week 7, Day 3 (Brenner & Hiranya).ipynb', 'repo_name': 'athena15/metis', 'size': '1457'}