{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "3644475e", "metadata": {}, "outputs": [], "source": [ "# Display hand writing dataset" ] }, { "cell_type": "code", "execution_count": null, "id": "8125479b", "metadata": {}, "outputs": [], "source": [ "import tensorflow as tf\n", "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": null, "id": "d45b964f", "metadata": {}, "outputs": [], "source": [ "# Load training dataset of 60000 images with greyscale values in 28 x 28\n", "# and labels \n", "(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()" ] }, { "cell_type": "code", "execution_count": null, "id": "fa8ae2a6", "metadata": {}, "outputs": [], "source": [ "# print the shape of the numpy arrays\n", "print ('Print shape of pixel data')\n", "print(x_train.shape)\n", "print ('Print shape of labels')\n", "print(y_train.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "be70973e", "metadata": {}, "outputs": [], "source": [ "# normalize pixel to 0-1\n", "x_train = x_train / 255\n", "x_test = x_test / 255" ] }, { "cell_type": "code", "execution_count": null, "id": "55f457d5", "metadata": {}, "outputs": [], "source": [ "# choose an image num to display and print\n", "num = 20\n", "\n", "image = x_train[num]\n", "label = y_train[num]" ] }, { "cell_type": "code", "execution_count": null, "id": "149788b7", "metadata": {}, "outputs": [], "source": [ "# plot the image using imshow\n", "plt.imshow(image, cmap='gray')\n", "# set the title\n", "plt.title(\"Label: %d\" % label )\n", "# remove the axis labels and ticks\n", "plt.axis('off')\n", "# show the plot\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "232ef6ca", "metadata": {}, "outputs": [], "source": [ "# Plot 16 examples from the numpy array which was read in above\n", "# and display it\n", "fig, axes = plt.subplots(4, 4, figsize=(10, 10))\n", "for i , ax in enumerate(axes.ravel()):\n", " ax.imshow(x_train[num+i], cmap='gray')\n", " ax.set_title(\"Label: %d\" % y_train[num+i])\n", " ax.axis('off')\n", "plt.suptitle(\"Examples of training set images\")\n", "plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.16" } }, "nbformat": 4, "nbformat_minor": 5 }