.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/manifold/plot_swissroll.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 Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_manifold_plot_swissroll.py: =================================== 瑞士卷和瑞士洞降维 =================================== 本笔记本旨在比较两种流行的非线性降维技术,T-分布随机邻嵌入(t-SNE)和局部线性嵌入(LLE),在经典的瑞士卷数据集上的表现。然后,我们将探讨它们如何处理数据中添加一个洞的情况。 .. GENERATED FROM PYTHON SOURCE LINES 9-13 Swiss Roll --------------------------------------------------- 我们首先生成瑞士卷数据集。 .. GENERATED FROM PYTHON SOURCE LINES 13-20 .. code-block:: Python import matplotlib.pyplot as plt from sklearn import datasets, manifold sr_points, sr_color = datasets.make_swiss_roll(n_samples=1500, random_state=0) .. GENERATED FROM PYTHON SOURCE LINES 21-22 现在,让我们来看一下我们的数据: .. GENERATED FROM PYTHON SOURCE LINES 22-34 .. code-block:: Python fig = plt.figure(figsize=(8, 6)) ax = fig.add_subplot(111, projection="3d") fig.add_axes(ax) ax.scatter( sr_points[:, 0], sr_points[:, 1], sr_points[:, 2], c=sr_color, s=50, alpha=0.8 ) ax.set_title("Swiss Roll in Ambient Space") ax.view_init(azim=-66, elev=12) _ = ax.text2D(0.8, 0.05, s="n_samples=1500", transform=ax.transAxes) .. image-sg:: /auto_examples/manifold/images/sphx_glr_plot_swissroll_001.png :alt: Swiss Roll in Ambient Space :srcset: /auto_examples/manifold/images/sphx_glr_plot_swissroll_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 35-36 计算LLE和t-SNE嵌入,我们发现LLE似乎能够非常有效地展开瑞士卷。另一方面,t-SNE能够保留数据的一般结构,但不能很好地表示我们原始数据的连续性。相反,它似乎不必要地将一些点聚集在一起。 .. GENERATED FROM PYTHON SOURCE LINES 36-52 .. code-block:: Python sr_lle, sr_err = manifold.locally_linear_embedding( sr_points, n_neighbors=12, n_components=2 ) sr_tsne = manifold.TSNE(n_components=2, perplexity=40, random_state=0).fit_transform( sr_points ) fig, axs = plt.subplots(figsize=(8, 8), nrows=2) axs[0].scatter(sr_lle[:, 0], sr_lle[:, 1], c=sr_color) axs[0].set_title("LLE Embedding of Swiss Roll") axs[1].scatter(sr_tsne[:, 0], sr_tsne[:, 1], c=sr_color) _ = axs[1].set_title("t-SNE Embedding of Swiss Roll") .. image-sg:: /auto_examples/manifold/images/sphx_glr_plot_swissroll_002.png :alt: LLE Embedding of Swiss Roll, t-SNE Embedding of Swiss Roll :srcset: /auto_examples/manifold/images/sphx_glr_plot_swissroll_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 53-56 .. note:: LLE 似乎正在拉伸瑞士卷中心(紫色)的点。然而,我们观察到这只是数据生成方式的副产品。瑞士卷中心附近的点密度较高,这最终影响了 LLE 在低维度上重建数据的方式。 .. GENERATED FROM PYTHON SOURCE LINES 58-62 Swiss-Hole --------------------------------------------------- 现在让我们看看这两种算法如何处理我们在数据中添加一个空洞。首先,我们生成瑞士卷空洞数据集并绘制它: .. GENERATED FROM PYTHON SOURCE LINES 62-77 .. code-block:: Python sh_points, sh_color = datasets.make_swiss_roll( n_samples=1500, hole=True, random_state=0 ) fig = plt.figure(figsize=(8, 6)) ax = fig.add_subplot(111, projection="3d") fig.add_axes(ax) ax.scatter( sh_points[:, 0], sh_points[:, 1], sh_points[:, 2], c=sh_color, s=50, alpha=0.8 ) ax.set_title("Swiss-Hole in Ambient Space") ax.view_init(azim=-66, elev=12) _ = ax.text2D(0.8, 0.05, s="n_samples=1500", transform=ax.transAxes) .. image-sg:: /auto_examples/manifold/images/sphx_glr_plot_swissroll_003.png :alt: Swiss-Hole in Ambient Space :srcset: /auto_examples/manifold/images/sphx_glr_plot_swissroll_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 78-79 计算LLE和t-SNE嵌入,我们得到与瑞士卷相似的结果。LLE非常有效地展开了数据,甚至保留了孔洞。t-SNE再次将部分点聚集在一起,但我们注意到它保留了原始数据的一般拓扑结构。 .. GENERATED FROM PYTHON SOURCE LINES 79-96 .. code-block:: Python sh_lle, sh_err = manifold.locally_linear_embedding( sh_points, n_neighbors=12, n_components=2 ) sh_tsne = manifold.TSNE( n_components=2, perplexity=40, init="random", random_state=0 ).fit_transform(sh_points) fig, axs = plt.subplots(figsize=(8, 8), nrows=2) axs[0].scatter(sh_lle[:, 0], sh_lle[:, 1], c=sh_color) axs[0].set_title("LLE Embedding of Swiss-Hole") axs[1].scatter(sh_tsne[:, 0], sh_tsne[:, 1], c=sh_color) _ = axs[1].set_title("t-SNE Embedding of Swiss-Hole") .. image-sg:: /auto_examples/manifold/images/sphx_glr_plot_swissroll_004.png :alt: LLE Embedding of Swiss-Hole, t-SNE Embedding of Swiss-Hole :srcset: /auto_examples/manifold/images/sphx_glr_plot_swissroll_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 97-103 结论 ------------------ 我们注意到,t-SNE 通过测试更多的参数组合可以受益。通过更好地调整这些参数,可能会获得更好的结果。 我们观察到,如“手写数字上的流形学习”示例所示,t-SNE 通常在真实世界数据上比 LLE 表现更好。 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 28.131 seconds) .. _sphx_glr_download_auto_examples_manifold_plot_swissroll.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/manifold/plot_swissroll.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_swissroll.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_swissroll.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_swissroll.zip ` .. include:: plot_swissroll.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_