.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_circles.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_circles.py: ============================================================== Comparative clustering of circles dataset with kernel change ============================================================== We show here a simple dataset consisting in two centred circle that can be challenging for some clustering algorithms. This dataset can be challenging for GEMINI as well, unless we change the kernel adequately. .. GENERATED FROM PYTHON SOURCE LINES 9-17 .. code-block:: default import numpy as np from matplotlib import pyplot as plt from sklearn import datasets, metrics from sklearn.cluster import SpectralClustering from sklearn.mixture import GaussianMixture from gemclus import mlp .. GENERATED FROM PYTHON SOURCE LINES 18-20 Draw samples for the circle dataset ------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-36 .. code-block:: default # We start by generating samples distributed on two circles X, y = datasets.make_circles(n_samples=200, noise=0.05, factor=0.05, random_state=0) # then normalise the data X = (X - np.mean(X, 0)) / np.std(X, ddof=0) # Have a look at it plt.scatter(X[:, 0], X[:, 1], c=y) plt.axis("off") plt.ylim((-3, 3)) plt.ylim((-3, 3)) plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_circles_001.png :alt: plot circles :srcset: /auto_examples/images/sphx_glr_plot_circles_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 37-39 Training clustering models --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 41-59 .. code-block:: default # Gaussian mixture model # We set the covariance type to *spherical* to lighten the number of parameters in correspondence # to the symmetry of the data # We ease the job by proposing a initialisation of the means close to the actual means gm = GaussianMixture(n_components=2, covariance_type='spherical', means_init=np.zeros((2, 2)), max_iter=1000, random_state=0).fit(X) # Spectral clustering sc = SpectralClustering(n_clusters=2, random_state=0).fit(X) # MMD MLP with linear kernel # We use multi layered perceptrons because a linear model is incapable of drawing the optimal decision boundary euclidean_gemini = mlp.MLPMMD(n_clusters=2, random_state=0).fit(X) # Then we take a similar model but use the RBF kernel for the computation of MMD rbf_gemini = mlp.MLPMMD(n_clusters=2, kernel="rbf", random_state=0).fit(X) .. GENERATED FROM PYTHON SOURCE LINES 60-62 Display predictions and decision boundaries --------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 64-104 .. code-block:: default # We generate a grid of values for showing the decision boundary x_vals = np.linspace(X[:, 0].min() - 0.5, X[:, 0].max() + 0.5, num=50) y_vals = np.linspace(X[:, 1].min() - 0.5, X[:, 1].max() + 0.5, num=50) xx, yy = np.meshgrid(x_vals, y_vals) grid_inputs = np.c_[xx.ravel(), yy.ravel()] # Plot for the Gaussian mixture plt.subplot(2, 2, 1) plt.contourf(xx, yy, gm.predict(grid_inputs).reshape((50, 50)), alpha=0.3, cmap=plt.cm.Spectral) plt.scatter(X[:, 0], X[:, 1], c=gm.predict(X)) plt.axis("off") plt.title("Gaussian mixture") # Plot for the spectral clustering (cannot draw decision boundary) plt.subplot(2, 2, 2) plt.scatter(X[:, 0], X[:, 1], c=sc.labels_) plt.axis("off") plt.title("Spectral clustering") # Plot for the MMD MLP GEMINI with linear kernel plt.subplot(2, 2, 3) plt.contourf(xx, yy, euclidean_gemini.predict(grid_inputs).reshape((50, 50)), alpha=0.3, cmap=plt.cm.Spectral) plt.scatter(X[:, 0], X[:, 1], c=euclidean_gemini.predict(X)) plt.axis("off") plt.title("MMD GEMINI (linear kernel)") # Plot for the MMD MLP GEMINI with RBF kernel plt.subplot(2, 2, 4) plt.contourf(xx, yy, rbf_gemini.predict(grid_inputs).reshape((50, 50)), alpha=0.3, cmap=plt.cm.Spectral) plt.scatter(X[:, 0], X[:, 1], c=rbf_gemini.predict(X)) plt.axis("off") plt.title("MMD GEMINI (rbf kernel)") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_circles_002.png :alt: Gaussian mixture, Spectral clustering, MMD GEMINI (linear kernel), MMD GEMINI (rbf kernel) :srcset: /auto_examples/images/sphx_glr_plot_circles_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (6 minutes 49.919 seconds) .. _sphx_glr_download_auto_examples_plot_circles.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_circles.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_circles.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_