.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/cluster/plot_digits_linkage.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end <sphx_glr_download_auto_examples_cluster_plot_digits_linkage.py>` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_cluster_plot_digits_linkage.py: ============================================================================= 二维嵌入数å—çš„å„ç§å‡èšèšç±» ============================================================================= åœ¨æ•°å—æ•°æ®é›†çš„二维嵌入上展示å„ç§å‡èšèšç±»çš„链接选项。 æ¤ç¤ºä¾‹çš„目的是直观地展示度é‡çš„è¡Œä¸ºï¼Œè€Œä¸æ˜¯ä¸ºæ•°å—æ•°æ®é›†æ‰¾åˆ°å¥½çš„èšç±»ã€‚这就是为什么示例在二维嵌入上进行。 è¿™ä¸ªç¤ºä¾‹å‘æˆ‘们展示了å‡èšèšç±»çš„“富者愈富â€è¡Œä¸ºï¼Œå€¾å‘于创建ä¸å‡åŒ€çš„èšç±»å¤§å°ã€‚ è¿™ç§è¡Œä¸ºåœ¨å¹³å‡é“¾æŽ¥ç–ç•¥ä¸å°¤ä¸ºæ˜Žæ˜¾ï¼Œæœ€ç»ˆä¼šå½¢æˆå‡ 个数æ®ç‚¹è¾ƒå°‘çš„èšç±»ã€‚ å•链接的情况甚至更为病æ€ï¼Œå½¢æˆä¸€ä¸ªè¦†ç›–大多数数å—çš„éžå¸¸å¤§çš„èšç±»ï¼Œä¸€ä¸ªä¸ç‰å¤§å°ï¼ˆå¹²å‡€ï¼‰çš„èšç±»åŒ…å«å¤§å¤šæ•°é›¶æ•°å—,其他所有èšç±»åˆ™ç”±è¾¹ç¼˜å™ªå£°ç‚¹ç»„æˆã€‚ 其他链接ç–略导致更å‡åŒ€åˆ†å¸ƒçš„èšç±»ï¼Œå› æ¤æ›´ä¸å®¹æ˜“å—到数æ®é›†éšæœºé‡é‡‡æ ·çš„å½±å“。 .. GENERATED FROM PYTHON SOURCE LINES 19-79 .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_digits_linkage_001.png :alt: ward linkage :srcset: /auto_examples/cluster/images/sphx_glr_plot_digits_linkage_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_digits_linkage_002.png :alt: average linkage :srcset: /auto_examples/cluster/images/sphx_glr_plot_digits_linkage_002.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_digits_linkage_003.png :alt: complete linkage :srcset: /auto_examples/cluster/images/sphx_glr_plot_digits_linkage_003.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/cluster/images/sphx_glr_plot_digits_linkage_004.png :alt: single linkage :srcset: /auto_examples/cluster/images/sphx_glr_plot_digits_linkage_004.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Computing embedding Done. ward : 0.04s average : 0.02s complete : 0.02s single : 0.04s | .. code-block:: Python # 作者:scikit-learn å¼€å‘者 # SPDX-License-Identifier: BSD-3-Clause from time import time import numpy as np from matplotlib import pyplot as plt from sklearn import datasets, manifold digits = datasets.load_digits() X, y = digits.data, digits.target n_samples, n_features = X.shape np.random.seed(0) # ---------------------------------------------------------------------- # å¯è§†åŒ–èšç±» def plot_clustering(X_red, labels, title=None): x_min, x_max = np.min(X_red, axis=0), np.max(X_red, axis=0) X_red = (X_red - x_min) / (x_max - x_min) plt.figure(figsize=(6, 4)) for digit in digits.target_names: plt.scatter( *X_red[y == digit].T, marker=f"${digit}$", s=50, c=plt.cm.nipy_spectral(labels[y == digit] / 10), alpha=0.5, ) plt.xticks([]) plt.yticks([]) if title is not None: plt.title(title, size=17) plt.axis("off") plt.tight_layout(rect=[0, 0.03, 1, 0.95]) # ---------------------------------------------------------------------- # æ•°å—æ•°æ®é›†çš„二维嵌入 print("Computing embedding") X_red = manifold.SpectralEmbedding(n_components=2).fit_transform(X) print("Done.") from sklearn.cluster import AgglomerativeClustering for linkage in ("ward", "average", "complete", "single"): clustering = AgglomerativeClustering(linkage=linkage, n_clusters=10) t0 = time() clustering.fit(X_red) print("%s :\t%.2fs" % (linkage, time() - t0)) plot_clustering(X_red, clustering.labels_, "%s linkage" % linkage) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.114 seconds) .. _sphx_glr_download_auto_examples_cluster_plot_digits_linkage.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_linkage.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_digits_linkage.ipynb <plot_digits_linkage.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_digits_linkage.py <plot_digits_linkage.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_digits_linkage.zip <plot_digits_linkage.zip>` .. include:: plot_digits_linkage.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_