<jupyter_start><jupyter_text>Author: [<NAME>](https://github.com/duducheng)<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans, SpectralClustering
from sklearn.mixture import GaussianMixture
from sklearn import datasets

% matplotlib inline<jupyter_output><empty_output><jupyter_text>Make dataset<jupyter_code>def make_clusters(skew=True, *arg,**kwargs):
    X, y = datasets.make_blobs(*arg,**kwargs)
    if skew:
        nrow = X.shape[1]
        for i in np.unique(y):
            X[y==i] = X[y==i].dot(np.random.random((nrow,nrow))-0.5)
    return X,y
X, y = make_clusters(skew=False, n_samples=1500, centers=3, cluster_std=[1,.6,.8])
print(X.shape)
print(y.shape)
print(np.unique(y))
def scatter(X, color=None, ax=None):
    assert X.shape[1]==2
    if color is not None:
        assert X.shape[0]==y.shape[0]
        assert len(y.shape)==1
    if ax:
        ax.scatter(X[:, 0], X[:, 1], c=color)
    else:
        plt.scatter(X[:, 0], X[:, 1], c=color)

scatter(X, y)
# if it looks strange, rerun it.
X, y = make_clusters(n_samples=1500, centers=3, cluster_std=[1,.6,.8])
scatter(X, y)<jupyter_output><empty_output><jupyter_text>Normal KMeans<jupyter_code>y_pred = KMeans(n_clusters=3).fit_predict(X)
scatter(X, y_pred)
plt.title("Normal KMeans")<jupyter_output><empty_output><jupyter_text>Then we see the KMeans failure, together with spectral clustering. Gaussian Mixture (GMM) wins<jupyter_code># if it looks too simple, rerun it.
n_clusters = 4
X, y = make_clusters(n_samples=1500, centers=4, cluster_std=[1,2,.5,1.5])
scatter(X, y)
# n_clusters = 4

fig, axes = plt.subplots(1,3,figsize=(12,4))
y_kmeans = KMeans(n_clusters=n_clusters).fit_predict(X)
scatter(X, y_kmeans, ax=axes[0])
axes[0].set_title("KMeans")
gmm = GaussianMixture(n_components=n_clusters, covariance_type='full').fit(X)
y_gmm = gmm.predict(X)
scatter(X, y_gmm, ax=axes[1])
axes[1].set_title("Gaussian Mixture")
y_sc = SpectralClustering(n_clusters=n_clusters, eigen_solver='arpack',affinity="nearest_neighbors").fit_predict(X)
scatter(X, y_sc,ax=axes[2])
plt.title("Spectral Clustering")<jupyter_output>C:\Miniconda2\envs\dev3\lib\site-packages\sklearn\manifold\spectral_embedding_.py:229: UserWarning: Graph is not fully connected, spectral embedding may not work as expected.
  warnings.warn("Graph is not fully connected, spectral embedding"<jupyter_text>Then we see the KMeans and GMM fails, only spectral clustering wins.<jupyter_code>X, y = datasets.make_moons(n_samples=1500, noise=.03)
scatter(X, y)
n_clusters = 2
fig, axes = plt.subplots(1,3,figsize=(12,4))
y_kmeans = KMeans(n_clusters=n_clusters).fit_predict(X)
scatter(X, y_kmeans, ax=axes[0])
axes[0].set_title("KMeans")
gmm = GaussianMixture(n_components=n_clusters, covariance_type='full').fit(X)
y_gmm = gmm.predict(X)
scatter(X, y_gmm, ax=axes[1])
axes[1].set_title("Gaussian Mixture")
y_sc = SpectralClustering(n_clusters=n_clusters, eigen_solver='arpack',affinity="nearest_neighbors").fit_predict(X)
scatter(X, y_sc,ax=axes[2])
plt.title("Spectral Clustering")
X, y = datasets.make_circles(n_samples=1500, factor=0.5, noise=0.05)
scatter(X, y)
n_clusters = 2
fig, axes = plt.subplots(1,3,figsize=(12,4))
y_kmeans = KMeans(n_clusters=n_clusters).fit_predict(X)
scatter(X, y_kmeans, ax=axes[0])
axes[0].set_title("KMeans")
gmm = GaussianMixture(n_components=n_clusters, covariance_type='full').fit(X)
y_gmm = gmm.predict(X)
scatter(X, y_gmm, ax=axes[1])
axes[1].set_title("Gaussian Mixture")
y_sc = SpectralClustering(n_clusters=n_clusters, eigen_solver='arpack',affinity="nearest_neighbors").fit_predict(X)
scatter(X, y_sc,ax=axes[2])
plt.title("Spectral Clustering")<jupyter_output>C:\Miniconda2\envs\dev3\lib\site-packages\sklearn\manifold\spectral_embedding_.py:229: UserWarning: Graph is not fully connected, spectral embedding may not work as expected.
  warnings.warn("Graph is not fully connected, spectral embedding"
{'fraction_non_alphanumeric': 0.15119836817950025, 'fraction_numerical': 0.024987251402345742, 'mean_word_length': 6.989816700610998, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '277975', 'license': 'MIT', 'n_tokens_mistral': 1652, 'n_tokens_neox': 1552, 'n_words': 276, 'path': '0_scikit-learn_KMeans_GMM_and_Spectral_Clustering.ipynb', 'repo_name': 'duducheng/clustering_tutorial', 'size': '3930'}