<jupyter_start><jupyter_text>Implement K-means Part Two<jupyter_code>def euclidean_distace(x1, x2):
    return np.sum(np.square(x1 - x2))
def initialize_centroids(data, k):
    """Pick random k points as initial centroids"""
    num_samples, _ = data.shape
    idx = np.random.choice(num_samples, size=k, replace=False)
    return data[idx]
def kmeans(data, k, num_iter=100, threshold=1e-5):
    """"""
    minimal_distance = np.inf
    for _ in range(num_iter):
        centroids = initialize_centroids(data, k)
        centroids, distance = _kmeans(data, centroids, threshold=threshold)
        if distance < minimal_distance:
            best_centroids = centroids
            best_distance = distance
    return best_centroids, best_distance
def _kmeans(data, centroids, threshold=1e-5):
    diff = np.inf
    while diff > threshold:
        prev_diff = diff
        code, min_dist = vector_quantize(data, centroids)
        
        
        
def vector_quantize(data, centroids):
    dist = euclidean_distance(data, centroids)
    code = dist.argmin(axis=1)
    min_dist = dist[np.arange(len(code)), code]
    return code, min_dist
def update_cluster_means(data, code, k):
    num_samples, num_features = data.shape
    codebook = np.zeros((k, num_features), dtype=np.float64)
    data_count = np.zeros(k)
    
    obs_p = data
    for i in range(num_samples):
        label = code[i]
        # this should be pointer arithmetic
        cb_p = codebook + num_features * label
        
        for j in range(num_features):
            cb_p[j] += obs_p[j]
            
        data_count[label] += 1
        obs_b += num_features
    
    for i in range(k):
        cluster_size = obs_cont[i]
        
        if cluster_size > 0:
            for j in range(num_features):
                cd_p[j] /= cluster_size
        
        cb_p += num_features
        
    has_members = data_count > 0
    
    return<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09727225939269171, 'fraction_numerical': 0.008749356664951106, 'mean_word_length': 2.4776386404293382, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '467889', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 652, 'n_tokens_neox': 643, 'n_words': 145, 'path': 'examples/module_03_kmeans/notebooks/quantization_kmeans.ipynb', 'repo_name': 'DSE512/twelve', 'size': '1943'}