<jupyter_start><jupyter_text>K-Means++- K-Means is quite sensitive to initalization, if the init is not good, our algorithm is not able to make desired numberof clusters.- To overcome this problem, we use technique called K-Means++ (described in paper [Robust Seed Selection for K-Means type of Algorithms](https://arxiv.org/abs/1202.1585))    which chooses initial centers so that they are statiscallly close to final ones.- The method is default choice for sklearn, so we will use it directly without going into much maths! Two different types of clustering oberserved for two different types of initialisations as discussed in the last example.**Clustering - 1** **Clustering - 2**  Using Sci-kit Learn<jupyter_code>import numpy as np
from matplotlib import pyplot as plt
from sklearn.datasets import make_blobs
X,y  = make_blobs(n_samples=500,n_features=2,centers=5,random_state=3)
from sklearn.cluster import KMeanss

kmeans  = KMeans(n_clusters=5)



kmeans.fit(X,y)
centers = kmeans.cluster_centers_
plt.scatter(X[:,0],X[:,1])
plt.scatter(centers[:,0],centers[:,1],marker='*',color='orange')
plt.show()
pred = kmeans.labels_
print(pred)
plt.scatter(X[:,0],X[:,1],c=pred)
plt.scatter(centers[:,0],centers[:,1],marker='*',color='black')
plt.title("Prediction")
plt.show()
plt.scatter(X[:,0],X[:,1],c=y)
plt.title("Actual")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13230994152046785, 'fraction_numerical': 0.019736842105263157, 'mean_word_length': 6.4, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, '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': '529547', 'license': 'MIT', 'n_tokens_mistral': 487, 'n_tokens_neox': 462, 'n_words': 135, 'path': 'ml_repo/10. K-Means and Color Extraction/K-Means++.ipynb', 'repo_name': 'sachinpr0001/data_science', 'size': '1368'}