.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/manifold/plot_t_sne_perplexity.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_t_sne_perplexity.py: ============================================================================= t-SNE:不同困惑度值对形状的影响 ============================================================================= 在两个同心圆和S曲线数据集上展示了不同困惑度值的t-SNE效果。 我们观察到,随着困惑度值的增加,形状趋于更加清晰。 簇的大小、距离和形状可能会因初始化和困惑度值的不同而变化,并不总是具有意义。 如下所示,对于较高的困惑度值,t-SNE能够找到两个同心圆的有意义拓扑结构,但圆的大小和距离与原始数据略有不同。与两个圆的数据集相反,即使在较大的困惑度值下,S曲线数据集上的形状在视觉上也会偏离S曲线的拓扑结构。 有关更多详细信息,"如何有效使用t-SNE" https://distill.pub/2016/misread-tsne/ 提供了对各种参数影响的良好讨论,以及用于探索这些影响的交互式图表。 .. GENERATED FROM PYTHON SOURCE LINES 17-138 .. image-sg:: /auto_examples/manifold/images/sphx_glr_plot_t_sne_perplexity_001.png :alt: Perplexity=5, Perplexity=30, Perplexity=50, Perplexity=100, Perplexity=5, Perplexity=30, Perplexity=50, Perplexity=100, Perplexity=5, Perplexity=30, Perplexity=50, Perplexity=100 :srcset: /auto_examples/manifold/images/sphx_glr_plot_t_sne_perplexity_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none circles, perplexity=5 in 0.21 sec circles, perplexity=30 in 0.37 sec circles, perplexity=50 in 0.42 sec circles, perplexity=100 in 0.42 sec S-curve, perplexity=5 in 0.23 sec S-curve, perplexity=30 in 0.34 sec S-curve, perplexity=50 in 0.4 sec S-curve, perplexity=100 in 0.4 sec uniform grid, perplexity=5 in 0.26 sec uniform grid, perplexity=30 in 0.41 sec uniform grid, perplexity=50 in 0.45 sec uniform grid, perplexity=100 in 0.45 sec | .. code-block:: Python # 作者:scikit-learn 开发者 # SPDX 许可证标识符:BSD-3-Clause from time import time import matplotlib.pyplot as plt import numpy as np from matplotlib.ticker import NullFormatter from sklearn import datasets, manifold n_samples = 150 n_components = 2 (fig, subplots) = plt.subplots(3, 5, figsize=(15, 8)) perplexities = [5, 30, 50, 100] X, y = datasets.make_circles( n_samples=n_samples, factor=0.5, noise=0.05, random_state=0 ) red = y == 0 green = y == 1 ax = subplots[0][0] ax.scatter(X[red, 0], X[red, 1], c="r") ax.scatter(X[green, 0], X[green, 1], c="g") ax.xaxis.set_major_formatter(NullFormatter()) ax.yaxis.set_major_formatter(NullFormatter()) plt.axis("tight") for i, perplexity in enumerate(perplexities): ax = subplots[0][i + 1] t0 = time() tsne = manifold.TSNE( n_components=n_components, init="random", random_state=0, perplexity=perplexity, max_iter=300, ) Y = tsne.fit_transform(X) t1 = time() print("circles, perplexity=%d in %.2g sec" % (perplexity, t1 - t0)) ax.set_title("Perplexity=%d" % perplexity) ax.scatter(Y[red, 0], Y[red, 1], c="r") ax.scatter(Y[green, 0], Y[green, 1], c="g") ax.xaxis.set_major_formatter(NullFormatter()) ax.yaxis.set_major_formatter(NullFormatter()) ax.axis("tight") # 另一个使用S曲线的例子 X, color = datasets.make_s_curve(n_samples, random_state=0) ax = subplots[1][0] ax.scatter(X[:, 0], X[:, 2], c=color) ax.xaxis.set_major_formatter(NullFormatter()) ax.yaxis.set_major_formatter(NullFormatter()) for i, perplexity in enumerate(perplexities): ax = subplots[1][i + 1] t0 = time() tsne = manifold.TSNE( n_components=n_components, init="random", random_state=0, perplexity=perplexity, learning_rate="auto", max_iter=300, ) Y = tsne.fit_transform(X) t1 = time() print("S-curve, perplexity=%d in %.2g sec" % (perplexity, t1 - t0)) ax.set_title("Perplexity=%d" % perplexity) ax.scatter(Y[:, 0], Y[:, 1], c=color) ax.xaxis.set_major_formatter(NullFormatter()) ax.yaxis.set_major_formatter(NullFormatter()) ax.axis("tight") # 另一个使用二维均匀网格的例子 x = np.linspace(0, 1, int(np.sqrt(n_samples))) xx, yy = np.meshgrid(x, x) X = np.hstack( [ xx.ravel().reshape(-1, 1), yy.ravel().reshape(-1, 1), ] ) color = xx.ravel() ax = subplots[2][0] ax.scatter(X[:, 0], X[:, 1], c=color) ax.xaxis.set_major_formatter(NullFormatter()) ax.yaxis.set_major_formatter(NullFormatter()) for i, perplexity in enumerate(perplexities): ax = subplots[2][i + 1] t0 = time() tsne = manifold.TSNE( n_components=n_components, init="random", random_state=0, perplexity=perplexity, max_iter=400, ) Y = tsne.fit_transform(X) t1 = time() print("uniform grid, perplexity=%d in %.2g sec" % (perplexity, t1 - t0)) ax.set_title("Perplexity=%d" % perplexity) ax.scatter(Y[:, 0], Y[:, 1], c=color) ax.xaxis.set_major_formatter(NullFormatter()) ax.yaxis.set_major_formatter(NullFormatter()) ax.axis("tight") plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.647 seconds) .. _sphx_glr_download_auto_examples_manifold_plot_t_sne_perplexity.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_t_sne_perplexity.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_t_sne_perplexity.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_t_sne_perplexity.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_t_sne_perplexity.zip ` .. include:: plot_t_sne_perplexity.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_