.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/_general/plot_kernel_kmeans.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__general_plot_kernel_kmeans.py: ==================================== Kernel KMeans clustering with GEMINI ==================================== Since the MMD GEMINI objective is equivalent in OvO mode to a kernel KMeans objective, we can use it with the nonparametric model that directly associates a cluster to each sample. The overall model would thus behave as a kernel KMeans algorithm. However, its training is done by gradient descent. .. GENERATED FROM PYTHON SOURCE LINES 10-17 .. code-block:: Python import numpy as np from matplotlib import pyplot as plt from sklearn import metrics, datasets from gemclus.nonparametric import CategoricalMMD .. GENERATED FROM PYTHON SOURCE LINES 18-20 Draw samples from a circular dataset -------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-36 .. code-block:: Python # 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/_general/images/sphx_glr_plot_kernel_kmeans_001.png :alt: plot kernel kmeans :srcset: /auto_examples/_general/images/sphx_glr_plot_kernel_kmeans_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 37-41 Train the model -------------------------------------------------------------- Create the Non parametric GEMINI clustering model and call the .fit method to optimise the cluster assignment of the nodes .. GENERATED FROM PYTHON SOURCE LINES 43-47 .. code-block:: Python model = CategoricalMMD(n_clusters=2, random_state=0, kernel="rbf") y_pred = model.fit_predict(X) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/circleci/.local/lib/python3.10/site-packages/sklearn/base.py:474: FutureWarning: `BaseEstimator._validate_data` is deprecated in 1.6 and will be removed in 1.7. Use `sklearn.utils.validation.validate_data` instead. This function becomes public and is part of the scikit-learn developer API. warnings.warn( .. GENERATED FROM PYTHON SOURCE LINES 48-50 Final Clustering ----------------- .. GENERATED FROM PYTHON SOURCE LINES 52-59 .. code-block:: Python plt.scatter(X[:, 0], X[:, 1], c=y_pred) plt.show() ari_score = metrics.adjusted_rand_score(y, y_pred) gemini_score = model.score(X) print(f"Final ARI score: {ari_score:.3f}") print(f"GEMINI score is {gemini_score:.3f}") .. image-sg:: /auto_examples/_general/images/sphx_glr_plot_kernel_kmeans_002.png :alt: plot kernel kmeans :srcset: /auto_examples/_general/images/sphx_glr_plot_kernel_kmeans_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Final ARI score: 1.000 GEMINI score is 0.330 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 10.002 seconds) .. _sphx_glr_download_auto_examples__general_plot_kernel_kmeans.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_kernel_kmeans.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_kernel_kmeans.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_kernel_kmeans.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_