.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_packages_scikit-learn_auto_examples_plot_tsne.py: ========================== tSNE to visualize digits ========================== Here we use :class:`sklearn.manifold.TSNE` to visualize the digits datasets. Indeed, the digits are vectors in a 8*8 = 64 dimensional space. We want to project them in 2D for visualization. tSNE is often a good solution, as it groups and separates data points based on their local relationship. Load the iris data .. code-block:: python from sklearn import datasets digits = datasets.load_digits() # Take the first 500 data points: it's hard to see 1500 points X = digits.data[:500] y = digits.target[:500] Fit and transform with a TSNE .. code-block:: python from sklearn.manifold import TSNE tsne = TSNE(n_components=2, random_state=0) Project the data in 2D .. code-block:: python X_2d = tsne.fit_transform(X) Visualize the data .. code-block:: python target_ids = range(len(digits.target_names)) from matplotlib import pyplot as plt plt.figure(figsize=(6, 5)) colors = 'r', 'g', 'b', 'c', 'm', 'y', 'k', 'w', 'orange', 'purple' for i, c, label in zip(target_ids, colors, digits.target_names): plt.scatter(X_2d[y == i, 0], X_2d[y == i, 1], c=c, label=label) plt.legend() plt.show() .. image:: /packages/scikit-learn/auto_examples/images/sphx_glr_plot_tsne_001.png :class: sphx-glr-single-img **Total running time of the script:** ( 0 minutes 1.335 seconds) .. _sphx_glr_download_packages_scikit-learn_auto_examples_plot_tsne.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: plot_tsne.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: plot_tsne.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_