.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/_general/plot_kernel_rim.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_rim.py: =================================== Clustering circles with kernel RIM =================================== We show here a simple dataset consisting in two centred circle that can be challenging for some clustering algorithms. We solve this case using the kernel RIM algorithm. This algorithm fits a logistic regression on the kernel matrix derived from the dataset. .. GENERATED FROM PYTHON SOURCE LINES 10-16 .. code-block:: Python from matplotlib import pyplot as plt from sklearn import datasets from sklearn import metrics from gemclus.linear import KernelRIM .. GENERATED FROM PYTHON SOURCE LINES 17-19 Draw samples for the circle dataset ------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-29 .. code-block:: Python # We start by generating samples distributed on two circles noise = 0.05 factor = 0.1 X, y = datasets.make_circles(n_samples=200, noise=noise, factor=factor, random_state=0) mean, std = X.mean(0), X.std(0) X = (X-mean)/std .. GENERATED FROM PYTHON SOURCE LINES 30-32 Training clustering model --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 34-38 .. code-block:: Python model_kernel = KernelRIM(n_clusters=2, base_kernel="rbf", reg=0, random_state=0) y_pred = model_kernel.fit_predict(X) print(f"ARI = {metrics.adjusted_rand_score(y, y_pred)}") .. 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( ARI = 1.0 .. GENERATED FROM PYTHON SOURCE LINES 39-41 Show predictions on similar samples ------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 41-53 .. code-block:: Python # Create a novel set of samples and cluster them new_X, new_y = datasets.make_circles(n_samples=200, noise=noise, factor=factor, random_state=1) new_X = (new_X-mean)/std new_pred = model_kernel.predict(new_X) print(f"ARI = {metrics.adjusted_rand_score(new_y, new_pred)}") plt.scatter(X[:, 0], X[:, 1], c=y_pred, marker="+", label="Training samples") plt.scatter(new_X[:, 0], new_X[:, 1], c=new_pred, marker="o", label="Testing samples") plt.legend() plt.show() .. image-sg:: /auto_examples/_general/images/sphx_glr_plot_kernel_rim_001.png :alt: plot kernel rim :srcset: /auto_examples/_general/images/sphx_glr_plot_kernel_rim_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ARI = 1.0 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.388 seconds) .. _sphx_glr_download_auto_examples__general_plot_kernel_rim.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_rim.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_kernel_rim.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_kernel_rim.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_