.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/cluster/plot_digits_agglomeration.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. or to run this example in your browser via JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_cluster_plot_digits_agglomeration.py: ========================================================= Feature agglomeration ========================================================= These images show how similar features are merged together using feature agglomeration. .. GENERATED FROM PYTHON SOURCE LINES 10-59 .. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_digits_agglomeration_001.png :alt: Original data, Agglomerated data, Labels :srcset: /auto_examples/cluster/images/sphx_glr_plot_digits_agglomeration_001.png :class: sphx-glr-single-img .. code-block:: Python # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause import matplotlib.pyplot as plt import numpy as np from sklearn import cluster, datasets from sklearn.feature_extraction.image import grid_to_graph digits = datasets.load_digits() images = digits.images X = np.reshape(images, (len(images), -1)) connectivity = grid_to_graph(*images[0].shape) agglo = cluster.FeatureAgglomeration(connectivity=connectivity, n_clusters=32) agglo.fit(X) X_reduced = agglo.transform(X) X_restored = agglo.inverse_transform(X_reduced) images_restored = np.reshape(X_restored, images.shape) plt.figure(1, figsize=(4, 3.5)) plt.clf() plt.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.91) for i in range(4): plt.subplot(3, 4, i + 1) plt.imshow(images[i], cmap=plt.cm.gray, vmax=16, interpolation="nearest") plt.xticks(()) plt.yticks(()) if i == 1: plt.title("Original data") plt.subplot(3, 4, 4 + i + 1) plt.imshow(images_restored[i], cmap=plt.cm.gray, vmax=16, interpolation="nearest") if i == 1: plt.title("Agglomerated data") plt.xticks(()) plt.yticks(()) plt.subplot(3, 4, 10) plt.imshow( np.reshape(agglo.labels_, images[0].shape), interpolation="nearest", cmap=plt.cm.nipy_spectral, ) plt.xticks(()) plt.yticks(()) plt.title("Labels") plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.173 seconds) .. _sphx_glr_download_auto_examples_cluster_plot_digits_agglomeration.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/main?urlpath=lab/tree/notebooks/auto_examples/cluster/plot_digits_agglomeration.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/cluster/plot_digits_agglomeration.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_digits_agglomeration.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_digits_agglomeration.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_digits_agglomeration.zip ` .. include:: plot_digits_agglomeration.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_