<jupyter_start><jupyter_text>Generate Data<jupyter_code>from sklearn.datasets.samples_generator import make_blobs
X, y = make_blobs(n_samples=10, centers=3, n_features=3,random_state=0)<jupyter_output><empty_output><jupyter_text>K-Means<jupyter_code>def k_means(X, K, max_iter):
    """
    Args:
        X (numpy.array): continuous input data: N x p
        K (int): number of clusters
        max_iter (int): maximum number of iterations
    """
    N = len(X)
    means = X[numpy.random.choice(N, K, replace=False)] # K x p
    for _ in range(max_iter):
        distances = numpy.sum(((X[:,:,None]-means[:,:,None].T)**2), axis=1)
        assignments = numpy.zeros((K,N,1))
        assignments[numpy.argmin(distances, axis=1), numpy.arange(N), 0] = 1
        top = (assignments * X[None, :, :]).sum(axis=1)
        bottom = assignments.sum(axis=1)
        means = top/bottom
    return numpy.argmax(assignments, axis=0).squeeze()
k_means(X, 3, 100)
y<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15478615071283094, 'fraction_numerical': 0.018329938900203666, 'mean_word_length': 3.866336633663366, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '383204', 'license': 'MIT', 'n_tokens_mistral': 376, 'n_tokens_neox': 367, 'n_words': 70, 'path': 'unsupervised.ipynb', 'repo_name': 'LarsHH/ml-interview', 'size': '982'}