<jupyter_start><jupyter_text>Prepare dataset<jupyter_code># Generate synthetic dataset
dataset = blobs()

X, y = dataset.generate(
    n_samples=100, n_centers=3, random_state=1234, cluster_std=[2.2, 1.4, 1.0]
)


# Plot dataset
plt.scatter(X[:, 0], X[:, 1])
plt.show()<jupyter_output><empty_output><jupyter_text>Init kmeans model<jupyter_code>kmeans = Kmeans()
kmeans.fit(X, n_centers=3, n_iterations=10)
kmeans.centroids
plt.scatter(X[:, 0], X[:, 1])
plt.scatter(kmeans.centroids[0, 0], kmeans.centroids[0, 1], marker="x", s=150)
plt.scatter(kmeans.centroids[1, 0], kmeans.centroids[1, 1], marker="x", s=150)
plt.scatter(kmeans.centroids[2, 0], kmeans.centroids[2, 1], marker="x", s=150)
plt.show()<jupyter_output><empty_output><jupyter_text>Implement Inertia as error meassurement$$\text{inertia} = \sum_{\forall center \in centers} \sum_{\forall point \in center} (point - center)^2 $$Based on the before error formula (inertia) code a method to select the best number of centroids<jupyter_code>#-- to do --

print(kmeans.inertia)<jupyter_output>inf
{'fraction_non_alphanumeric': 0.16619183285849953, 'fraction_numerical': 0.04083570750237417, 'mean_word_length': 6.807407407407408, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '257809', 'license': 'MIT', 'n_tokens_mistral': 465, 'n_tokens_neox': 424, 'n_words': 87, 'path': 'notebooks/clustering.ipynb', 'repo_name': 'rmaestre/ml-code-lectures', 'size': '1053'}