Machine Learning Kurs im Rahmen der Studierendentage im SS 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
2.9 KiB

2 years ago
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": null,
  6. "id": "3644475e",
  7. "metadata": {},
  8. "outputs": [],
  9. "source": [
  10. "# Display hand writing dataset"
  11. ]
  12. },
  13. {
  14. "cell_type": "code",
  15. "execution_count": null,
  16. "id": "8125479b",
  17. "metadata": {},
  18. "outputs": [],
  19. "source": [
  20. "import tensorflow as tf\n",
  21. "import numpy as np\n",
  22. "import matplotlib.pyplot as plt"
  23. ]
  24. },
  25. {
  26. "cell_type": "code",
  27. "execution_count": null,
  28. "id": "d45b964f",
  29. "metadata": {},
  30. "outputs": [],
  31. "source": [
  32. "# Load training dataset of 60000 images with greyscale values in 28 x 28\n",
  33. "# and labels \n",
  34. "(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()"
  35. ]
  36. },
  37. {
  38. "cell_type": "code",
  39. "execution_count": null,
  40. "id": "fa8ae2a6",
  41. "metadata": {},
  42. "outputs": [],
  43. "source": [
  44. "# print the shape of the numpy arrays\n",
  45. "print ('Print shape of pixel data')\n",
  46. "print(x_train.shape)\n",
  47. "print ('Print shape of labels')\n",
  48. "print(y_train.shape)"
  49. ]
  50. },
  51. {
  52. "cell_type": "code",
  53. "execution_count": null,
  54. "id": "be70973e",
  55. "metadata": {},
  56. "outputs": [],
  57. "source": [
  58. "# normalize pixel to 0-1\n",
  59. "x_train = x_train / 255\n",
  60. "x_test = x_test / 255"
  61. ]
  62. },
  63. {
  64. "cell_type": "code",
  65. "execution_count": null,
  66. "id": "55f457d5",
  67. "metadata": {},
  68. "outputs": [],
  69. "source": [
  70. "# choose an image num to display and print\n",
  71. "num = 20\n",
  72. "\n",
  73. "image = x_train[num]\n",
  74. "label = y_train[num]"
  75. ]
  76. },
  77. {
  78. "cell_type": "code",
  79. "execution_count": null,
  80. "id": "149788b7",
  81. "metadata": {},
  82. "outputs": [],
  83. "source": [
  84. "# plot the image using imshow\n",
  85. "plt.imshow(image, cmap='gray')\n",
  86. "# set the title\n",
  87. "plt.title(\"Label: %d\" % label )\n",
  88. "# remove the axis labels and ticks\n",
  89. "plt.axis('off')\n",
  90. "# show the plot\n",
  91. "plt.show()"
  92. ]
  93. },
  94. {
  95. "cell_type": "code",
  96. "execution_count": null,
  97. "id": "232ef6ca",
  98. "metadata": {},
  99. "outputs": [],
  100. "source": [
  101. "# Plot 16 examples from the numpy array which was read in above\n",
  102. "# and display it\n",
  103. "fig, axes = plt.subplots(4, 4, figsize=(10, 10))\n",
  104. "for i , ax in enumerate(axes.ravel()):\n",
  105. " ax.imshow(x_train[num+i], cmap='gray')\n",
  106. " ax.set_title(\"Label: %d\" % y_train[num+i])\n",
  107. " ax.axis('off')\n",
  108. "plt.suptitle(\"Examples of training set images\")\n",
  109. "plt.show()"
  110. ]
  111. }
  112. ],
  113. "metadata": {
  114. "kernelspec": {
  115. "display_name": "Python 3 (ipykernel)",
  116. "language": "python",
  117. "name": "python3"
  118. },
  119. "language_info": {
  120. "codemirror_mode": {
  121. "name": "ipython",
  122. "version": 3
  123. },
  124. "file_extension": ".py",
  125. "mimetype": "text/x-python",
  126. "name": "python",
  127. "nbconvert_exporter": "python",
  128. "pygments_lexer": "ipython3",
  129. "version": "3.8.16"
  130. }
  131. },
  132. "nbformat": 4,
  133. "nbformat_minor": 5
  134. }