<jupyter_start><jupyter_text>Clustering=============<jupyter_code>from sklearn.datasets import make_blobs
X, y = make_blobs(random_state=42)
X.shape
plt.scatter(X[:, 0], X[:, 1])
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=3)
kmeans.fit(X)
cluster_labels = kmeans.predict(X)
cluster_labels
plt.scatter(X[:, 0], X[:, 1], c=cluster_labels)
y
from sklearn.metrics import accuracy_score, confusion_matrix
accuracy_score(y, cluster_labels)
confusion_matrix(y, cluster_labels)
from sklearn.metrics import adjusted_rand_score
adjusted_rand_score(y, cluster_labels)<jupyter_output><empty_output><jupyter_text>KMeans can generalize, SpectralClustering can not-----------------------------------------------------------KMeans has a ``predict`` function.<jupyter_code>X_more = np.random.uniform(X.min(), X.max(), size=(10, 2))
X_more
more_cluster_labels = kmeans.predict(X_more)
plt.scatter(X[:, 0], X[:, 1], c=cluster_labels)
plt.scatter(X_more[:, 0], X_more[:, 1], marker="x", c=more_cluster_labels)
from sklearn.cluster import SpectralClustering
spectral_clustering = SpectralClustering(n_clusters=3, gamma=.1)
spectral_clustering.fit(X)

cluster_labels = spectral_clustering.predict(X)
spectral_clustering.labels_
cluster_labels = spectral_clustering.fit_predict(X)
plt.scatter(X[:, 0], X[:, 1], c=cluster_labels)<jupyter_output><empty_output><jupyter_text>There is no way to apply ``spectral_clustering`` not to ``X_more``. An overview of clustering algorithms---------------------------------------  More on the website: http://scikit-learn.org/stable/modules/clustering.html A less trivial example-------------------------<jupyter_code>from sklearn.datasets import load_digits
digits = load_digits()

X, y = digits.data, digits.target
kmeans = KMeans(n_clusters=10)
kmeans.fit(X)
adjusted_rand_score(y, kmeans.predict(X))
_, axes = plt.subplots(2, 5)
for ax, center in zip(axes.ravel(), kmeans.cluster_centers_):
    ax.matshow(center.reshape(8, 8), cmap=plt.cm.gray)
    ax.set_xticks(())
    ax.set_yticks(())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18929440389294405, 'fraction_numerical': 0.01167883211678832, 'mean_word_length': 5.100890207715134, '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': '530910', 'license': 'Apache-2.0', 'n_tokens_mistral': 755, 'n_tokens_neox': 708, 'n_words': 139, 'path': 'scikit/Chapter 1/Clustering.ipynb', 'repo_name': 'KarthikKothareddy/Data-Science-Practice', 'size': '2055'}