{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Load the Digits dataset" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.datasets import load_digits\n", "\n", "digits = load_digits()\n", "print('Original data shape: digits.data.shape = ' +\n", " f'{digits.data.shape}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a TSNE Estimator to reduce the data to two dimensions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.manifold import TSNE\n", "\n", "tsne = TSNE(n_components=2, random_state=11)\n", "reduced_digits = tsne.fit_transform(digits.data)\n", "\n", "print(f'Reduced data shape: reduced_digits.shape = {reduced_digits.shape}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Visualize the reduced data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "\n", "dots = plt.scatter(reduced_digits[:, 0], reduced_digits[:, 1], c='black')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Visualize the reduced data with a different color for each digit" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dots = plt.scatter(reduced_digits[:, 0], reduced_digits[:, 1], c=digits.target, \n", " cmap=plt.cm.get_cmap('nipy_spectral_r', 10))\n", "\n", "colorbar = plt.colorbar(dots)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "##########################################################################\n", "# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n", "# Pearson Education, Inc. All Rights Reserved. #\n", "# #\n", "# DISCLAIMER: The authors and publisher of this book have used their #\n", "# best efforts in preparing the book. These efforts include the #\n", "# development, research, and testing of the theories and programs #\n", "# to determine their effectiveness. The authors and publisher make #\n", "# no warranty of any kind, expressed or implied, with regard to these #\n", "# programs or to the documentation contained in these books. The authors #\n", "# and publisher shall not be liable in any event for incidental or #\n", "# consequential damages in connection with, or arising out of, the #\n", "# furnishing, performance, or use of these programs. #\n", "##########################################################################\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.7" } }, "nbformat": 4, "nbformat_minor": 4 }