<jupyter_start><jupyter_text>Example: K-Means<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import scipy.spatial.distance as sp_dist
import seaborn

from scratch.algos.kmeans import KMeans

plt.rcParams["figure.figsize"] = (15, 30)
seaborn.set(font_scale=1.5)
seaborn.set_style("whitegrid")
X = np.loadtxt("../data/Q4Test.txt", skiprows=1)
N = X.shape[0]

idx = np.array(range(N))
idx_train = np.random.choice(idx, 140, replace=False)
idx_valid = np.delete(idx, idx_train, axis=0)

Xtrain = X[idx_train, :]
Xvalid = X[idx_valid, :]

print(Xtrain.shape)
print(Xvalid.shape)
n_clusts = 3

m = KMeans(n_clusts=n_clusts)
m.fit(X=Xtrain)

preds = m.predict(X=Xvalid)

print(m.centers)
print(m.labels)
print(preds)
# cluster convergence
its = len(m.trajectory_labels)

fig, ax = plt.subplots(its, 1)

for i in range(its):
    for j in range(n_clusts):
        tmp = Xtrain[m.trajectory_labels[i] == j]
        ax[i].set_title("Iteration: " + str(i))
        ax[i].scatter(tmp[:,0], tmp[:,1])
        ax[i].scatter(m.trajectory_centers[i][:,0], m.trajectory_centers[i][:,1], c="black", s=100)

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16768558951965065, 'fraction_numerical': 0.01921397379912664, 'mean_word_length': 5.128342245989304, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '68191', 'license': 'MIT', 'n_tokens_mistral': 509, 'n_tokens_neox': 497, 'n_words': 83, 'path': 'examples/ex_kmeans.ipynb', 'repo_name': 'aaron-christopher-jones/scratch', 'size': '1145'}