<jupyter_start><jupyter_text>Now, we can test the class on generated isotropic Gaussian blobs for clustering with `make_blobs`<jupyter_code>from sklearn.datasets import make_blobs

X_blob, y_blob = make_blobs(n_samples=100, centers=2, random_state=3)
sns.scatterplot(x=X_blob[:,0], y=X_blob[:,1], hue=y_blob, size=5, legend=False, palette='tab20')
plt.show()<jupyter_output><empty_output><jupyter_text>Fit the class on the dataset<jupyter_code>kmeans_clf = KMeans(n_clusters=2)
kmeans_clf.fit(X_blob)
y_pred = kmeans_clf.predict([-4, 0])
print(f'Data point belongs to cluster {y_pred}')
kmeans_clf.plot_graph()<jupyter_output><empty_output><jupyter_text>Check the optimal number of cluster for the data set by plotting the 'Elbow' graph<jupyter_code>inertias = []
for k in range(1, 6):
    model = KMeans(n_clusters=k)
    model.fit(X_blob)
    model_inertia = model.inertia()
    inertias.append(model_inertia)
plt.plot(range(1, 6), inertias, "bo-")
plt.xlabel("k", fontsize=14)
plt.ylabel("Inertia", fontsize=14)
plt.annotate('Elbow',
             xy=(2, inertias[1]),
             xytext=(0.4, 0.5),
             textcoords='figure fraction',
             fontsize=16,
             arrowprops=dict(facecolor='black', shrink=0.1)
            )
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1368584758942457, 'fraction_numerical': 0.02410575427682737, 'mean_word_length': 5.070754716981132, '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': '231295', 'license': 'MIT', 'n_tokens_mistral': 523, 'n_tokens_neox': 491, 'n_words': 91, 'path': 'KMeans from scratch.ipynb', 'repo_name': 'k13var/from-scratch-algorithms', 'size': '1286'}