<jupyter_start><jupyter_text>> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python.  8.8. Detecting hidden structures in a dataset with clustering 1. Let's import the libraries.<jupyter_code>from itertools import permutations
import numpy as np
import sklearn
import sklearn.decomposition as dec
import sklearn.cluster as clu
import sklearn.datasets as ds
import sklearn.grid_search as gs
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>2. Let's generate a random dataset with three clusters.<jupyter_code>X, y = ds.make_blobs(n_samples=200, n_features=2, centers=3)<jupyter_output><empty_output><jupyter_text>3. We will need a couple of functions to relabel and display the results of the clustering algorithms.<jupyter_code>def relabel(cl):
    """Relabel a clustering with three clusters
    to match the original classes."""
    if np.max(cl) != 2:
        return cl
    perms = np.array(list(permutations((0, 1, 2))))
    i = np.argmin([np.sum(np.abs(perm[cl] - y))
                   for perm in perms])
    p = perms[i]
    return p[cl]
def display_clustering(labels, title):
    """Plot the data points with the cluster colors."""
    # We relabel the classes when there are 3 clusters.
    labels = relabel(labels)
    plt.figure(figsize=(8,3));
    # Display the points with the true labels on the left, 
    # and with the clustering labels on the right.
    for i, (c, title) in enumerate(zip(
            [y, labels], ["True labels", title])):
        plt.subplot(121 + i);
        plt.scatter(X[:,0], X[:,1], c=c, s=30, 
                    linewidths=0, cmap=plt.cm.rainbow);
        plt.xticks([]); plt.yticks([]);
        plt.title(title);<jupyter_output><empty_output><jupyter_text>4. Now, we cluster the dataset with the **K-means** algorithm, a classic and simple clustering algorithm.<jupyter_code>km = clu.KMeans()
km.fit(X);
display_clustering(km.labels_, "KMeans")<jupyter_output><empty_output><jupyter_text>5. This algorithm requires the number of clusters at initialization time. In general, however, we do not necessarily now the number of clusters in the dataset. Here, let's try with `n_clusters=3` (that's cheating, because we happen to know that there are 3 clusters!).<jupyter_code>km = clu.KMeans(n_clusters=3)
km.fit(X);
display_clustering(km.labels_, "KMeans(3)")<jupyter_output><empty_output><jupyter_text>6. Let's try a few other clustering algorithms implemented in scikit-learn. The simplicity of the API makes it really easy to try different methods: it is just a matter of changing the name of the class.<jupyter_code>plt.figure(figsize=(8,5));
plt.subplot(231);
plt.scatter(X[:,0], X[:,1], c=y, s=30,
            linewidths=0, cmap=plt.cm.rainbow);
plt.xticks([]); plt.yticks([]);
plt.title("True labels");
for i, est in enumerate([
        clu.SpectralClustering(3),
        clu.AgglomerativeClustering(3),
        clu.MeanShift(),
        clu.AffinityPropagation(),
        clu.DBSCAN(),
    ]):
    est.fit(X);
    c = relabel(est.labels_)
    plt.subplot(232 + i);
    plt.scatter(X[:,0], X[:,1], c=c, s=30,
                linewidths=0, cmap=plt.cm.rainbow);
    plt.xticks([]); plt.yticks([]);
    plt.title(est.__class__.__name__);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12529550827423167, 'fraction_numerical': 0.016252955082742316, 'mean_word_length': 4.044709388971684, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '181022', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 1173, 'n_tokens_neox': 1109, 'n_words': 328, 'path': 'notebooks/chapter08_ml/08_clustering.ipynb', 'repo_name': 'khanparwaz/PythonProjects', 'size': '3384'}