├── .gitignore ├── 00-Python-Basics ├── 01-Built-in-Scalar-Types.ipynb ├── 02-Built-in-Data-Structures.ipynb ├── 03-Control-Flow-Statements.ipynb ├── 04-Functions.ipynb ├── 04-IPython-and-Shell-Commands.ipynb ├── 05-Errors-and-Debugging.ipynb ├── 06-Iterators.ipynb ├── 07-Generators.ipynb ├── 08-Modules-and-Packages.ipynb ├── 09-Strings-and-Regular-Expressions.ipynb ├── Exercise-01-Built-in-Scalar-Types.ipynb ├── Exercise-02-Built-in-Data-Structures.ipynb ├── Exercise-03-Control-Flow-Statements.ipynb ├── Exercise-04-Functions.ipynb ├── Exercise-05-Errors-and-Debugging.ipynb ├── Exercise-06-Iterators.ipynb ├── Exercise-07-Generators.ipynb ├── Exercise-08-Modules-and-Packages.ipynb └── Exercise-09-Strings-and-Regular-Expressions.ipynb ├── 01-Introduction.ipynb ├── 02-Python-Syntax-and-Semantics.ipynb ├── 03-Magic-Commands.ipynb ├── 04-A-Preview-of-Data-Science-Tools.ipynb ├── 05-File-Import-Export-in-Colab.ipynb ├── 06-Reading-Tabular-Data-into-DataFrames.ipynb ├── 07-Missing-Values.ipynb ├── 08-Aggregation-and-Grouping.ipynb ├── 09-Matplotlib.ipynb ├── 10-Seaborn.ipynb ├── 11-Merge-and-Join.ipynb ├── 12-Exploratory-Data-Analysis.ipynb ├── 13-Working-with-APIs.ipynb ├── 14-Working-with-Time-Series.ipynb ├── 15-Machine-Learning-Overview.ipynb ├── 16-Introducing-Scikit-Learn.ipynb ├── 17-Classification.ipynb ├── 18-Regression.ipynb ├── 19-Exploring-Hand-written-Digits.ipynb ├── Archive └── DL-in-Py-1st-Ed │ ├── 20-A-First-Look-at-a-Neural-Network-in-Keras.ipynb │ ├── 21-Classifying-Movie-Reviews.ipynb │ ├── 22-Classifying-Newswires.ipynb │ ├── 23-Predicting-House-Prices.ipynb │ ├── 24-Overfitting-and-Underfitting.ipynb │ ├── 25-Introduction-to-ConvNets.ipynb │ ├── 26-Using-ConvNets-with-Small-Datasets.ipynb │ ├── 27-Using-a-Pretrained-ConvNet.ipynb │ ├── 28-Visualizing-What-ConvNets-Learn.ipynb │ ├── 29-Deep-Dream.ipynb │ ├── 30-Neural-Style-Transfer.ipynb │ ├── 31-Generating-Images-with-VAEs.ipynb │ └── 32-Introduction-to-GANs.ipynb ├── Cheatsheets ├── python_cheatsheet_dictionaries.pdf ├── python_cheatsheet_functions.pdf ├── python_cheatsheet_if_while.pdf └── python_cheatsheet_lists.pdf ├── Exercises ├── Assignment-1.ipynb ├── Assignment-2.ipynb ├── Assignment-3.ipynb ├── Exercise-01-Introduction.ipynb ├── Exercise-02-Python-Syntax-and-Semantics.ipynb ├── Exercise-04-Preview-of-Data-Science-Tools.ipynb ├── Exercise-07-Pandas-and-Missing-Values.ipynb └── Exercise-08-Aggregation-and-Grouping.ipynb.ipynb ├── LICENSE ├── Projects ├── 2020 │ ├── German-Traffic-Sign-Recognition │ │ ├── German-Traffic-Sign-Recognition-data-prep.ipynb │ │ ├── German_Traffic_Sign_Recognition_v1.ipynb │ │ ├── German_Traffic_Sign_Recognition_v2_augmentation.ipynb │ │ └── German_Traffic_Sign_Recognition_v3_VGG16.ipynb │ └── Messy-vs-Clean-Room │ │ ├── messy-vs-clean-room-data-prep.ipynb │ │ └── messy_vs_clean_room.ipynb └── Readme.md ├── README.md ├── chapter02_mathematical-building-blocks.ipynb ├── chapter03_introduction-to-keras-and-tf.ipynb ├── chapter04_getting-started-with-neural-networks.ipynb ├── chapter05_fundamentals-of-ml.ipynb ├── chapter07_working-with-keras.ipynb ├── chapter08_intro-to-dl-for-computer-vision.ipynb ├── chapter09_part01_image-segmentation.ipynb ├── chapter09_part02_modern-convnet-architecture-patterns.ipynb ├── chapter09_part03_interpreting-what-convnets-learn.ipynb ├── chapter10_dl-for-timeseries.ipynb ├── chapter11_part01_introduction.ipynb ├── chapter11_part02_sequence-models.ipynb ├── chapter11_part03_transformer.ipynb ├── chapter11_part04_sequence-to-sequence-learning.ipynb ├── chapter12_part01_text-generation.ipynb ├── chapter12_part02_deep-dream.ipynb ├── chapter12_part03_neural-style-transfer.ipynb ├── chapter12_part04_variational-autoencoders.ipynb ├── chapter12_part05_gans.ipynb ├── chapter13_best-practices-for-the-real-world.ipynb ├── chapter14_conclusions.ipynb └── data ├── 120-years-of-olympic-history-athletes-and-results ├── athlete_events.csv └── noc_regions.csv ├── 2017_StPaul_MN_Real_Estate.csv ├── FremontBridge.csv ├── GOOGL.csv ├── GOT-battles.csv ├── GOT-character-deaths.csv ├── Pandas-Example.xlsx ├── Telco-Customer-Churn.csv ├── anscombe.json ├── california_housing_test.csv ├── datasets.xlsx ├── movies_metadata.csv ├── movies_ratings.csv ├── mtcars.csv ├── state-abbrevs.csv ├── state-areas.csv └── state-population.csv /.gitignore: -------------------------------------------------------------------------------- 1 | materials 2 | *.docx 3 | *.swp 4 | KEY-*.ipynb 5 | *.html 6 | 7 | # Byte-compiled / optimized / DLL files 8 | __pycache__/ 9 | *.py[cod] 10 | *$py.class 11 | 12 | # C extensions 13 | *.so 14 | 15 | # Distribution / packaging 16 | .Python 17 | build/ 18 | develop-eggs/ 19 | dist/ 20 | downloads/ 21 | eggs/ 22 | .eggs/ 23 | lib/ 24 | lib64/ 25 | parts/ 26 | sdist/ 27 | var/ 28 | wheels/ 29 | *.egg-info/ 30 | .installed.cfg 31 | *.egg 32 | MANIFEST 33 | 34 | # PyInstaller 35 | # Usually these files are written by a python script from a template 36 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 37 | *.manifest 38 | *.spec 39 | 40 | # Installer logs 41 | pip-log.txt 42 | pip-delete-this-directory.txt 43 | 44 | # Unit test / coverage reports 45 | htmlcov/ 46 | .tox/ 47 | .coverage 48 | .coverage.* 49 | .cache 50 | nosetests.xml 51 | coverage.xml 52 | *.cover 53 | .hypothesis/ 54 | .pytest_cache/ 55 | 56 | # Translations 57 | *.mo 58 | *.pot 59 | 60 | # Django stuff: 61 | *.log 62 | local_settings.py 63 | db.sqlite3 64 | 65 | # Flask stuff: 66 | instance/ 67 | .webassets-cache 68 | 69 | # Scrapy stuff: 70 | .scrapy 71 | 72 | # Sphinx documentation 73 | docs/_build/ 74 | 75 | # PyBuilder 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # pyenv 82 | .python-version 83 | 84 | # celery beat schedule file 85 | celerybeat-schedule 86 | 87 | # SageMath parsed files 88 | *.sage.py 89 | 90 | # Environments 91 | .env 92 | .venv 93 | env/ 94 | venv/ 95 | ENV/ 96 | env.bak/ 97 | venv.bak/ 98 | 99 | # Spyder project settings 100 | .spyderproject 101 | .spyproject 102 | 103 | # Rope project settings 104 | .ropeproject 105 | 106 | # mkdocs documentation 107 | /site 108 | 109 | # mypy 110 | .mypy_cache/ 111 | 112 | notebooks 113 | -------------------------------------------------------------------------------- /00-Python-Basics/04-IPython-and-Shell-Commands.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# IPython and Shell Commands" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "When working interactively with the standard Python interpreter, one of the frustrations is the need to switch between multiple windows to access Python tools and system command-line tools.\n", 15 | "IPython bridges this gap, and gives you a syntax for executing shell commands directly from within the IPython terminal.\n", 16 | "The magic happens with the exclamation point: anything appearing after ``!`` on a line will be executed not by the Python kernel, but by the system command-line.\n", 17 | "\n", 18 | "The following assumes you're on a Unix-like system, such as Linux or Mac OSX.\n", 19 | "Some of the examples that follow will fail on Windows, which uses a different type of shell by default (though with the 2016 announcement of native Bash shells on Windows, soon this may no longer be an issue!).\n", 20 | "If you're unfamiliar with shell commands, I'd suggest reviewing the [Shell Tutorial](http://swcarpentry.github.io/shell-novice/) put together by the always excellent Software Carpentry Foundation." 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "metadata": {}, 26 | "source": [ 27 | "## Quick Introduction to the Shell\n", 28 | "\n", 29 | "A full intro to using the shell/terminal/command-line is well beyond the scope of this chapter, but for the uninitiated we will offer a quick introduction here.\n", 30 | "The shell is a way to interact textually with your computer.\n", 31 | "Ever since the mid 1980s, when Microsoft and Apple introduced the first versions of their now ubiquitous graphical operating systems, most computer users have interacted with their operating system through familiar clicking of menus and drag-and-drop movements.\n", 32 | "But operating systems existed long before these graphical user interfaces, and were primarily controlled through sequences of text input: at the prompt, the user would type a command, and the computer would do what the user told it to.\n", 33 | "Those early prompt systems are the precursors of the shells and terminals that most active data scientists still use today.\n", 34 | "\n", 35 | "Someone unfamiliar with the shell might ask why you would bother with this, when many results can be accomplished by simply clicking on icons and menus.\n", 36 | "A shell user might reply with another question: why hunt icons and click menus when you can accomplish things much more easily by typing?\n", 37 | "While it might sound like a typical tech preference impasse, when moving beyond basic tasks it quickly becomes clear that the shell offers much more control of advanced tasks, though admittedly the learning curve can intimidate the average computer user.\n", 38 | "\n", 39 | "As an example, here is a sample of a Linux/OSX shell session where a user explores, creates, and modifies directories and files on their system (``osx:~ $`` is the prompt, and everything after the ``$`` sign is the typed command; text that is preceded by a ``#`` is meant just as description, rather than something you would actually type in):\n", 40 | "\n", 41 | "```bash\n", 42 | "osx:~ $ echo \"hello world\" # echo is like Python's print function\n", 43 | "hello world\n", 44 | "\n", 45 | "osx:~ $ pwd # pwd = print working directory\n", 46 | "/home/jake # this is the \"path\" that we're sitting in\n", 47 | "\n", 48 | "osx:~ $ ls # ls = list working directory contents\n", 49 | "notebooks projects \n", 50 | "\n", 51 | "osx:~ $ cd projects/ # cd = change directory\n", 52 | "\n", 53 | "osx:projects $ pwd\n", 54 | "/home/jake/projects\n", 55 | "\n", 56 | "osx:projects $ ls\n", 57 | "datasci_book mpld3 myproject.txt\n", 58 | "\n", 59 | "osx:projects $ mkdir myproject # mkdir = make new directory\n", 60 | "\n", 61 | "osx:projects $ cd myproject/\n", 62 | "\n", 63 | "osx:myproject $ mv ../myproject.txt ./ # mv = move file. Here we're moving the\n", 64 | " # file myproject.txt from one directory\n", 65 | " # up (../) to the current directory (./)\n", 66 | "osx:myproject $ ls\n", 67 | "myproject.txt\n", 68 | "```\n", 69 | "\n", 70 | "Notice that all of this is just a compact way to do familiar operations (navigating a directory structure, creating a directory, moving a file, etc.) by typing commands rather than clicking icons and menus.\n", 71 | "Note that with just a few commands (``pwd``, ``ls``, ``cd``, ``mkdir``, and ``cp``) you can do many of the most common file operations.\n", 72 | "It's when you go beyond these basics that the shell approach becomes really powerful." 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "## Shell Commands in IPython\n", 80 | "\n", 81 | "Any command that works at the command-line can be used in IPython by prefixing it with the ``!`` character.\n", 82 | "For example, the ``ls``, ``pwd``, and ``echo`` commands can be run as follows:\n", 83 | "\n", 84 | "```ipython\n", 85 | "In [1]: !ls\n", 86 | "myproject.txt\n", 87 | "\n", 88 | "In [2]: !pwd\n", 89 | "/home/jake/projects/myproject\n", 90 | "\n", 91 | "In [3]: !echo \"printing from the shell\"\n", 92 | "printing from the shell\n", 93 | "```" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "## Passing Values to and from the Shell\n", 101 | "\n", 102 | "Shell commands can not only be called from IPython, but can also be made to interact with the IPython namespace.\n", 103 | "For example, you can save the output of any shell command to a Python list using the assignment operator:\n", 104 | "\n", 105 | "```ipython\n", 106 | "In [4]: contents = !ls\n", 107 | "\n", 108 | "In [5]: print(contents)\n", 109 | "['myproject.txt']\n", 110 | "\n", 111 | "In [6]: directory = !pwd\n", 112 | "\n", 113 | "In [7]: print(directory)\n", 114 | "['/Users/jakevdp/notebooks/tmp/myproject']\n", 115 | "```\n", 116 | "\n", 117 | "Note that these results are not returned as lists, but as a special shell return type defined in IPython:\n", 118 | "\n", 119 | "```ipython\n", 120 | "In [8]: type(directory)\n", 121 | "IPython.utils.text.SList\n", 122 | "```\n", 123 | "\n", 124 | "This looks and acts a lot like a Python list, but has additional functionality, such as\n", 125 | "the ``grep`` and ``fields`` methods and the ``s``, ``n``, and ``p`` properties that allow you to search, filter, and display the results in convenient ways.\n", 126 | "For more information on these, you can use IPython's built-in help features." 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "Communication in the other direction–passing Python variables into the shell–is possible using the ``{varname}`` syntax:\n", 134 | "\n", 135 | "```ipython\n", 136 | "In [9]: message = \"hello from Python\"\n", 137 | "\n", 138 | "In [10]: !echo {message}\n", 139 | "hello from Python\n", 140 | "```\n", 141 | "\n", 142 | "The curly braces contain the variable name, which is replaced by the variable's contents in the shell command." 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "# Shell-Related Magic Commands\n", 150 | "\n", 151 | "If you play with IPython's shell commands for a while, you might notice that you cannot use ``!cd`` to navigate the filesystem:\n", 152 | "\n", 153 | "```ipython\n", 154 | "In [11]: !pwd\n", 155 | "/home/jake/projects/myproject\n", 156 | "\n", 157 | "In [12]: !cd ..\n", 158 | "\n", 159 | "In [13]: !pwd\n", 160 | "/home/jake/projects/myproject\n", 161 | "```\n", 162 | "\n", 163 | "The reason is that shell commands in the notebook are executed in a temporary subshell.\n", 164 | "If you'd like to change the working directory in a more enduring way, you can use the ``%cd`` magic command:\n", 165 | "\n", 166 | "```ipython\n", 167 | "In [14]: %cd ..\n", 168 | "/home/jake/projects\n", 169 | "```\n", 170 | "\n", 171 | "In fact, by default you can even use this without the ``%`` sign:\n", 172 | "\n", 173 | "```ipython\n", 174 | "In [15]: cd myproject\n", 175 | "/home/jake/projects/myproject\n", 176 | "```\n", 177 | "\n", 178 | "This is known as an ``automagic`` function, and this behavior can be toggled with the ``%automagic`` magic function.\n", 179 | "\n", 180 | "Besides ``%cd``, other available shell-like magic functions are ``%cat``, ``%cp``, ``%env``, ``%ls``, ``%man``, ``%mkdir``, ``%more``, ``%mv``, ``%pwd``, ``%rm``, and ``%rmdir``, any of which can be used without the ``%`` sign if ``automagic`` is on.\n", 181 | "This makes it so that you can almost treat the IPython prompt as if it's a normal shell:\n", 182 | "\n", 183 | "```ipython\n", 184 | "In [16]: mkdir tmp\n", 185 | "\n", 186 | "In [17]: ls\n", 187 | "myproject.txt tmp/\n", 188 | "\n", 189 | "In [18]: cp myproject.txt tmp/\n", 190 | "\n", 191 | "In [19]: ls tmp\n", 192 | "myproject.txt\n", 193 | "\n", 194 | "In [20]: rm -r tmp\n", 195 | "```\n", 196 | "\n", 197 | "This access to the shell from within the same terminal window as your Python session means that there is a lot less switching back and forth between interpreter and shell as you write your Python code." 198 | ] 199 | } 200 | ], 201 | "metadata": { 202 | "kernelspec": { 203 | "display_name": "Python 3", 204 | "language": "python", 205 | "name": "python3" 206 | }, 207 | "language_info": { 208 | "codemirror_mode": { 209 | "name": "ipython", 210 | "version": 3 211 | }, 212 | "file_extension": ".py", 213 | "mimetype": "text/x-python", 214 | "name": "python", 215 | "nbconvert_exporter": "python", 216 | "pygments_lexer": "ipython3", 217 | "version": "3.7.2" 218 | } 219 | }, 220 | "nbformat": 4, 221 | "nbformat_minor": 2 222 | } 223 | -------------------------------------------------------------------------------- /00-Python-Basics/07-Generators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Generators" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Here we'll take a deeper dive into Python generators, including *generator expressions* and *generator functions*." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "## Generator Expressions" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "The difference between list comprehensions and generator expressions is sometimes confusing; here we'll quickly outline the differences between them:" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "### List comprehensions use square brackets, while generator expressions use parentheses\n", 36 | "This is a representative list comprehension:" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 1, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]" 48 | ] 49 | }, 50 | "execution_count": 1, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "[n ** 2 for n in range(12)]" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "While this is a representative generator expression:" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 2, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "data": { 73 | "text/plain": [ 74 | " at 0x111a84930>" 75 | ] 76 | }, 77 | "execution_count": 2, 78 | "metadata": {}, 79 | "output_type": "execute_result" 80 | } 81 | ], 82 | "source": [ 83 | "(n ** 2 for n in range(12))" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "Notice that printing the generator expression does not print the contents; one way to print the contents of a generator expression is to pass it to the ``list`` constructor:" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 3, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]" 102 | ] 103 | }, 104 | "execution_count": 3, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "G = (n ** 2 for n in range(12))\n", 111 | "list(G)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "### A list is a collection of values, while a generator is a recipe for producing values\n", 119 | "When you create a list, you are actually building a collection of values, and there is some memory cost associated with that.\n", 120 | "When you create a generator, you are not building a collection of values, but a recipe for producing those values.\n", 121 | "Both expose the same iterator interface, as we can see here:" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 4, 127 | "metadata": {}, 128 | "outputs": [ 129 | { 130 | "name": "stdout", 131 | "output_type": "stream", 132 | "text": [ 133 | "0 1 4 9 16 25 36 49 64 81 100 121 " 134 | ] 135 | } 136 | ], 137 | "source": [ 138 | "L = [n ** 2 for n in range(12)]\n", 139 | "for val in L:\n", 140 | " print(val, end=' ')" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 5, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "name": "stdout", 150 | "output_type": "stream", 151 | "text": [ 152 | "0 1 4 9 16 25 36 49 64 81 100 121 " 153 | ] 154 | } 155 | ], 156 | "source": [ 157 | "G = (n ** 2 for n in range(12))\n", 158 | "for val in G:\n", 159 | " print(val, end=' ')" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": {}, 165 | "source": [ 166 | "The difference is that a generator expression does not actually compute the values until they are needed.\n", 167 | "This not only leads to memory efficiency, but to computational efficiency as well!\n", 168 | "This also means that while the size of a list is limited by available memory, the size of a generator expression is unlimited!\n", 169 | "\n", 170 | "An example of an infinite generator expression can be created using the ``count`` iterator defined in ``itertools``:" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 6, 176 | "metadata": {}, 177 | "outputs": [ 178 | { 179 | "data": { 180 | "text/plain": [ 181 | "count(0)" 182 | ] 183 | }, 184 | "execution_count": 6, 185 | "metadata": {}, 186 | "output_type": "execute_result" 187 | } 188 | ], 189 | "source": [ 190 | "from itertools import count\n", 191 | "count()" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 7, 197 | "metadata": {}, 198 | "outputs": [ 199 | { 200 | "name": "stdout", 201 | "output_type": "stream", 202 | "text": [ 203 | "0 1 2 3 4 5 6 7 8 9 10 " 204 | ] 205 | } 206 | ], 207 | "source": [ 208 | "for i in count():\n", 209 | " print(i, end=' ')\n", 210 | " if i >= 10: break" 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "The ``count`` iterator will go on happily counting forever until you tell it to stop; this makes it convenient to create generators that will also go on forever:" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 8, 223 | "metadata": {}, 224 | "outputs": [ 225 | { 226 | "name": "stdout", 227 | "output_type": "stream", 228 | "text": [ 229 | "1 11 13 17 19 23 29 31 37 41 " 230 | ] 231 | } 232 | ], 233 | "source": [ 234 | "factors = [2, 3, 5, 7]\n", 235 | "G = (i for i in count() if all(i % n > 0 for n in factors))\n", 236 | "for val in G:\n", 237 | " print(val, end=' ')\n", 238 | " if val > 40: break" 239 | ] 240 | }, 241 | { 242 | "cell_type": "markdown", 243 | "metadata": {}, 244 | "source": [ 245 | "Here we have used `all()` function: Returns True if bool(x) is True for all values x in the iterable.\n", 246 | "\n", 247 | "You might see what we're getting at here: if we were to expand the list of factors appropriately, what we would have the beginnings of is a prime number generator, using the Sieve of Eratosthenes algorithm. We'll explore this more momentarily." 248 | ] 249 | }, 250 | { 251 | "cell_type": "markdown", 252 | "metadata": {}, 253 | "source": [ 254 | "### A list can be iterated multiple times; a generator expression is single-use\n", 255 | "This is one of those potential gotchas of generator expressions.\n", 256 | "With a list, we can straightforwardly do this:" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 9, 262 | "metadata": {}, 263 | "outputs": [ 264 | { 265 | "name": "stdout", 266 | "output_type": "stream", 267 | "text": [ 268 | "0 1 4 9 16 25 36 49 64 81 100 121 \n", 269 | "0 1 4 9 16 25 36 49 64 81 100 121 " 270 | ] 271 | } 272 | ], 273 | "source": [ 274 | "L = [n ** 2 for n in range(12)]\n", 275 | "for val in L:\n", 276 | " print(val, end=' ')\n", 277 | "print()\n", 278 | "\n", 279 | "for val in L:\n", 280 | " print(val, end=' ')" 281 | ] 282 | }, 283 | { 284 | "cell_type": "markdown", 285 | "metadata": {}, 286 | "source": [ 287 | "A generator expression, on the other hand, is used-up after one iteration:" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 10, 293 | "metadata": {}, 294 | "outputs": [ 295 | { 296 | "data": { 297 | "text/plain": [ 298 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]" 299 | ] 300 | }, 301 | "execution_count": 10, 302 | "metadata": {}, 303 | "output_type": "execute_result" 304 | } 305 | ], 306 | "source": [ 307 | "G = (n ** 2 for n in range(12))\n", 308 | "list(G)" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 11, 314 | "metadata": {}, 315 | "outputs": [ 316 | { 317 | "data": { 318 | "text/plain": [ 319 | "[]" 320 | ] 321 | }, 322 | "execution_count": 11, 323 | "metadata": {}, 324 | "output_type": "execute_result" 325 | } 326 | ], 327 | "source": [ 328 | "list(G)" 329 | ] 330 | }, 331 | { 332 | "cell_type": "markdown", 333 | "metadata": {}, 334 | "source": [ 335 | "This can be very useful because it means iteration can be stopped and started:" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": 12, 341 | "metadata": {}, 342 | "outputs": [ 343 | { 344 | "name": "stdout", 345 | "output_type": "stream", 346 | "text": [ 347 | "0 1 4 9 16 25 36 \n", 348 | "doing something in between\n", 349 | "49 64 81 100 121 " 350 | ] 351 | } 352 | ], 353 | "source": [ 354 | "G = (n**2 for n in range(12))\n", 355 | "for n in G:\n", 356 | " print(n, end=' ')\n", 357 | " if n > 30: break\n", 358 | "\n", 359 | "print(\"\\ndoing something in between\")\n", 360 | "\n", 361 | "for n in G:\n", 362 | " print(n, end=' ')" 363 | ] 364 | }, 365 | { 366 | "cell_type": "markdown", 367 | "metadata": {}, 368 | "source": [ 369 | "One place I've found this useful is when working with collections of data files on disk; it means that you can quite easily analyze them in batches, letting the generator keep track of which ones you have yet to see." 370 | ] 371 | }, 372 | { 373 | "cell_type": "markdown", 374 | "metadata": {}, 375 | "source": [ 376 | "## Generator Functions: Using ``yield``\n", 377 | "We saw in the previous section that list comprehensions are best used to create relatively simple lists, while using a normal ``for`` loop can be better in more complicated situations.\n", 378 | "The same is true of generator expressions: we can make more complicated generators using *generator functions*, which make use of the ``yield`` statement.\n", 379 | "\n", 380 | "Here we have two ways of constructing the same list:" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": 13, 386 | "metadata": {}, 387 | "outputs": [ 388 | { 389 | "name": "stdout", 390 | "output_type": "stream", 391 | "text": [ 392 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]\n", 393 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]\n" 394 | ] 395 | } 396 | ], 397 | "source": [ 398 | "L1 = [n ** 2 for n in range(12)]\n", 399 | "\n", 400 | "L2 = []\n", 401 | "for n in range(12):\n", 402 | " L2.append(n ** 2)\n", 403 | "\n", 404 | "print(L1)\n", 405 | "print(L2)" 406 | ] 407 | }, 408 | { 409 | "cell_type": "markdown", 410 | "metadata": {}, 411 | "source": [ 412 | "Similarly, here we have two ways of constructing equivalent generators:" 413 | ] 414 | }, 415 | { 416 | "cell_type": "code", 417 | "execution_count": 14, 418 | "metadata": {}, 419 | "outputs": [ 420 | { 421 | "name": "stdout", 422 | "output_type": "stream", 423 | "text": [ 424 | "0 1 4 9 16 25 36 49 64 81 100 121\n", 425 | "0 1 4 9 16 25 36 49 64 81 100 121\n" 426 | ] 427 | } 428 | ], 429 | "source": [ 430 | "G1 = (n ** 2 for n in range(12))\n", 431 | "\n", 432 | "def gen():\n", 433 | " for n in range(12):\n", 434 | " yield n ** 2\n", 435 | "\n", 436 | "G2 = gen()\n", 437 | "print(*G1)\n", 438 | "print(*G2)" 439 | ] 440 | }, 441 | { 442 | "cell_type": "markdown", 443 | "metadata": {}, 444 | "source": [ 445 | "A generator function is a function that, rather than using ``return`` to return a value once, uses ``yield`` to yield a (potentially infinite) sequence of values.\n", 446 | "Just as in generator expressions, the state of the generator is preserved between partial iterations, but if we want a fresh copy of the generator we can simply call the function again." 447 | ] 448 | }, 449 | { 450 | "cell_type": "markdown", 451 | "metadata": {}, 452 | "source": [ 453 | "## Example: Prime Number Generator\n", 454 | "Here I'll show my favorite example of a generator function: a function to generate an unbounded series of prime numbers.\n", 455 | "A classic algorithm for this is the *Sieve of Eratosthenes*, which works something like this:" 456 | ] 457 | }, 458 | { 459 | "cell_type": "code", 460 | "execution_count": 15, 461 | "metadata": {}, 462 | "outputs": [ 463 | { 464 | "name": "stdout", 465 | "output_type": "stream", 466 | "text": [ 467 | "[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]\n" 468 | ] 469 | } 470 | ], 471 | "source": [ 472 | "# Generate a list of candidates\n", 473 | "L = [n for n in range(2, 40)]\n", 474 | "print(L)" 475 | ] 476 | }, 477 | { 478 | "cell_type": "code", 479 | "execution_count": 16, 480 | "metadata": {}, 481 | "outputs": [ 482 | { 483 | "name": "stdout", 484 | "output_type": "stream", 485 | "text": [ 486 | "[2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39]\n" 487 | ] 488 | } 489 | ], 490 | "source": [ 491 | "# Remove all multiples of the first value\n", 492 | "L = [n for n in L if n == L[0] or n % L[0] > 0]\n", 493 | "print(L)" 494 | ] 495 | }, 496 | { 497 | "cell_type": "code", 498 | "execution_count": 17, 499 | "metadata": {}, 500 | "outputs": [ 501 | { 502 | "name": "stdout", 503 | "output_type": "stream", 504 | "text": [ 505 | "[2, 3, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37]\n" 506 | ] 507 | } 508 | ], 509 | "source": [ 510 | "# Remove all multiples of the second value\n", 511 | "L = [n for n in L if n == L[1] or n % L[1] > 0]\n", 512 | "print(L)" 513 | ] 514 | }, 515 | { 516 | "cell_type": "code", 517 | "execution_count": 18, 518 | "metadata": {}, 519 | "outputs": [ 520 | { 521 | "name": "stdout", 522 | "output_type": "stream", 523 | "text": [ 524 | "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]\n" 525 | ] 526 | } 527 | ], 528 | "source": [ 529 | "# Remove all multiples of the third value\n", 530 | "L = [n for n in L if n == L[2] or n % L[2] > 0]\n", 531 | "print(L)" 532 | ] 533 | }, 534 | { 535 | "cell_type": "markdown", 536 | "metadata": {}, 537 | "source": [ 538 | "If we repeat this procedure enough times on a large enough list, we can generate as many primes as we wish.\n", 539 | "\n", 540 | "Let's encapsulate this logic in a generator function:" 541 | ] 542 | }, 543 | { 544 | "cell_type": "code", 545 | "execution_count": 19, 546 | "metadata": {}, 547 | "outputs": [ 548 | { 549 | "name": "stdout", 550 | "output_type": "stream", 551 | "text": [ 552 | "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97\n" 553 | ] 554 | } 555 | ], 556 | "source": [ 557 | "def gen_primes(N):\n", 558 | " \"\"\"Generate primes up to N\"\"\"\n", 559 | " primes = set()\n", 560 | " for n in range(2, N):\n", 561 | " if all(n % p > 0 for p in primes):\n", 562 | " primes.add(n)\n", 563 | " yield n\n", 564 | "\n", 565 | "print(*gen_primes(100))" 566 | ] 567 | }, 568 | { 569 | "cell_type": "markdown", 570 | "metadata": {}, 571 | "source": [ 572 | "That's all there is to it!\n", 573 | "While this is certainly not the most computationally efficient implementation of the Sieve of Eratosthenes, it illustrates how convenient the generator function syntax can be for building more complicated sequences." 574 | ] 575 | } 576 | ], 577 | "metadata": { 578 | "kernelspec": { 579 | "display_name": "Python 3", 580 | "language": "python", 581 | "name": "python3" 582 | }, 583 | "language_info": { 584 | "codemirror_mode": { 585 | "name": "ipython", 586 | "version": 3 587 | }, 588 | "file_extension": ".py", 589 | "mimetype": "text/x-python", 590 | "name": "python", 591 | "nbconvert_exporter": "python", 592 | "pygments_lexer": "ipython3", 593 | "version": "3.7.2" 594 | } 595 | }, 596 | "nbformat": 4, 597 | "nbformat_minor": 2 598 | } 599 | -------------------------------------------------------------------------------- /00-Python-Basics/08-Modules-and-Packages.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Modules and Packages" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "One feature of Python that makes it useful for a wide range of tasks is the fact that it comes \"batteries included\" – that is, the Python standard library contains useful tools for a wide range of tasks.\n", 15 | "On top of this, there is a broad ecosystem of third-party tools and packages that offer more specialized functionality.\n", 16 | "Here we'll take a look at importing standard library modules, tools for installing third-party modules, and a description of how you can make your own modules." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "## Loading Modules: the ``import`` Statement\n", 24 | "\n", 25 | "For loading built-in and third-party modules, Python provides the ``import`` statement.\n", 26 | "There are a few ways to use the statement, which we will mention briefly here, from most recommended to least recommended." 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": {}, 32 | "source": [ 33 | "### Explicit module import\n", 34 | "\n", 35 | "Explicit import of a module preserves the module's content in a namespace.\n", 36 | "The namespace is then used to refer to its contents with a \"``.``\" between them.\n", 37 | "For example, here we'll import the built-in ``math`` module and compute the cosine of pi:" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 1, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "data": { 47 | "text/plain": [ 48 | "-1.0" 49 | ] 50 | }, 51 | "execution_count": 1, 52 | "metadata": {}, 53 | "output_type": "execute_result" 54 | } 55 | ], 56 | "source": [ 57 | "import math\n", 58 | "math.cos(math.pi)" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "### Explicit module import by alias\n", 66 | "\n", 67 | "For longer module names, it's not convenient to use the full module name each time you access some element.\n", 68 | "For this reason, we'll commonly use the \"``import ... as ...``\" pattern to create a shorter alias for the namespace.\n", 69 | "For example, the NumPy (Numerical Python) package, a popular third-party package useful for data science, is by convention imported under the alias ``np``:" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 2, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "data": { 79 | "text/plain": [ 80 | "-1.0" 81 | ] 82 | }, 83 | "execution_count": 2, 84 | "metadata": {}, 85 | "output_type": "execute_result" 86 | } 87 | ], 88 | "source": [ 89 | "import numpy as np\n", 90 | "np.cos(np.pi)" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "### Explicit import of module contents\n", 98 | "\n", 99 | "Sometimes rather than importing the module namespace, you would just like to import a few particular items from the module.\n", 100 | "This can be done with the \"``from ... import ...``\" pattern.\n", 101 | "For example, we can import just the ``cos`` function and the ``pi`` constant from the ``math`` module:" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 3, 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "-1.0" 113 | ] 114 | }, 115 | "execution_count": 3, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "from math import cos, pi\n", 122 | "cos(pi)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "### Implicit import of module contents\n", 130 | "\n", 131 | "Finally, it is sometimes useful to import the entirety of the module contents into the local namespace.\n", 132 | "This can be done with the \"``from ... import *``\" pattern:" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 4, 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "data": { 142 | "text/plain": [ 143 | "1.0" 144 | ] 145 | }, 146 | "execution_count": 4, 147 | "metadata": {}, 148 | "output_type": "execute_result" 149 | } 150 | ], 151 | "source": [ 152 | "from math import *\n", 153 | "sin(pi) ** 2 + cos(pi) ** 2" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "This pattern should be used sparingly, if at all.\n", 161 | "The problem is that such imports can sometimes overwrite function names that you do not intend to overwrite, and the implicitness of the statement makes it difficult to determine what has changed.\n", 162 | "\n", 163 | "For example, Python has a built-in ``sum`` function that can be used for various operations:" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 5, 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "name": "stdout", 173 | "output_type": "stream", 174 | "text": [ 175 | "Help on built-in function sum in module builtins:\n", 176 | "\n", 177 | "sum(iterable, start=0, /)\n", 178 | " Return the sum of a 'start' value (default: 0) plus an iterable of numbers\n", 179 | " \n", 180 | " When the iterable is empty, return the start value.\n", 181 | " This function is intended specifically for use with numeric values and may\n", 182 | " reject non-numeric types.\n", 183 | "\n" 184 | ] 185 | } 186 | ], 187 | "source": [ 188 | "help(sum)" 189 | ] 190 | }, 191 | { 192 | "cell_type": "markdown", 193 | "metadata": {}, 194 | "source": [ 195 | "We can use this to compute the sum of a sequence, starting with a certain value (here, we'll start with ``-1``):" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 6, 201 | "metadata": {}, 202 | "outputs": [ 203 | { 204 | "data": { 205 | "text/plain": [ 206 | "9" 207 | ] 208 | }, 209 | "execution_count": 6, 210 | "metadata": {}, 211 | "output_type": "execute_result" 212 | } 213 | ], 214 | "source": [ 215 | "sum(range(5), -1)" 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "metadata": {}, 221 | "source": [ 222 | "Now observe what happens if we make the *exact same function call* after importing ``*`` from ``numpy``:" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": 7, 228 | "metadata": {}, 229 | "outputs": [], 230 | "source": [ 231 | "from numpy import *" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": 8, 237 | "metadata": {}, 238 | "outputs": [ 239 | { 240 | "data": { 241 | "text/plain": [ 242 | "10" 243 | ] 244 | }, 245 | "execution_count": 8, 246 | "metadata": {}, 247 | "output_type": "execute_result" 248 | } 249 | ], 250 | "source": [ 251 | "sum(range(5), -1)" 252 | ] 253 | }, 254 | { 255 | "cell_type": "markdown", 256 | "metadata": {}, 257 | "source": [ 258 | "The result is off by one!\n", 259 | "The reason for this is that the ``import *`` statement *replaces* the built-in ``sum`` function with the ``numpy.sum`` function, which has a different call signature: in the former, we're summing ``range(5)`` starting at ``-1``; in the latter, we're summing ``range(5)`` along the last axis (indicated by ``-1``).\n", 260 | "This is the type of situation that may arise if care is not taken when using \"``import *``\" – for this reason, it is best to avoid this unless you know exactly what you are doing." 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": {}, 266 | "source": [ 267 | "## Importing from Python's Standard Library\n", 268 | "\n", 269 | "Python's standard library contains many useful built-in modules, which you can read about fully in [Python's documentation](https://docs.python.org/3/library/).\n", 270 | "Any of these can be imported with the ``import`` statement, and then explored using the help function seen in the previous section.\n", 271 | "Here is an extremely incomplete list of some of the modules you might wish to explore and learn about:\n", 272 | "\n", 273 | "- ``os`` and ``sys``: Tools for interfacing with the operating system, including navigating file directory structures and executing shell commands\n", 274 | "- ``math`` and ``cmath``: Mathematical functions and operations on real and complex numbers\n", 275 | "- ``itertools``: Tools for constructing and interacting with iterators and generators\n", 276 | "- ``functools``: Tools that assist with functional programming\n", 277 | "- ``random``: Tools for generating pseudorandom numbers\n", 278 | "- ``pickle``: Tools for object persistence: saving objects to and loading objects from disk\n", 279 | "- ``json`` and ``csv``: Tools for reading JSON-formatted and CSV-formatted files.\n", 280 | "- ``urllib``: Tools for doing HTTP and other web requests.\n", 281 | "\n", 282 | "You can find information on these, and many more, in the Python standard library documentation: https://docs.python.org/3/library/." 283 | ] 284 | }, 285 | { 286 | "cell_type": "markdown", 287 | "metadata": {}, 288 | "source": [ 289 | "## Importing from Third-Party Modules\n", 290 | "\n", 291 | "One of the things that makes Python useful, especially within the world of data science, is its ecosystem of third-party modules.\n", 292 | "These can be imported just as the built-in modules, but first the modules must be installed on your system. To install common third-party packages one can use one of the following approaches:\n", 293 | "\n", 294 | "### Conda\n", 295 | "Conda is an open source package management system and environment management system that runs on Windows, macOS and Linux. Conda quickly installs, runs and updates packages and their dependencies. \n", 296 | "For example, if you'd like to install the ``numpy`` package, all that is required is to type the following at the command line:\n", 297 | "```\n", 298 | "$ conda install numpy\n", 299 | "```\n", 300 | "\n", 301 | "As we saw in the first class you can access **Conda** by installing Miniconda. [Miniconda](http://conda.pydata.org/miniconda.html) gives you Python interpreter itself, along with a command-line tool called ``conda`` which operates as a cross-platform package manager geared toward Python packages\n", 302 | "\n", 303 | "### pip\n", 304 | "Another standard registry for such modules is the Python Package Index (*PyPI* for short), found on the Web at http://pypi.python.org/.\n", 305 | "For convenience, Python comes with a program called ``pip`` (a recursive acronym meaning \"pip installs packages\"), which will automatically fetch packages released and listed on PyPI (if you use Python version 2, ``pip`` must be installed separately).\n", 306 | "For example, if you'd like to install the ``numpy`` package, all that is required is to type the following at the command line:\n", 307 | "```\n", 308 | "$ pip install numpy\n", 309 | "```\n", 310 | "The source code for the package will be automatically downloaded from the PyPI repository, and the package installed in the standard Python path (assuming you have permission to do so on the computer you're using).\n", 311 | "\n", 312 | "For more information about PyPI and the ``pip`` installer, refer to the documentation at http://pypi.python.org/." 313 | ] 314 | } 315 | ], 316 | "metadata": { 317 | "kernelspec": { 318 | "display_name": "Python 3", 319 | "language": "python", 320 | "name": "python3" 321 | }, 322 | "language_info": { 323 | "codemirror_mode": { 324 | "name": "ipython", 325 | "version": 3 326 | }, 327 | "file_extension": ".py", 328 | "mimetype": "text/x-python", 329 | "name": "python", 330 | "nbconvert_exporter": "python", 331 | "pygments_lexer": "ipython3", 332 | "version": "3.7.2" 333 | } 334 | }, 335 | "nbformat": 4, 336 | "nbformat_minor": 2 337 | } 338 | -------------------------------------------------------------------------------- /00-Python-Basics/Exercise-01-Built-in-Scalar-Types.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise\n", 8 | "Capitalize the following string:" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "metadata": {}, 15 | "outputs": [], 16 | "source": [ 17 | "text1 = \"python\"" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 2, 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "data": { 27 | "text/plain": [ 28 | "'Python'" 29 | ] 30 | }, 31 | "execution_count": 2, 32 | "metadata": {}, 33 | "output_type": "execute_result" 34 | } 35 | ], 36 | "source": [ 37 | "# Your answer goes here\n" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "Convert the following string to lowercase:" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 3, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "text2 = \"Python-Bootcamp\"" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 4, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "data": { 63 | "text/plain": [ 64 | "'python-bootcamp'" 65 | ] 66 | }, 67 | "execution_count": 4, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "# Your answer goes here\n" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "Did these methods change the original string? If not, how can you ensure that your changes are saved?" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 5, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "name": "stdout", 90 | "output_type": "stream", 91 | "text": [ 92 | "python-bootcamp\n" 93 | ] 94 | } 95 | ], 96 | "source": [ 97 | "# Your answer goes here\n" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "How can you access the third character of `text2`?" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 6, 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "data": { 114 | "text/plain": [ 115 | "'T'" 116 | ] 117 | }, 118 | "execution_count": 6, 119 | "metadata": {}, 120 | "output_type": "execute_result" 121 | } 122 | ], 123 | "source": [ 124 | "# Your answer goes here\n" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "Print the second half of text2:\n", 132 | "\n", 133 | "Hint 1: strings have a method called `split()`. This method will split the string into pieces based on the delimiter provided. \n", 134 | "\n", 135 | "Hint 2: One way to access the help documentation in notebook is by pressing Shift + Tab while the cursor is on the word. Try typing `text2.split` and then hit Shift + Tab." 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 7, 141 | "metadata": {}, 142 | "outputs": [ 143 | { 144 | "data": { 145 | "text/plain": [ 146 | "'bootcamp'" 147 | ] 148 | }, 149 | "execution_count": 7, 150 | "metadata": {}, 151 | "output_type": "execute_result" 152 | } 153 | ], 154 | "source": [ 155 | "# Your answer goes here\n" 156 | ] 157 | } 158 | ], 159 | "metadata": { 160 | "kernelspec": { 161 | "display_name": "Python 3", 162 | "language": "python", 163 | "name": "python3" 164 | }, 165 | "language_info": { 166 | "codemirror_mode": { 167 | "name": "ipython", 168 | "version": 3 169 | }, 170 | "file_extension": ".py", 171 | "mimetype": "text/x-python", 172 | "name": "python", 173 | "nbconvert_exporter": "python", 174 | "pygments_lexer": "ipython3", 175 | "version": "3.7.2" 176 | } 177 | }, 178 | "nbformat": 4, 179 | "nbformat_minor": 2 180 | } 181 | -------------------------------------------------------------------------------- /00-Python-Basics/Exercise-02-Built-in-Data-Structures.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise 1\n", 8 | "Write a Python program which accepts a sequence of comma-separated numbers from user and generates a list and a tuple with those numbers.\n", 9 | "\n", 10 | "Sample data: 4,6,9\n", 11 | "\n", 12 | "Output: \n", 13 | "\n", 14 | "List : ['4', '6', '9']\n", 15 | "\n", 16 | "Tuple : ('4', '6', '9')\n", 17 | "\n", 18 | "Hint: you can use `input()` function to get user's input through keyboard. The input values can be stored to a variable. E.g.: \n", 19 | "\n", 20 | "```values = input(\"Input some comma seprated numbers : \")```" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 1, 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "name": "stdout", 30 | "output_type": "stream", 31 | "text": [ 32 | "Input some comma seprated numbers : 4,6,9\n", 33 | "List : ['4', '6', '9']\n", 34 | "Tuple : ('4', '6', '9')\n" 35 | ] 36 | } 37 | ], 38 | "source": [ 39 | "# Your answer goes here\n" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "What is the size of the tuple?" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 2, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "3" 58 | ] 59 | }, 60 | "execution_count": 2, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "# Your answer goes here\n" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "Add a new element '999' to the end of the list and print it:" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 3, 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "name": "stdout", 83 | "output_type": "stream", 84 | "text": [ 85 | "['4', '6', '9', '999']\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "# Your answer goes here\n" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "Can you do the same thing with the tuple?" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 4, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "# Your answer goes here\n" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": {}, 112 | "source": [ 113 | "# Exercise 2\n", 114 | "Given this nested list, use indexing to grab the word \"hello\"" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 5, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [ 123 | "lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7]" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 6, 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "hello\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "# Your answer goes here\n" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "# Exercise 3\n", 148 | "Given this nested dictionary grab the word \"hello\". Be prepared, this will be annoying/tricky" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 7, 154 | "metadata": {}, 155 | "outputs": [], 156 | "source": [ 157 | "d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]}" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 8, 163 | "metadata": {}, 164 | "outputs": [ 165 | { 166 | "name": "stdout", 167 | "output_type": "stream", 168 | "text": [ 169 | "hello\n" 170 | ] 171 | } 172 | ], 173 | "source": [ 174 | "# Your answer goes here\n" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "# Exercise 4\n", 182 | "Write a Python program to accept a filename from the user and print the extension of that.\n", 183 | "\n", 184 | "Example:\n", 185 | "\n", 186 | "Input the Filename: my.file.csv\n", 187 | "\n", 188 | "The extension of the file is : 'csv'" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 9, 194 | "metadata": {}, 195 | "outputs": [ 196 | { 197 | "name": "stdout", 198 | "output_type": "stream", 199 | "text": [ 200 | "Input the Filename: my.file.csv\n", 201 | "The extension of the file is : 'csv'\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "# Your answer goes here\n" 207 | ] 208 | }, 209 | { 210 | "cell_type": "markdown", 211 | "metadata": {}, 212 | "source": [ 213 | "# Exercise 5\n", 214 | "Display the first and last colors from the following list:" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 10, 220 | "metadata": {}, 221 | "outputs": [], 222 | "source": [ 223 | "color_list = [\"Red\",\"Green\",\"White\" ,\"Black\"]" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": 11, 229 | "metadata": {}, 230 | "outputs": [ 231 | { 232 | "data": { 233 | "text/plain": [ 234 | "'Red Black'" 235 | ] 236 | }, 237 | "execution_count": 11, 238 | "metadata": {}, 239 | "output_type": "execute_result" 240 | } 241 | ], 242 | "source": [ 243 | "# Your answer goes here\n" 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": {}, 249 | "source": [ 250 | "# Exercise 6\n", 251 | "Write a Python program that accepts an integer (n) and computes the value of n+nn+nnn.\n", 252 | "\n", 253 | "Example: n = 10 ==> Output: 10+1010+101010 = 102030" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": 12, 259 | "metadata": {}, 260 | "outputs": [ 261 | { 262 | "name": "stdout", 263 | "output_type": "stream", 264 | "text": [ 265 | "Input an integer : 10\n", 266 | "102030\n" 267 | ] 268 | } 269 | ], 270 | "source": [ 271 | "# Your answer goes here\n" 272 | ] 273 | }, 274 | { 275 | "cell_type": "markdown", 276 | "metadata": {}, 277 | "source": [ 278 | "# Exercise 7\n", 279 | "Write a Python program that can detect whether a sting appears in another string. Make it case insensitive.\n", 280 | "\n", 281 | "Example:\n", 282 | "\n", 283 | "a = \"Cat\"\n", 284 | "\n", 285 | "b = \"Do cats dream?\"\n", 286 | "\n", 287 | "Does a appear in b? ==> Output: True|" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 13, 293 | "metadata": {}, 294 | "outputs": [ 295 | { 296 | "data": { 297 | "text/plain": [ 298 | "True" 299 | ] 300 | }, 301 | "execution_count": 13, 302 | "metadata": {}, 303 | "output_type": "execute_result" 304 | } 305 | ], 306 | "source": [ 307 | "# Your answer goes here\n" 308 | ] 309 | }, 310 | { 311 | "cell_type": "markdown", 312 | "metadata": {}, 313 | "source": [ 314 | "How many string characters are in b? (white space counts)" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 14, 320 | "metadata": {}, 321 | "outputs": [ 322 | { 323 | "data": { 324 | "text/plain": [ 325 | "14" 326 | ] 327 | }, 328 | "execution_count": 14, 329 | "metadata": {}, 330 | "output_type": "execute_result" 331 | } 332 | ], 333 | "source": [ 334 | "# Your answer goes here\n" 335 | ] 336 | }, 337 | { 338 | "cell_type": "markdown", 339 | "metadata": {}, 340 | "source": [ 341 | "# Exercise 8\n", 342 | "Write a Python program to print the documents (syntax, description etc.) of Python built-in function(s).\n", 343 | "\n", 344 | "Try to find an answer online.\n", 345 | "\n", 346 | "Example input: print\n", 347 | "\n", 348 | "```\n", 349 | "print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", 350 | "\n", 351 | "Prints the values to a stream, or to sys.stdout by default.\n", 352 | "Optional keyword arguments:\n", 353 | "file: a file-like object (stream); defaults to the current sys.stdout.\n", 354 | "sep: string inserted between values, default a space.\n", 355 | "end: string appended after the last value, default a newline.\n", 356 | "flush: whether to forcibly flush the stream.\n", 357 | "```" 358 | ] 359 | }, 360 | { 361 | "cell_type": "code", 362 | "execution_count": 15, 363 | "metadata": {}, 364 | "outputs": [ 365 | { 366 | "name": "stdout", 367 | "output_type": "stream", 368 | "text": [ 369 | "print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", 370 | "\n", 371 | "Prints the values to a stream, or to sys.stdout by default.\n", 372 | "Optional keyword arguments:\n", 373 | "file: a file-like object (stream); defaults to the current sys.stdout.\n", 374 | "sep: string inserted between values, default a space.\n", 375 | "end: string appended after the last value, default a newline.\n", 376 | "flush: whether to forcibly flush the stream.\n" 377 | ] 378 | } 379 | ], 380 | "source": [ 381 | "# Your answer goes here\n" 382 | ] 383 | } 384 | ], 385 | "metadata": { 386 | "kernelspec": { 387 | "display_name": "Python 3", 388 | "language": "python", 389 | "name": "python3" 390 | }, 391 | "language_info": { 392 | "codemirror_mode": { 393 | "name": "ipython", 394 | "version": 3 395 | }, 396 | "file_extension": ".py", 397 | "mimetype": "text/x-python", 398 | "name": "python", 399 | "nbconvert_exporter": "python", 400 | "pygments_lexer": "ipython3", 401 | "version": "3.7.2" 402 | } 403 | }, 404 | "nbformat": 4, 405 | "nbformat_minor": 2 406 | } 407 | -------------------------------------------------------------------------------- /00-Python-Basics/Exercise-03-Control-Flow-Statements.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise 1\n", 8 | "Write a program to sum all the items in a list.\n", 9 | "\n", 10 | "L = [1, 6, 10] ==> Output: 17" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | "17\n" 23 | ] 24 | } 25 | ], 26 | "source": [ 27 | "# Your answer goes here\n" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": {}, 33 | "source": [ 34 | "# Exercise 2\n", 35 | "Find all the numbers between 1800 and 1995 (both included) that are divisible by 7 and multiple of 5.\n", 36 | "\n", 37 | "How many are there?" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 2, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "[1820, 1855, 1890, 1925, 1960, 1995]\n", 50 | "6\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "# Your answer goes here\n" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "# Exercise 3\n", 63 | "Write a program that prints each item and its corresponding type from the following list." 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 3, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [ 72 | "datalist = [1452, 11.23, 1+2j, True, 'Python', (0, -1), [5, 12], {\"class\":'V', \"section\":'A'}, {1,2,3}]" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 4, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "name": "stdout", 82 | "output_type": "stream", 83 | "text": [ 84 | "Type of 1452 is: \n", 85 | "Type of 11.23 is: \n", 86 | "Type of (1+2j) is: \n", 87 | "Type of True is: \n", 88 | "Type of Python is: \n", 89 | "Type of (0, -1) is: \n", 90 | "Type of [5, 12] is: \n", 91 | "Type of {'class': 'V', 'section': 'A'} is: \n", 92 | "Type of {1, 2, 3} is: \n" 93 | ] 94 | } 95 | ], 96 | "source": [ 97 | "# Your answer goes here\n" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "# Exercise 4\n", 105 | "Write a program which iterates the integers from 1 to 20. For multiples of three print \"Fizz\" instead of the number and for the multiples of five print \"Buzz\". For numbers which are multiples of both three and five print \"FizzBuzz\".\n", 106 | "\n", 107 | "Sample Output : \n", 108 | "\n", 109 | "1\n", 110 | "\n", 111 | "2\n", 112 | "\n", 113 | "fizz\n", 114 | "\n", 115 | "4 \n", 116 | "\n", 117 | "buzz" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 5, 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "name": "stdout", 127 | "output_type": "stream", 128 | "text": [ 129 | "1\n", 130 | "2\n", 131 | "fizz\n", 132 | "4\n", 133 | "buzz\n", 134 | "fizz\n", 135 | "7\n", 136 | "8\n", 137 | "fizz\n", 138 | "buzz\n", 139 | "11\n", 140 | "fizz\n", 141 | "13\n", 142 | "14\n", 143 | "fizzbuzz\n", 144 | "16\n", 145 | "17\n", 146 | "fizz\n", 147 | "19\n", 148 | "buzz\n" 149 | ] 150 | } 151 | ], 152 | "source": [ 153 | "# Your answer goes here\n" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "# Exercise 5\n", 161 | "Write a program to convert temperatures to and from celsius, fahrenheit.\n", 162 | "\n", 163 | "[ Formula : c = (f - 32) * 5/9 [ where c = temperature in celsius and f = temperature in fahrenheit ] " 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 6, 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "name": "stdout", 173 | "output_type": "stream", 174 | "text": [ 175 | "Input the temperature you like to convert? (e.g., 45F, 102C, etc.) : 70f\n", 176 | "The temperature in Celsius is 21 degrees.\n" 177 | ] 178 | } 179 | ], 180 | "source": [ 181 | "# Your answer goes here\n" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": {}, 187 | "source": [ 188 | "# Exercise 6\n", 189 | "Convert the following for loop to a one-liner using list comprehension:" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 1, 195 | "metadata": {}, 196 | "outputs": [], 197 | "source": [ 198 | "numbers = [5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5]" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 2, 204 | "metadata": {}, 205 | "outputs": [ 206 | { 207 | "data": { 208 | "text/plain": [ 209 | "[25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25]" 210 | ] 211 | }, 212 | "execution_count": 2, 213 | "metadata": {}, 214 | "output_type": "execute_result" 215 | } 216 | ], 217 | "source": [ 218 | "L = []\n", 219 | "for n in numbers:\n", 220 | " L.append(n ** 2)\n", 221 | "L" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 3, 227 | "metadata": {}, 228 | "outputs": [ 229 | { 230 | "data": { 231 | "text/plain": [ 232 | "[25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25]" 233 | ] 234 | }, 235 | "execution_count": 3, 236 | "metadata": {}, 237 | "output_type": "execute_result" 238 | } 239 | ], 240 | "source": [ 241 | "# Your answer goes here\n" 242 | ] 243 | }, 244 | { 245 | "cell_type": "markdown", 246 | "metadata": {}, 247 | "source": [ 248 | "# Exercise 7\n", 249 | "Convert the following for loop to a one-liner using list comprehension:" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": 4, 255 | "metadata": {}, 256 | "outputs": [], 257 | "source": [ 258 | "numbers = [5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5]" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 5, 264 | "metadata": {}, 265 | "outputs": [ 266 | { 267 | "data": { 268 | "text/plain": [ 269 | "[0, 1, 4, 9, 16, 25]" 270 | ] 271 | }, 272 | "execution_count": 5, 273 | "metadata": {}, 274 | "output_type": "execute_result" 275 | } 276 | ], 277 | "source": [ 278 | "L = []\n", 279 | "for val in numbers:\n", 280 | " if val <= 0:\n", 281 | " L.append(val ** 2)\n", 282 | "L" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": 6, 288 | "metadata": {}, 289 | "outputs": [ 290 | { 291 | "data": { 292 | "text/plain": [ 293 | "[0, 1, 4, 9, 16, 25]" 294 | ] 295 | }, 296 | "execution_count": 6, 297 | "metadata": {}, 298 | "output_type": "execute_result" 299 | } 300 | ], 301 | "source": [ 302 | "# Your answer goes here\n" 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "metadata": {}, 308 | "source": [ 309 | "# Exercise 8\n", 310 | "Using a list comprehension, create a new list called `newlist` out of the list `numbers`, which contains only the positive numbers from the list, as integers." 311 | ] 312 | }, 313 | { 314 | "cell_type": "code", 315 | "execution_count": 7, 316 | "metadata": {}, 317 | "outputs": [], 318 | "source": [ 319 | "numbers = [4.2, -0.01, 1.1, -1.5, -2.2, 5.4, -3.5, 3.7, 2.8, -4.3, -5]" 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": 8, 325 | "metadata": {}, 326 | "outputs": [ 327 | { 328 | "data": { 329 | "text/plain": [ 330 | "[4, 1, 5, 3, 2]" 331 | ] 332 | }, 333 | "execution_count": 8, 334 | "metadata": {}, 335 | "output_type": "execute_result" 336 | } 337 | ], 338 | "source": [ 339 | "# Your answer goes here\n" 340 | ] 341 | } 342 | ], 343 | "metadata": { 344 | "kernelspec": { 345 | "display_name": "Python 3", 346 | "language": "python", 347 | "name": "python3" 348 | }, 349 | "language_info": { 350 | "codemirror_mode": { 351 | "name": "ipython", 352 | "version": 3 353 | }, 354 | "file_extension": ".py", 355 | "mimetype": "text/x-python", 356 | "name": "python", 357 | "nbconvert_exporter": "python", 358 | "pygments_lexer": "ipython3", 359 | "version": "3.7.2" 360 | } 361 | }, 362 | "nbformat": 4, 363 | "nbformat_minor": 2 364 | } 365 | -------------------------------------------------------------------------------- /00-Python-Basics/Exercise-04-Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise 1\n", 8 | "Write a function that sums all the numbers in a list and name it `list_sum`." 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "metadata": {}, 15 | "outputs": [], 16 | "source": [ 17 | "# Your answer goes here\n" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 2, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "L = [8, 2, 13, 0, 7]\n", 27 | "list_sum(L)" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": {}, 33 | "source": [ 34 | "# Exercise 2\n", 35 | "Write a function to reverse a string." 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 3, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "# Your answer goes here\n" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 4, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "s = \"Hello World!\"\n", 54 | "str_reverse(s)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "# Exercise 3\n", 62 | "Write a function that takes a list and returns a new list with unique elements of the first list." 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 5, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "# Your answer goes here\n" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 6, 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "[0, 'dog', 4, 'cat']" 83 | ] 84 | }, 85 | "execution_count": 6, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "L = ['dog',0,4,'cat','dog',0,'dog','cat',4,0]\n", 92 | "unique_elements(L)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "# Exercise 4\n", 100 | "Write a function to print all the even numbers from a given list." 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 7, 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [ 109 | "# Your answer goes here\n" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 8, 115 | "metadata": {}, 116 | "outputs": [ 117 | { 118 | "data": { 119 | "text/plain": [ 120 | "[2, 4, 6, 8, 10]" 121 | ] 122 | }, 123 | "execution_count": 8, 124 | "metadata": {}, 125 | "output_type": "execute_result" 126 | } 127 | ], 128 | "source": [ 129 | "L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", 130 | "is_even_num(L)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "# Exercise 5\n", 138 | "Write a function that takes a number as a parameter and checks the number is prime or not.\n", 139 | "\n", 140 | "Note : A prime number is a natural number greater than 1 and that has no positive divisors other than 1 and itself." 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 9, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [ 149 | "# Your answer goes here\n" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 10, 155 | "metadata": {}, 156 | "outputs": [ 157 | { 158 | "data": { 159 | "text/plain": [ 160 | "False" 161 | ] 162 | }, 163 | "execution_count": 10, 164 | "metadata": {}, 165 | "output_type": "execute_result" 166 | } 167 | ], 168 | "source": [ 169 | "test_prime(9)" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 11, 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "data": { 179 | "text/plain": [ 180 | "True" 181 | ] 182 | }, 183 | "execution_count": 11, 184 | "metadata": {}, 185 | "output_type": "execute_result" 186 | } 187 | ], 188 | "source": [ 189 | "test_prime(15487271)" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": {}, 195 | "source": [ 196 | "# Exercise 6\n", 197 | "Write a function that takes an unknown number of parameters, converts them to string (if they are not), capitalizes them and prints one string separated by a space `\" \"`. " 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": 12, 203 | "metadata": {}, 204 | "outputs": [], 205 | "source": [ 206 | "# Your answer goes here\n" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 13, 212 | "metadata": {}, 213 | "outputs": [ 214 | { 215 | "name": "stdout", 216 | "output_type": "stream", 217 | "text": [ 218 | "2 Cats Are Sleeping Under The Blanket! " 219 | ] 220 | } 221 | ], 222 | "source": [ 223 | "print_unknown(2, 'cats', 'are', 'sleeping', 'under', 'the', 'blanket!')" 224 | ] 225 | }, 226 | { 227 | "cell_type": "markdown", 228 | "metadata": {}, 229 | "source": [ 230 | "# Exercise 7\n", 231 | "Use lambda expressions and `filter()` function to filter words from a list that don't start with the letter 's'. For example:\n", 232 | "\n", 233 | " seq = ['soup','dog','salad','cat','great']\n", 234 | "\n", 235 | "**should be filtered down to:**\n", 236 | "\n", 237 | " ['soup','salad']" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 14, 243 | "metadata": {}, 244 | "outputs": [], 245 | "source": [ 246 | "seq = ['soup','dog','salad','cat','great']" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 15, 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "data": { 256 | "text/plain": [ 257 | "['soup', 'salad']" 258 | ] 259 | }, 260 | "execution_count": 15, 261 | "metadata": {}, 262 | "output_type": "execute_result" 263 | } 264 | ], 265 | "source": [ 266 | "# Your answer goes here\n" 267 | ] 268 | } 269 | ], 270 | "metadata": { 271 | "kernelspec": { 272 | "display_name": "Python 3", 273 | "language": "python", 274 | "name": "python3" 275 | }, 276 | "language_info": { 277 | "codemirror_mode": { 278 | "name": "ipython", 279 | "version": 3 280 | }, 281 | "file_extension": ".py", 282 | "mimetype": "text/x-python", 283 | "name": "python", 284 | "nbconvert_exporter": "python", 285 | "pygments_lexer": "ipython3", 286 | "version": "3.7.2" 287 | } 288 | }, 289 | "nbformat": 4, 290 | "nbformat_minor": 2 291 | } 292 | -------------------------------------------------------------------------------- /00-Python-Basics/Exercise-05-Errors-and-Debugging.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise\n", 8 | "Write a program that asks for users input and if the input is not a valid integer repeats the question until a valid input is provided. Then print the integer input.\n", 9 | "\n", 10 | "Hint: You can use a format similar to `x = int(input(\"Please enter a number: \"))`. `int()` function will try to convert the user input (which is a string by default, i.e., even input 1 will be proceeded as '1') to an int. See what kind of error you get by providing bad inputs and use that for your exception (`except`) argument.\n", 11 | "\n", 12 | "For instance in the following cell I have provided 3.14 which results in a \"ValueError\" error:" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 1, 18 | "metadata": {}, 19 | "outputs": [ 20 | { 21 | "name": "stdin", 22 | "output_type": "stream", 23 | "text": [ 24 | "Please enter a number: 3.14\n" 25 | ] 26 | }, 27 | { 28 | "ename": "ValueError", 29 | "evalue": "invalid literal for int() with base 10: '3.14'", 30 | "output_type": "error", 31 | "traceback": [ 32 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 33 | "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", 34 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Please enter a number: \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 35 | "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: '3.14'" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "x = int(input(\"Please enter a number: \"))" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "Use a while loop similar to the following:\n", 48 | "\n", 49 | "```python\n", 50 | "while True:\n", 51 | " try:\n", 52 | " # your code here\n", 53 | " break\n", 54 | " except ValueError:\n", 55 | " # your code here\n", 56 | "\n", 57 | "print(\"x:\", x)\n", 58 | "```" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 1, 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "name": "stdin", 68 | "output_type": "stream", 69 | "text": [ 70 | "Please enter a number: 3.14\n" 71 | ] 72 | }, 73 | { 74 | "name": "stdout", 75 | "output_type": "stream", 76 | "text": [ 77 | "Oops! That was not a valid number. Try again...\n" 78 | ] 79 | }, 80 | { 81 | "name": "stdin", 82 | "output_type": "stream", 83 | "text": [ 84 | "Please enter a number: Hello\n" 85 | ] 86 | }, 87 | { 88 | "name": "stdout", 89 | "output_type": "stream", 90 | "text": [ 91 | "Oops! That was not a valid number. Try again...\n" 92 | ] 93 | }, 94 | { 95 | "name": "stdin", 96 | "output_type": "stream", 97 | "text": [ 98 | "Please enter a number: 20\n" 99 | ] 100 | }, 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "x: 20\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "# Your answer goes here\n" 111 | ] 112 | } 113 | ], 114 | "metadata": { 115 | "kernelspec": { 116 | "display_name": "Python 3", 117 | "language": "python", 118 | "name": "python3" 119 | }, 120 | "language_info": { 121 | "codemirror_mode": { 122 | "name": "ipython", 123 | "version": 3 124 | }, 125 | "file_extension": ".py", 126 | "mimetype": "text/x-python", 127 | "name": "python", 128 | "nbconvert_exporter": "python", 129 | "pygments_lexer": "ipython3", 130 | "version": "3.7.2" 131 | } 132 | }, 133 | "nbformat": 4, 134 | "nbformat_minor": 2 135 | } 136 | -------------------------------------------------------------------------------- /00-Python-Basics/Exercise-06-Iterators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise 1\n", 8 | "Write a function that takes a list and prints a corresponding enumerate object of this list, i.e., index and value of each element. You can do this by using the function `enumerate(L)`.\n", 9 | "\n", 10 | "Make sure to include a docstring for this function. Name this function `print_enumerate`." 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "L = [4, 'A', 6, 0, 'Z']" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "# Your answer goes here\n" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 3, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "0 4\n", 41 | "1 A\n", 42 | "2 6\n", 43 | "3 0\n", 44 | "4 Z\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "print_enumerate(L)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "# Exercise 2\n", 57 | "Write a function that accepts a list of words and returns a list with only the ones that are capitalized. \n", 58 | "\n", 59 | "Use `filter` in conjunction with a for loop. You can use `isupper()` to identify whether a single letter is capitalized or not.\n", 60 | "\n", 61 | "Include a docstring." 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 4, 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [ 70 | "R = ['computers', 'month', 'English', 'digital', 'Questrom']" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 5, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "# Your answer goes here\n" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 6, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "data": { 89 | "text/plain": [ 90 | "['English', 'Questrom']" 91 | ] 92 | }, 93 | "execution_count": 6, 94 | "metadata": {}, 95 | "output_type": "execute_result" 96 | } 97 | ], 98 | "source": [ 99 | "filter_capitalized(R)" 100 | ] 101 | } 102 | ], 103 | "metadata": { 104 | "kernelspec": { 105 | "display_name": "Python 3", 106 | "language": "python", 107 | "name": "python3" 108 | }, 109 | "language_info": { 110 | "codemirror_mode": { 111 | "name": "ipython", 112 | "version": 3 113 | }, 114 | "file_extension": ".py", 115 | "mimetype": "text/x-python", 116 | "name": "python", 117 | "nbconvert_exporter": "python", 118 | "pygments_lexer": "ipython3", 119 | "version": "3.7.2" 120 | } 121 | }, 122 | "nbformat": 4, 123 | "nbformat_minor": 2 124 | } 125 | -------------------------------------------------------------------------------- /00-Python-Basics/Exercise-07-Generators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise 1\n", 8 | "Using generator expression create a generator that generates values that are multiples of 3, then use this generator in a list comprehension to subset a list that gives the elements with indices that are multiples of 3." 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "metadata": {}, 15 | "outputs": [], 16 | "source": [ 17 | "L = [3, 37, 72, 3, 75, 44, 3, 8, 59, 3, 50, 15, 3, 10, 29, 3, 98, 39, 3, 7, 4, 3]" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 2, 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "data": { 27 | "text/plain": [ 28 | "[3, 3, 3, 3, 3, 3, 3, 3]" 29 | ] 30 | }, 31 | "execution_count": 2, 32 | "metadata": {}, 33 | "output_type": "execute_result" 34 | } 35 | ], 36 | "source": [ 37 | "# Your answer goes here\n" 38 | ] 39 | } 40 | ], 41 | "metadata": { 42 | "kernelspec": { 43 | "display_name": "Python 3", 44 | "language": "python", 45 | "name": "python3" 46 | }, 47 | "language_info": { 48 | "codemirror_mode": { 49 | "name": "ipython", 50 | "version": 3 51 | }, 52 | "file_extension": ".py", 53 | "mimetype": "text/x-python", 54 | "name": "python", 55 | "nbconvert_exporter": "python", 56 | "pygments_lexer": "ipython3", 57 | "version": "3.7.2" 58 | } 59 | }, 60 | "nbformat": 4, 61 | "nbformat_minor": 2 62 | } 63 | -------------------------------------------------------------------------------- /00-Python-Basics/Exercise-08-Modules-and-Packages.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise 1\n", 8 | "From `math` package import function `ceil()`. `ceil(x)` is equivalent of ceiling function in mathematics where it gives the closest integer bigger than x." 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 8, 14 | "metadata": {}, 15 | "outputs": [], 16 | "source": [ 17 | "# Your answer goes here\n" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 10, 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "data": { 27 | "text/plain": [ 28 | "6" 29 | ] 30 | }, 31 | "execution_count": 10, 32 | "metadata": {}, 33 | "output_type": "execute_result" 34 | } 35 | ], 36 | "source": [ 37 | "ceil(5.1)" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 12, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "data": { 47 | "text/plain": [ 48 | "-1" 49 | ] 50 | }, 51 | "execution_count": 12, 52 | "metadata": {}, 53 | "output_type": "execute_result" 54 | } 55 | ], 56 | "source": [ 57 | "ceil(-1.5)" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "Note: If you are getting an error in evaluating the above expressions it could be because you haven't explicitly imported the module content." 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "# Exercise 2\n", 72 | "Import package `peewee`." 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 17, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "ename": "ModuleNotFoundError", 82 | "evalue": "No module named 'peewee'", 83 | "output_type": "error", 84 | "traceback": [ 85 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 86 | "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", 87 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Your answer goes here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mpeewee\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 88 | "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'peewee'" 89 | ] 90 | } 91 | ], 92 | "source": [ 93 | "# Your answer goes here\n" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "You will most likely get an error indicating that this packages does not exist. Try to install it by\n", 101 | "\n", 102 | "`conda install peewee` or `pip install peewee`. Note that these commands are for command line.\n", 103 | "\n", 104 | "Hint: You can use a `!` prior to any command in a Jupyter cell to make that line a command-line command." 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 16, 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "name": "stdout", 114 | "output_type": "stream", 115 | "text": [ 116 | "Solving environment: failed\n", 117 | "\n", 118 | "PackagesNotFoundError: The following packages are not available from current channels:\n", 119 | "\n", 120 | " - peewee\n", 121 | "\n", 122 | "Current channels:\n", 123 | "\n", 124 | " - https://repo.anaconda.com/pkgs/main/osx-64\n", 125 | " - https://repo.anaconda.com/pkgs/main/noarch\n", 126 | " - https://repo.anaconda.com/pkgs/free/osx-64\n", 127 | " - https://repo.anaconda.com/pkgs/free/noarch\n", 128 | " - https://repo.anaconda.com/pkgs/r/osx-64\n", 129 | " - https://repo.anaconda.com/pkgs/r/noarch\n", 130 | " - https://repo.anaconda.com/pkgs/pro/osx-64\n", 131 | " - https://repo.anaconda.com/pkgs/pro/noarch\n", 132 | "\n", 133 | "To search for alternate channels that may provide the conda package you're\n", 134 | "looking for, navigate to\n", 135 | "\n", 136 | " https://anaconda.org\n", 137 | "\n", 138 | "and use the search bar at the top of the page.\n", 139 | "\n", 140 | "\n" 141 | ] 142 | } 143 | ], 144 | "source": [ 145 | "# Your answer goes here\n" 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": {}, 151 | "source": [ 152 | "After a successful installation you should be able to import it." 153 | ] 154 | } 155 | ], 156 | "metadata": { 157 | "kernelspec": { 158 | "display_name": "Python 3", 159 | "language": "python", 160 | "name": "python3" 161 | }, 162 | "language_info": { 163 | "codemirror_mode": { 164 | "name": "ipython", 165 | "version": 3 166 | }, 167 | "file_extension": ".py", 168 | "mimetype": "text/x-python", 169 | "name": "python", 170 | "nbconvert_exporter": "python", 171 | "pygments_lexer": "ipython3", 172 | "version": "3.7.2" 173 | } 174 | }, 175 | "nbformat": 4, 176 | "nbformat_minor": 2 177 | } 178 | -------------------------------------------------------------------------------- /00-Python-Basics/Exercise-09-Strings-and-Regular-Expressions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise 1\n", 8 | "Given the current date and dictionary below format the following string to produce custom message:\n", 9 | "\n", 10 | "Hello xxx xxx. As of xxx, your current balance is $xxx." 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "import datetime\n", 20 | "now = datetime.datetime.now()\n", 21 | "current_date = now.strftime(\"%Y-%m-%d\")\n", 22 | "\n", 23 | "d = {\"first\":\"John\", \"last\":\"Smith\", \"balance\":120.55}" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "Hello John Smith. As of 2019-02-03, your current balance is $120.55.\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "# Your answer goes here\n" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "# Exercise 2\n", 48 | "Using `center()`, can you produce a similar output?" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 3, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "A = 'This is a centered text'\n", 58 | "B = 'x = 60'\n", 59 | "C = 'E = m c ^ 2'" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 4, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | " This is a centered text \n", 72 | " x = 60 \n", 73 | " E = m c ^ 2 \n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "# Your answer goes here\n" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "# Exercise 3\n", 86 | "In the following string replace \"skills\" with \"experience\"." 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 5, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "text = 'Put your skills to use!'" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 6, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "'Put your experience to use!'" 107 | ] 108 | }, 109 | "execution_count": 6, 110 | "metadata": {}, 111 | "output_type": "execute_result" 112 | } 113 | ], 114 | "source": [ 115 | "# Your answer goes here\n" 116 | ] 117 | } 118 | ], 119 | "metadata": { 120 | "kernelspec": { 121 | "display_name": "Python 3", 122 | "language": "python", 123 | "name": "python3" 124 | }, 125 | "language_info": { 126 | "codemirror_mode": { 127 | "name": "ipython", 128 | "version": 3 129 | }, 130 | "file_extension": ".py", 131 | "mimetype": "text/x-python", 132 | "name": "python", 133 | "nbconvert_exporter": "python", 134 | "pygments_lexer": "ipython3", 135 | "version": "3.7.2" 136 | } 137 | }, 138 | "nbformat": 4, 139 | "nbformat_minor": 2 140 | } 141 | -------------------------------------------------------------------------------- /03-Magic-Commands.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# IPython Magic Commands" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Here we'll begin discussing some of the enhancements that IPython adds on top of the normal Python syntax.\n", 15 | "These are known in IPython as *magic commands*, and are prefixed by the ``%`` character.\n", 16 | "These magic commands are designed to succinctly solve various common problems in standard data analysis.\n", 17 | "Magic commands come in two flavors: *line magics*, which are denoted by a single ``%`` prefix and operate on a single line of input, and *cell magics*, which are denoted by a double ``%%`` prefix and operate on multiple lines of input.\n", 18 | "We'll demonstrate and discuss a few brief examples here, and come back to more focused discussion of several useful magic commands later in the chapter." 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "## Running External Code: ``%run``\n", 26 | "As you begin developing more extensive code, you will likely find yourself working in both IPython for interactive exploration, as well as a text editor to store code that you want to reuse.\n", 27 | "Rather than running this code in a new window, it can be convenient to run it within your IPython session.\n", 28 | "This can be done with the ``%run`` magic.\n", 29 | "\n", 30 | "For example, imagine you've created a ``myscript.py`` file with the following contents:\n", 31 | "\n", 32 | "```python\n", 33 | "#-------------------------------------\n", 34 | "# file: myscript.py\n", 35 | "\n", 36 | "def square(x):\n", 37 | " \"\"\"square a number\"\"\"\n", 38 | " return x ** 2\n", 39 | "\n", 40 | "for N in range(1, 4):\n", 41 | " print(N, \"squared is\", square(N))\n", 42 | "```\n", 43 | "\n", 44 | "You can execute this from your IPython session as follows:\n", 45 | "\n", 46 | "```ipython\n", 47 | "In [6]: %run myscript.py\n", 48 | "1 squared is 1\n", 49 | "2 squared is 4\n", 50 | "3 squared is 9\n", 51 | "```\n", 52 | "\n", 53 | "Note also that after you've run this script, any functions defined within it are available for use in your IPython session:\n", 54 | "\n", 55 | "```ipython\n", 56 | "In [7]: square(5)\n", 57 | "Out[7]: 25\n", 58 | "```\n", 59 | "\n", 60 | "There are several options to fine-tune how your code is run; you can see the documentation in the normal way, by typing **``%run?``** in the IPython interpreter." 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "## Timing Code Execution: ``%timeit``\n", 68 | "Another example of a useful magic function is ``%timeit``, which will automatically determine the execution time of the single-line Python statement that follows it.\n", 69 | "For example, we may want to check the performance of a list comprehension:\n", 70 | "\n", 71 | "```ipython\n", 72 | "In [8]: %timeit L = [n ** 2 for n in range(1000)]\n", 73 | "1000 loops, best of 3: 325 µs per loop\n", 74 | "```\n", 75 | "\n", 76 | "The benefit of ``%timeit`` is that for short commands it will automatically perform multiple runs in order to attain more robust results.\n", 77 | "For multi line statements, adding a second ``%`` sign will turn this into a cell magic that can handle multiple lines of input.\n", 78 | "For example, here's the equivalent construction with a ``for``-loop:\n", 79 | "\n", 80 | "```ipython\n", 81 | "In [9]: %%timeit\n", 82 | " ...: L = []\n", 83 | " ...: for n in range(1000):\n", 84 | " ...: L.append(n ** 2)\n", 85 | " ...: \n", 86 | "1000 loops, best of 3: 373 µs per loop\n", 87 | "```\n", 88 | "\n", 89 | "We can immediately see that list comprehensions are about 10% faster than the equivalent ``for``-loop construction in this case." 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "## Help on Magic Functions: ``?``, ``%magic``, and ``%lsmagic``\n", 97 | "\n", 98 | "Like normal Python functions, IPython magic functions have docstrings, and this useful\n", 99 | "documentation can be accessed in the standard manner.\n", 100 | "So, for example, to read the documentation of the ``%timeit`` magic simply type this:\n", 101 | "\n", 102 | "```ipython\n", 103 | "In [10]: %timeit?\n", 104 | "```\n", 105 | "\n", 106 | "Documentation for other functions can be accessed similarly.\n", 107 | "To access a general description of available magic functions, including some examples, you can type this:\n", 108 | "\n", 109 | "```ipython\n", 110 | "In [11]: %magic\n", 111 | "```\n", 112 | "\n", 113 | "For a quick and simple list of all available magic functions, type this:\n", 114 | "\n", 115 | "```ipython\n", 116 | "In [12]: %lsmagic\n", 117 | "```" 118 | ] 119 | } 120 | ], 121 | "metadata": { 122 | "kernelspec": { 123 | "display_name": "Python 3", 124 | "language": "python", 125 | "name": "python3" 126 | }, 127 | "language_info": { 128 | "codemirror_mode": { 129 | "name": "ipython", 130 | "version": 3 131 | }, 132 | "file_extension": ".py", 133 | "mimetype": "text/x-python", 134 | "name": "python", 135 | "nbconvert_exporter": "python", 136 | "pygments_lexer": "ipython3", 137 | "version": "3.7.2" 138 | } 139 | }, 140 | "nbformat": 4, 141 | "nbformat_minor": 2 142 | } 143 | -------------------------------------------------------------------------------- /Archive/DL-in-Py-1st-Ed/25-Introduction-to-ConvNets.ipynb: -------------------------------------------------------------------------------- 1 | {"nbformat":4,"nbformat_minor":0,"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.5.2"},"colab":{"name":"25-Introduction-to-ConvNets.ipynb","provenance":[],"collapsed_sections":[]},"accelerator":"GPU"},"cells":[{"cell_type":"code","metadata":{"id":"jSUZ8YPZUoPr","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":53},"executionInfo":{"status":"ok","timestamp":1595367719484,"user_tz":240,"elapsed":3374,"user":{"displayName":"Mohammad Soltanieh Ha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjHn5A5nCXb4FsIfIBdin6XTbj6u54lww1hF6ECkT8=s64","userId":"12308918870841825745"}},"outputId":"858105d0-eefd-4d8b-a8ea-965ae83b93fc"},"source":["import keras\n","keras.__version__"],"execution_count":1,"outputs":[{"output_type":"stream","text":["Using TensorFlow backend.\n"],"name":"stderr"},{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic":{"type":"string"},"text/plain":["'2.3.1'"]},"metadata":{"tags":[]},"execution_count":1}]},{"cell_type":"code","metadata":{"id":"Mc1TXD14Uzo0","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":35},"executionInfo":{"status":"ok","timestamp":1595367719777,"user_tz":240,"elapsed":3514,"user":{"displayName":"Mohammad Soltanieh Ha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjHn5A5nCXb4FsIfIBdin6XTbj6u54lww1hF6ECkT8=s64","userId":"12308918870841825745"}},"outputId":"54c46a5f-c391-4f4a-84a2-26ca5808b859"},"source":["import tensorflow as tf\n","print(\"GPU Available: \", tf.config.list_physical_devices('GPU'))"],"execution_count":2,"outputs":[{"output_type":"stream","text":["GPU Available: [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"collapsed":true,"id":"_uiKfbLIUoPy","colab_type":"text"},"source":["# 5.1 - Introduction to convnets\n","\n","This notebook contains the code sample found in Chapter 5, Section 1 of [Deep Learning with Python](https://www.manning.com/books/deep-learning-with-python?a_aid=keras&a_bid=76564dff). Note that the original text features far more content, in particular further explanations and figures: in this notebook, you will only find source code and related comments.\n","\n","----\n","\n","First, let's take a practical look at a very simple convnet example. We will use our convnet to classify MNIST digits, a task that you've already been \n","through in Chapter 2, using a densely-connected network (our test accuracy then was 97.8%). Even though our convnet will be very basic, its \n","accuracy will still blow out of the water that of the densely-connected model from Chapter 2.\n","\n","The 6 lines of code below show you what a basic convnet looks like. It's a stack of `Conv2D` and `MaxPooling2D` layers. We'll see in a \n","minute what they do concretely.\n","Importantly, a convnet takes as input tensors of shape `(image_height, image_width, image_channels)` (not including the batch dimension). \n","In our case, we will configure our convnet to process inputs of size `(28, 28, 1)`, which is the format of MNIST images. We do this via \n","passing the argument `input_shape=(28, 28, 1)` to our first layer."]},{"cell_type":"code","metadata":{"id":"byZkME1qUoPz","colab_type":"code","colab":{},"executionInfo":{"status":"ok","timestamp":1595367817697,"user_tz":240,"elapsed":6367,"user":{"displayName":"Mohammad Soltanieh Ha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjHn5A5nCXb4FsIfIBdin6XTbj6u54lww1hF6ECkT8=s64","userId":"12308918870841825745"}}},"source":["from keras import layers\n","from keras import models\n","\n","model = models.Sequential()\n","model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))\n","model.add(layers.MaxPooling2D((2, 2)))\n","model.add(layers.Conv2D(64, (3, 3), activation='relu'))\n","model.add(layers.MaxPooling2D((2, 2)))\n","model.add(layers.Conv2D(64, (3, 3), activation='relu'))"],"execution_count":3,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"CHV8R6vLUoP2","colab_type":"text"},"source":["Let's display the architecture of our convnet so far:"]},{"cell_type":"code","metadata":{"id":"2S9DWUvWUoP3","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":328},"executionInfo":{"status":"ok","timestamp":1595367819678,"user_tz":240,"elapsed":359,"user":{"displayName":"Mohammad Soltanieh Ha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjHn5A5nCXb4FsIfIBdin6XTbj6u54lww1hF6ECkT8=s64","userId":"12308918870841825745"}},"outputId":"467dfc04-6aac-4d5d-d2e6-ba749c66839c"},"source":["model.summary()"],"execution_count":4,"outputs":[{"output_type":"stream","text":["Model: \"sequential_1\"\n","_________________________________________________________________\n","Layer (type) Output Shape Param # \n","=================================================================\n","conv2d_1 (Conv2D) (None, 26, 26, 32) 320 \n","_________________________________________________________________\n","max_pooling2d_1 (MaxPooling2 (None, 13, 13, 32) 0 \n","_________________________________________________________________\n","conv2d_2 (Conv2D) (None, 11, 11, 64) 18496 \n","_________________________________________________________________\n","max_pooling2d_2 (MaxPooling2 (None, 5, 5, 64) 0 \n","_________________________________________________________________\n","conv2d_3 (Conv2D) (None, 3, 3, 64) 36928 \n","=================================================================\n","Total params: 55,744\n","Trainable params: 55,744\n","Non-trainable params: 0\n","_________________________________________________________________\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"collapsed":true,"id":"TjuScTiBUoP6","colab_type":"text"},"source":["You can see above that the output of every `Conv2D` and `MaxPooling2D` layer is a 3D tensor of shape `(height, width, channels)`. The width \n","and height dimensions tend to shrink as we go deeper in the network. The number of channels is controlled by the first argument passed to \n","the `Conv2D` layers (e.g. 32 or 64).\n","\n","The next step would be to feed our last output tensor (of shape `(3, 3, 64)`) into a densely-connected classifier network like those you are \n","already familiar with: a stack of `Dense` layers. These classifiers process vectors, which are 1D, whereas our current output is a 3D tensor. \n","So first, we will have to flatten our 3D outputs to 1D, and then add a few `Dense` layers on top:"]},{"cell_type":"code","metadata":{"id":"VtQ3IND2UoP7","colab_type":"code","colab":{},"executionInfo":{"status":"ok","timestamp":1595367977412,"user_tz":240,"elapsed":336,"user":{"displayName":"Mohammad Soltanieh Ha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjHn5A5nCXb4FsIfIBdin6XTbj6u54lww1hF6ECkT8=s64","userId":"12308918870841825745"}}},"source":["model.add(layers.Flatten())\n","model.add(layers.Dense(64, activation='relu'))\n","model.add(layers.Dense(10, activation='softmax'))"],"execution_count":5,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"L248vrcUUoP-","colab_type":"text"},"source":["We are going to do 10-way classification, so we use a final layer with 10 outputs and a softmax activation. Now here's what our network \n","looks like:"]},{"cell_type":"code","metadata":{"id":"1UkE1iiIUoP_","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":432},"executionInfo":{"status":"ok","timestamp":1595367981061,"user_tz":240,"elapsed":372,"user":{"displayName":"Mohammad Soltanieh Ha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjHn5A5nCXb4FsIfIBdin6XTbj6u54lww1hF6ECkT8=s64","userId":"12308918870841825745"}},"outputId":"21e3d62a-cadf-4099-8247-906ec87f5c7d"},"source":["model.summary()"],"execution_count":6,"outputs":[{"output_type":"stream","text":["Model: \"sequential_1\"\n","_________________________________________________________________\n","Layer (type) Output Shape Param # \n","=================================================================\n","conv2d_1 (Conv2D) (None, 26, 26, 32) 320 \n","_________________________________________________________________\n","max_pooling2d_1 (MaxPooling2 (None, 13, 13, 32) 0 \n","_________________________________________________________________\n","conv2d_2 (Conv2D) (None, 11, 11, 64) 18496 \n","_________________________________________________________________\n","max_pooling2d_2 (MaxPooling2 (None, 5, 5, 64) 0 \n","_________________________________________________________________\n","conv2d_3 (Conv2D) (None, 3, 3, 64) 36928 \n","_________________________________________________________________\n","flatten_1 (Flatten) (None, 576) 0 \n","_________________________________________________________________\n","dense_1 (Dense) (None, 64) 36928 \n","_________________________________________________________________\n","dense_2 (Dense) (None, 10) 650 \n","=================================================================\n","Total params: 93,322\n","Trainable params: 93,322\n","Non-trainable params: 0\n","_________________________________________________________________\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"_uzOzQQwUoQC","colab_type":"text"},"source":["As you can see, our `(3, 3, 64)` outputs were flattened into vectors of shape `(576,)`, before going through two `Dense` layers.\n","\n","Now, let's train our convnet on the MNIST digits. We will reuse a lot of the code we have already covered in the MNIST example from Chapter \n","2."]},{"cell_type":"code","metadata":{"id":"OZpm6_FtUoQD","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":52},"executionInfo":{"status":"ok","timestamp":1595368011264,"user_tz":240,"elapsed":976,"user":{"displayName":"Mohammad Soltanieh Ha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjHn5A5nCXb4FsIfIBdin6XTbj6u54lww1hF6ECkT8=s64","userId":"12308918870841825745"}},"outputId":"22e1b9b1-7211-4cba-954c-7fd6812ffeeb"},"source":["from keras.datasets import mnist\n","from keras.utils import to_categorical\n","\n","(train_images, train_labels), (test_images, test_labels) = mnist.load_data()\n","\n","train_images = train_images.reshape((60000, 28, 28, 1))\n","train_images = train_images.astype('float32') / 255\n","\n","test_images = test_images.reshape((10000, 28, 28, 1))\n","test_images = test_images.astype('float32') / 255\n","\n","train_labels = to_categorical(train_labels)\n","test_labels = to_categorical(test_labels)"],"execution_count":7,"outputs":[{"output_type":"stream","text":["Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz\n","11493376/11490434 [==============================] - 0s 0us/step\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"qrnuZ0onUoQG","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":207},"executionInfo":{"status":"ok","timestamp":1595368063768,"user_tz":240,"elapsed":44591,"user":{"displayName":"Mohammad Soltanieh Ha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjHn5A5nCXb4FsIfIBdin6XTbj6u54lww1hF6ECkT8=s64","userId":"12308918870841825745"}},"outputId":"967fdce2-caef-40cd-cd6b-f1b1b01a2f42"},"source":["model.compile(optimizer='rmsprop',\n"," loss='categorical_crossentropy',\n"," metrics=['accuracy'])\n","model.fit(train_images, train_labels, epochs=5, batch_size=64)"],"execution_count":8,"outputs":[{"output_type":"stream","text":["Epoch 1/5\n","60000/60000 [==============================] - 14s 229us/step - loss: 0.1724 - accuracy: 0.9458\n","Epoch 2/5\n","60000/60000 [==============================] - 7s 125us/step - loss: 0.0465 - accuracy: 0.9858\n","Epoch 3/5\n","60000/60000 [==============================] - 7s 125us/step - loss: 0.0326 - accuracy: 0.9902\n","Epoch 4/5\n","60000/60000 [==============================] - 7s 125us/step - loss: 0.0256 - accuracy: 0.9922\n","Epoch 5/5\n","60000/60000 [==============================] - 7s 125us/step - loss: 0.0197 - accuracy: 0.9941\n"],"name":"stdout"},{"output_type":"execute_result","data":{"text/plain":[""]},"metadata":{"tags":[]},"execution_count":8}]},{"cell_type":"markdown","metadata":{"id":"UhFZ8L_gUoQK","colab_type":"text"},"source":["Let's evaluate the model on the test data:"]},{"cell_type":"code","metadata":{"id":"j_TVa8pbUoQK","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":35},"executionInfo":{"status":"ok","timestamp":1595368064848,"user_tz":240,"elapsed":39124,"user":{"displayName":"Mohammad Soltanieh Ha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjHn5A5nCXb4FsIfIBdin6XTbj6u54lww1hF6ECkT8=s64","userId":"12308918870841825745"}},"outputId":"6e5986cc-4aa7-480f-e7c4-03169f4c59e2"},"source":["test_loss, test_acc = model.evaluate(test_images, test_labels)"],"execution_count":9,"outputs":[{"output_type":"stream","text":["10000/10000 [==============================] - 1s 106us/step\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"uLCTbgp9UoQO","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":35},"executionInfo":{"status":"ok","timestamp":1595368064849,"user_tz":240,"elapsed":38238,"user":{"displayName":"Mohammad Soltanieh Ha","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjHn5A5nCXb4FsIfIBdin6XTbj6u54lww1hF6ECkT8=s64","userId":"12308918870841825745"}},"outputId":"a0dd1e28-a30d-4f5a-aacd-c504d1bfb773"},"source":["test_acc"],"execution_count":10,"outputs":[{"output_type":"execute_result","data":{"text/plain":["0.9908999800682068"]},"metadata":{"tags":[]},"execution_count":10}]},{"cell_type":"markdown","metadata":{"id":"J65ARXZgUoQR","colab_type":"text"},"source":["While our densely-connected network from Chapter 2 had a test accuracy of 97.8%, our basic convnet has a test accuracy of 99.3%: we \n","decreased our error rate by 68% (relative). Not bad! "]}]} -------------------------------------------------------------------------------- /Cheatsheets/python_cheatsheet_dictionaries.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Deep-Learning-in-Computer-Vision/5251ac143cd8ef39da75ffb7ff5ede29b7c2d447/Cheatsheets/python_cheatsheet_dictionaries.pdf -------------------------------------------------------------------------------- /Cheatsheets/python_cheatsheet_functions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Deep-Learning-in-Computer-Vision/5251ac143cd8ef39da75ffb7ff5ede29b7c2d447/Cheatsheets/python_cheatsheet_functions.pdf -------------------------------------------------------------------------------- /Cheatsheets/python_cheatsheet_if_while.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Deep-Learning-in-Computer-Vision/5251ac143cd8ef39da75ffb7ff5ede29b7c2d447/Cheatsheets/python_cheatsheet_if_while.pdf -------------------------------------------------------------------------------- /Cheatsheets/python_cheatsheet_lists.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Deep-Learning-in-Computer-Vision/5251ac143cd8ef39da75ffb7ff5ede29b7c2d447/Cheatsheets/python_cheatsheet_lists.pdf -------------------------------------------------------------------------------- /Exercises/Assignment-1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This exercise is to test your understanding of Python basics.\n", 8 | "\n", 9 | "Answer the questions and complete the tasks outlined below; use the specific method described if applicable. In order to get complete points on your homework assigment you have to a) complete this notebook, b) based on your results answer the multiple choice questions on QoestromTools, and c) upload your completed notebook to the last question of the assignment. Both your notebook and your answers will be graded.\n", 10 | "\n", 11 | "# Question 1\n", 12 | "**What is 9 to the power of 7?**" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 1, 18 | "metadata": {}, 19 | "outputs": [], 20 | "source": [ 21 | "# Your answer goes here\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "# Question 2\n", 29 | "\n", 30 | "**What is the quotient and remainder of 453634/34?**" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 2, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "Quotient of 453634//34:\n", 43 | "Remainder of 453634//34:\n" 44 | ] 45 | } 46 | ], 47 | "source": [ 48 | "# Your answer goes here\n", 49 | "print('Quotient of 453634//34:')\n", 50 | "print('Remainder of 453634//34:')" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "# Question 3\n", 58 | "\n", 59 | "Write a statement to check whether `a` is a multiple of 7 and within the range of [1000, 1800) or (0, 300]. \n", 60 | "\n", 61 | "**What is the outcome of `a = 833`?**\n", 62 | "\n", 63 | "Note: (0, 300] represents a range from 0 to 300, where 0 is not included in the range, but 300 is." 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 3, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [ 72 | "a = 833" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 4, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "# Your answer goes here\n" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "# Question 4\n", 89 | "\n", 90 | "**Given this nested list, what indexing yields to the word \"hello\"?**" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 5, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "name": "stdout", 100 | "output_type": "stream", 101 | "text": [ 102 | "[[5, [100, 200, {'target': [1, 2, 3, 'hello']}], 23, 11], 1, 71, 2, [3, 4], 'bye']\n" 103 | ] 104 | } 105 | ], 106 | "source": [ 107 | "lst = [[5,[100,200,{'target':[1,2,3,'hello']}],23,11],1,71,2,[3,4],'bye']\n", 108 | "print(lst)" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 6, 114 | "metadata": {}, 115 | "outputs": [], 116 | "source": [ 117 | "# Your answer goes here\n" 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | "# Question 5\n", 125 | "\n", 126 | "Using a list comprehension, create a new list out of the list `L1`, which contains only the even numbers from `L1`, and converts them into absolute values (using `abs()` function). Call this new list `L2`.\n", 127 | "\n", 128 | "**What is the sum of all of the elements of `L2`?** \n", 129 | "\n", 130 | "Hint: Use `sum(L2)` to get the sum of all the elements." 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 7, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "L1 = [64, 34, 112, 91, 62, 40, 117, 80, 96, 34, 48, -9, -33,\n", 140 | " 99, 16, 118, -51, 60, 115, 4, -10, 82, -7, 77, -33, -40,\n", 141 | " 77, 90, -9, 52, -44, 25, -43, 28, -37, 92, 25, -45, 3,\n", 142 | " 103, 22, 39, -52, 74, -54, -76, -10, 5, -54, 95, -59, -2,\n", 143 | " 110, 63, -53, 113, -43, 18, 49, -20, 81, -67, 1, 38, -24,\n", 144 | " 57, -11, -69, -66, -67, -68, -16, 64, -34, 52, -37, -7, -40,\n", 145 | " 11, -3, 76, 91, -57, -48, -10, -16, 14, 13, -65]" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 8, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [ 154 | "# Your answer goes here\n" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "# Question 6\n", 162 | "\n", 163 | "Write a function that receives a list of integer numbers and returns a list of numbers that are multiples of 4. Call this function `mult4_filter()`.\n", 164 | "\n", 165 | "**Given the list `L3` below how many elements the outcome of `mult4_filter(L3)` has?**\n", 166 | "\n", 167 | "Hint: use `len(mult4_filter(L3))` to get the number of elements." 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 9, 173 | "metadata": {}, 174 | "outputs": [], 175 | "source": [ 176 | "L3 = [15, 11, 1, 3, 13, 3, 14, 16, 17, 17, 6, 18, 10, 19, 8, 1, 18,\n", 177 | " 17, 14, 1, 5, 2, 13, 0, 1, 13, 16, 8, 5, 11, 12, 8, 17, 14,\n", 178 | " 10, 18, 17, 16, 3, 7, 8, 15, 18, 7, 10, 5, 7, 16, 6, 5]" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 10, 184 | "metadata": {}, 185 | "outputs": [], 186 | "source": [ 187 | "# Your answer goes here\n", 188 | "def mult4_filter(L):\n", 189 | " # Your code goes here\n", 190 | " return" 191 | ] 192 | } 193 | ], 194 | "metadata": { 195 | "kernelspec": { 196 | "display_name": "Python 3", 197 | "language": "python", 198 | "name": "python3" 199 | }, 200 | "language_info": { 201 | "codemirror_mode": { 202 | "name": "ipython", 203 | "version": 3 204 | }, 205 | "file_extension": ".py", 206 | "mimetype": "text/x-python", 207 | "name": "python", 208 | "nbconvert_exporter": "python", 209 | "pygments_lexer": "ipython3", 210 | "version": "3.7.2" 211 | } 212 | }, 213 | "nbformat": 4, 214 | "nbformat_minor": 2 215 | } 216 | -------------------------------------------------------------------------------- /Exercises/Assignment-2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Assignment 2" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Data\n", 15 | "\n", 16 | "This is a historical dataset on the modern Olympic Games, including all the Games from Athens 1896 to Rio 2016. The data was taken from Kaggle. The `athlete_events` Dataset contains $271,116$ rows and $15$ columns.\n", 17 | "\n", 18 | "**Source:**\n", 19 | "\n", 20 | "Griffin, R, H (2018) 120 years of Olympic history: athletes and results, athlete_events, Found at: https://www.kaggle.com/heesoo37/120-years-of-olympic-history-athletes-and-results#athlete_events.csv\n", 21 | "\n", 22 | "**ATTRIBUTES:**\n", 23 | "\n", 24 | "**athlete_events.csv**\n", 25 | "\n", 26 | "| Column Name | Data Type | Description/Notes |\n", 27 | "|:----:|:----:|:----|\n", 28 | "| ID | integer | Unique number for each athlete |\n", 29 | "| Name | string | Athlete’s name |\n", 30 | "| Sex | string | M or F |\n", 31 | "| Age | integer | |\n", 32 | "| Height | integer | In centimeters |\n", 33 | "| Weight | integer | In kilograms |\n", 34 | "| Team | string | Team name |\n", 35 | "| NOC | string | National Olympic Committee, 3 letter code (Matches with `NOC` from noc_regions.csv) |\n", 36 | "| Games | string | Year and season |\n", 37 | "| Year | integer | |\n", 38 | "| Season | string | Summer or Winter |\n", 39 | "| City | string | Host city |\n", 40 | "| Sport | string | |\n", 41 | "| Event | string | |\n", 42 | "| Medal | string | Gold, Silver, Bronze, or NA |" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "Download athlete_events.csv from the link above and load it into a DataFrame called `athlete_events`:" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 1, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "# Your answer goes here\n" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "Use `info()` method on this DataFrame to get a sense of the data:" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 2, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "# Your answer goes here\n" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "## Question 1\n", 82 | "Clearly not every athlete receives a medal in the Olympics. How many records have a non-null value for `Medal` field? In other words, how many medals were given according to this dataset?" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 3, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "# Your answer goes here\n" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "Use the `head()` method to inspect the first few rows of the data:" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 4, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "# Your answer goes here\n" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "## Question 2\n", 115 | "\n", 116 | "What is the minimum and maximum `Year`?" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 5, 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [ 125 | "# Your answer goes here\n" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 6, 131 | "metadata": {}, 132 | "outputs": [], 133 | "source": [ 134 | "# Your answer goes here\n" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "## Question 3\n", 142 | "\n", 143 | "Is the following statement True or False?\n", 144 | "\n", 145 | "> Average age of female athletes who attended the olympic games after 1990 has raised when compared to the era before then." 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 7, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [ 154 | "# Your answer goes here\n" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 8, 160 | "metadata": {}, 161 | "outputs": [], 162 | "source": [ 163 | "# Your answer goes here\n" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "## Question 4\n", 171 | "\n", 172 | "How many Gold medals were given to men from 1970 to 2000 (including both years)?" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 9, 178 | "metadata": {}, 179 | "outputs": [], 180 | "source": [ 181 | "# Your answer goes here\n" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": {}, 187 | "source": [ 188 | "## Question 5\n", 189 | "\n", 190 | "How many athletes attended the olympic games in 1968?\n", 191 | "\n", 192 | "Hint: You can use the method `.unique()` on any pandas Series to get the unique values. Any column subset of a DataFrame returns a Series object." 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 10, 198 | "metadata": {}, 199 | "outputs": [], 200 | "source": [ 201 | "# Your answer goes here\n" 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": {}, 207 | "source": [ 208 | "## Question 6\n", 209 | "\n", 210 | "Who won the event \"Swimming Men's 100 metres Breaststroke\" in 2004? Please note that in the Event description \"metres\" is spelled in British!" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 11, 216 | "metadata": {}, 217 | "outputs": [], 218 | "source": [ 219 | "# Your answer goes here\n" 220 | ] 221 | } 222 | ], 223 | "metadata": { 224 | "kernelspec": { 225 | "display_name": "Python 3", 226 | "language": "python", 227 | "name": "python3" 228 | }, 229 | "language_info": { 230 | "codemirror_mode": { 231 | "name": "ipython", 232 | "version": 3 233 | }, 234 | "file_extension": ".py", 235 | "mimetype": "text/x-python", 236 | "name": "python", 237 | "nbconvert_exporter": "python", 238 | "pygments_lexer": "ipython3", 239 | "version": "3.7.2" 240 | } 241 | }, 242 | "nbformat": 4, 243 | "nbformat_minor": 4 244 | } 245 | -------------------------------------------------------------------------------- /Exercises/Assignment-3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Assignment 3\n", 8 | "\n", 9 | "Using the customer churn dataset create a logistic regression model to predict whether a customer will churn or not.\n", 10 | "\n", 11 | "Follow the steps outlined in the [Classification notebook](https://github.com/soltaniehha/Python-Bootcamp/blob/master/Lectures/06-ML/03-Classification.ipynb):\n", 12 | "* Preprocessing: deleting columns with no predictive power\n", 13 | "* Preprocessing: categorical variables\n", 14 | "* Create feature matrix and target vector\n", 15 | "* Split the data into train and test with a 70-30 split\n", 16 | "* Instantiate and fit the model\n", 17 | "* Make predictions and find the overall accuracy on your test set\n", 18 | "\n", 19 | "Note: We have seen this dataset before in the [EDA notebook](https://github.com/soltaniehha/Python-Bootcamp/blob/master/Lectures/05-Joins-and-EDA/02-Exploratory-Data-Analysis.ipynb).\n", 20 | "\n", 21 | "The data has been loaded in the following cell:" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 2, 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "data": { 31 | "text/html": [ 32 | "
\n", 33 | "\n", 46 | "\n", 47 | " \n", 48 | " \n", 49 | " \n", 50 | " \n", 51 | " \n", 52 | " \n", 53 | " \n", 54 | " \n", 55 | " \n", 56 | " \n", 57 | " \n", 58 | " \n", 59 | " \n", 60 | " \n", 61 | " \n", 62 | " \n", 63 | " \n", 64 | " \n", 65 | " \n", 66 | " \n", 67 | " \n", 68 | " \n", 69 | " \n", 70 | " \n", 71 | " \n", 72 | " \n", 73 | " \n", 74 | " \n", 75 | " \n", 76 | " \n", 77 | " \n", 78 | " \n", 79 | " \n", 80 | " \n", 81 | " \n", 82 | " \n", 83 | " \n", 84 | " \n", 85 | " \n", 86 | " \n", 87 | " \n", 88 | " \n", 89 | " \n", 90 | " \n", 91 | " \n", 92 | " \n", 93 | " \n", 94 | " \n", 95 | " \n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | " \n", 100 | " \n", 101 | " \n", 102 | " \n", 103 | " \n", 104 | " \n", 105 | " \n", 106 | " \n", 107 | " \n", 108 | " \n", 109 | " \n", 110 | " \n", 111 | " \n", 112 | " \n", 113 | " \n", 114 | " \n", 115 | " \n", 116 | " \n", 117 | " \n", 118 | " \n", 119 | " \n", 120 | " \n", 121 | " \n", 122 | " \n", 123 | " \n", 124 | " \n", 125 | " \n", 126 | " \n", 127 | " \n", 128 | " \n", 129 | " \n", 130 | " \n", 131 | " \n", 132 | " \n", 133 | " \n", 134 | " \n", 135 | " \n", 136 | " \n", 137 | " \n", 138 | " \n", 139 | " \n", 140 | " \n", 141 | " \n", 142 | " \n", 143 | " \n", 144 | " \n", 145 | " \n", 146 | " \n", 147 | "
customerIDgenderSeniorCitizenPartnerDependentstenurePhoneServiceMultipleLinesInternetServiceOnlineSecurity...DeviceProtectionTechSupportStreamingTVStreamingMoviesContractPaperlessBillingPaymentMethodMonthlyChargesTotalChargesChurn
07590-VHVEGFemale0YesNo1NoNo phone serviceDSLNo...NoNoNoNoMonth-to-monthYesElectronic check29.8529.850000No
15575-GNVDEMale0NoNo34YesNoDSLYes...YesNoNoNoOne yearNoMailed check56.951889.500000No
23668-QPYBKMale0NoNo2YesNoDSLYes...NoNoNoNoMonth-to-monthYesMailed check53.85108.150002Yes
\n", 148 | "

3 rows × 21 columns

\n", 149 | "
" 150 | ], 151 | "text/plain": [ 152 | " customerID gender SeniorCitizen Partner Dependents tenure PhoneService \\\n", 153 | "0 7590-VHVEG Female 0 Yes No 1 No \n", 154 | "1 5575-GNVDE Male 0 No No 34 Yes \n", 155 | "2 3668-QPYBK Male 0 No No 2 Yes \n", 156 | "\n", 157 | " MultipleLines InternetService OnlineSecurity ... DeviceProtection \\\n", 158 | "0 No phone service DSL No ... No \n", 159 | "1 No DSL Yes ... Yes \n", 160 | "2 No DSL Yes ... No \n", 161 | "\n", 162 | " TechSupport StreamingTV StreamingMovies Contract PaperlessBilling \\\n", 163 | "0 No No No Month-to-month Yes \n", 164 | "1 No No No One year No \n", 165 | "2 No No No Month-to-month Yes \n", 166 | "\n", 167 | " PaymentMethod MonthlyCharges TotalCharges Churn \n", 168 | "0 Electronic check 29.85 29.850000 No \n", 169 | "1 Mailed check 56.95 1889.500000 No \n", 170 | "2 Mailed check 53.85 108.150002 Yes \n", 171 | "\n", 172 | "[3 rows x 21 columns]" 173 | ] 174 | }, 175 | "execution_count": 2, 176 | "metadata": {}, 177 | "output_type": "execute_result" 178 | } 179 | ], 180 | "source": [ 181 | "import pandas as pd\n", 182 | "\n", 183 | "df = pd.read_csv('https://raw.githubusercontent.com/soltaniehha/Python-Bootcamp/master/data/Telco-Customer-Churn.csv')\n", 184 | "df['TotalCharges'] = df['TotalCharges'].replace(\" \", 0).astype('float32')\n", 185 | "df.head(3)" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "**Note: For this assignment you are allowed to work within your assigned team, but what you submit has to be your own work.**" 193 | ] 194 | } 195 | ], 196 | "metadata": { 197 | "kernelspec": { 198 | "display_name": "Python 3", 199 | "language": "python", 200 | "name": "python3" 201 | }, 202 | "language_info": { 203 | "codemirror_mode": { 204 | "name": "ipython", 205 | "version": 3 206 | }, 207 | "file_extension": ".py", 208 | "mimetype": "text/x-python", 209 | "name": "python", 210 | "nbconvert_exporter": "python", 211 | "pygments_lexer": "ipython3", 212 | "version": "3.7.2" 213 | } 214 | }, 215 | "nbformat": 4, 216 | "nbformat_minor": 4 217 | } 218 | -------------------------------------------------------------------------------- /Exercises/Exercise-01-Introduction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "kernelspec": { 6 | "display_name": "Python 3", 7 | "language": "python", 8 | "name": "python3" 9 | }, 10 | "language_info": { 11 | "codemirror_mode": { 12 | "name": "ipython", 13 | "version": 3 14 | }, 15 | "file_extension": ".py", 16 | "mimetype": "text/x-python", 17 | "name": "python", 18 | "nbconvert_exporter": "python", 19 | "pygments_lexer": "ipython3", 20 | "version": "3.7.2" 21 | }, 22 | "colab": { 23 | "name": "Exercise-01-Introduction.ipynb", 24 | "provenance": [], 25 | "collapsed_sections": [] 26 | } 27 | }, 28 | "cells": [ 29 | { 30 | "cell_type": "markdown", 31 | "metadata": { 32 | "id": "3f2a_IlEgY84", 33 | "colab_type": "text" 34 | }, 35 | "source": [ 36 | "# Exercise 1\n", 37 | "For this exercise we are going to get more familiar with Jupyter notebooks. Follow these steps to complete the task:\n", 38 | "\n", 39 | "* Create a new notebook: In Colab select **File** > **New notebook**\n", 40 | "* Create a new \"Text\" cell on the top: you should hover over the top of your notebook (in the middle of the screen) to see the option appear.\n", 41 | "* In your Text/Markdown cell type some description, something like:\n", 42 | "\n", 43 | "```\n", 44 | "# Python version\n", 45 | "The following code provides the version of the Python that this notebook is running on:\n", 46 | "```\n", 47 | "\n", 48 | "* Now hit Shift + Enter to see the rendered text.\n", 49 | "* You should have a new cell below that has a Code type by default.\n", 50 | "* In this Code cell type the following code to get the version of your Python:\n", 51 | "\n", 52 | "```python\n", 53 | "import sys\n", 54 | "print(sys.version)\n", 55 | "```\n", 56 | "\n", 57 | "* Hit Shift + Enter for it to run. Alternatively, you can hit the Run button on the left hand side.\n", 58 | "* Give your notebook a good name. You can modify the name of your notebook by clicking on the current name (Untitled) on the top. Call it anything you think is descriptive of this notebook, something like My First Notebook!\n", 59 | "* You can run your notebook from top to bottom by selecting **Runtime** > **Restart & Run All** (or **Run all**) from the menu. Try it! This is particularly useful after you are done working on a notebook to make sure everything works as expected when the RAM is fresh.\n", 60 | "* Save your notebook - **File** > **Save** from the menu. Your Colab notebook should also get saved periodically. \n", 61 | "* Export - From **File** > **Download .ipynb**\n", 62 | "* Check the history of your edits: On the top of the notebook where it says \"All changes saved\" you can view the history like any other Google Doc.\n", 63 | "* Share: you may share this notebook with others by putting their email address. It's located on the top right." 64 | ] 65 | } 66 | ] 67 | } -------------------------------------------------------------------------------- /Exercises/Exercise-02-Python-Syntax-and-Semantics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Exercise 1\n", 8 | "How can you improve the syntax used in the following code cells?\n", 9 | "\n", 10 | "Think about conventions, and code readability." 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "x= 4+ 1" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "metadata": {}, 26 | "outputs": [ 27 | { 28 | "name": "stdout", 29 | "output_type": "stream", 30 | "text": [ 31 | "5\n", 32 | "6\n", 33 | "7\n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "print(x); x = x + 1; print(x); x += 1; print(x)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 3, 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "0 is a multiple of 3!\n", 51 | "3 is a multiple of 3!\n", 52 | "6 is a multiple of 3!\n", 53 | "9 is a multiple of 3!\n" 54 | ] 55 | } 56 | ], 57 | "source": [ 58 | "for i in range(10):\n", 59 | " if i % 3 == 0 :#checks to see if i is a multiple of 3\n", 60 | " print(i, 'is a multiple of 3!')" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 4, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "data": { 70 | "text/plain": [ 71 | "10.0" 72 | ] 73 | }, 74 | "execution_count": 4, 75 | "metadata": {}, 76 | "output_type": "execute_result" 77 | } 78 | ], 79 | "source": [ 80 | "2*8+8/4-9+2*1-1" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "# Exercise 2\n", 88 | "Create a variable `savings` and give it value 200" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 1, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "# Your answer goes here\n" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "Create a variable `factor` and assign it value 1.1" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 2, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "# Your answer goes here\n" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "Calculate `balance`:" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 3, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "balance = savings * factor ** 5" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "Now print a statement that says something like \"Your account balance after 5 years would be: $xxx\" and use the value of `balance` to populate xxx:" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 4, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "name": "stdout", 146 | "output_type": "stream", 147 | "text": [ 148 | "Your account balance after 5 years would be: $ 322.1020000000001\n" 149 | ] 150 | } 151 | ], 152 | "source": [ 153 | "# Your answer goes here\n" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "Let's make two modifications to this:\n", 161 | "* use `sep = ''` in print function to avoid the extra space between dollar sign and balance value.\n", 162 | "* use `round()` function to round to two decimals\n", 163 | "\n", 164 | "Note: Use `?round` in a new cell to get help from the help function." 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 5, 170 | "metadata": {}, 171 | "outputs": [ 172 | { 173 | "name": "stdout", 174 | "output_type": "stream", 175 | "text": [ 176 | "Your account balance after 5 years would be: $322.1\n" 177 | ] 178 | } 179 | ], 180 | "source": [ 181 | "# Your answer goes here\n" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": {}, 187 | "source": [ 188 | "# Exercise 3\n", 189 | "The value of pi is given below in a string/character format:" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 6, 195 | "metadata": {}, 196 | "outputs": [ 197 | { 198 | "data": { 199 | "text/plain": [ 200 | "str" 201 | ] 202 | }, 203 | "execution_count": 6, 204 | "metadata": {}, 205 | "output_type": "execute_result" 206 | } 207 | ], 208 | "source": [ 209 | "pi_char = \"3.1415926535\"\n", 210 | "type(pi_char)" 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "Google and find a function that would convert it to float so we can use it in math equations, call this new converted value `pi`" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 7, 223 | "metadata": {}, 224 | "outputs": [], 225 | "source": [ 226 | "# Your answer goes here\n" 227 | ] 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "metadata": {}, 232 | "source": [ 233 | "Using this variable calculate the area of a circle with radius `r = 2` and print a rounded value for area (up to 3 decimals):" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 8, 239 | "metadata": {}, 240 | "outputs": [ 241 | { 242 | "name": "stdout", 243 | "output_type": "stream", 244 | "text": [ 245 | "The area of a circle with radius 2 is 12.566\n" 246 | ] 247 | } 248 | ], 249 | "source": [ 250 | "# Your answer goes here\n" 251 | ] 252 | }, 253 | { 254 | "cell_type": "markdown", 255 | "metadata": {}, 256 | "source": [ 257 | "# Exercise 4\n", 258 | "What is the quotient and remainder of 67432/98?" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 1, 264 | "metadata": {}, 265 | "outputs": [ 266 | { 267 | "name": "stdout", 268 | "output_type": "stream", 269 | "text": [ 270 | "Quotient of 67432/98: 688\n", 271 | "Remainder of 67432/98: 8\n" 272 | ] 273 | } 274 | ], 275 | "source": [ 276 | "# Your answer goes here\n" 277 | ] 278 | }, 279 | { 280 | "cell_type": "markdown", 281 | "metadata": {}, 282 | "source": [ 283 | "# Exercise 5\n", 284 | "Write a statement to check whether `a` is a multiple of 5 and within the range of 100-150 or 0-50. Check your statement with the following values and it should give you the following values:\n", 285 | "\n", 286 | "55 --> False\n", 287 | "\n", 288 | "110 --> True\n", 289 | "\n", 290 | "-25 --> False\n", 291 | "\n", 292 | "150 --> False\n", 293 | "\n", 294 | "100 --> True" 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": 1, 300 | "metadata": {}, 301 | "outputs": [], 302 | "source": [ 303 | "# Your answer goes here\n" 304 | ] 305 | } 306 | ], 307 | "metadata": { 308 | "kernelspec": { 309 | "display_name": "Python 3", 310 | "language": "python", 311 | "name": "python3" 312 | }, 313 | "language_info": { 314 | "codemirror_mode": { 315 | "name": "ipython", 316 | "version": 3 317 | }, 318 | "file_extension": ".py", 319 | "mimetype": "text/x-python", 320 | "name": "python", 321 | "nbconvert_exporter": "python", 322 | "pygments_lexer": "ipython3", 323 | "version": "3.7.2" 324 | } 325 | }, 326 | "nbformat": 4, 327 | "nbformat_minor": 2 328 | } 329 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Mohammad Soltanieh-ha 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Projects/Readme.md: -------------------------------------------------------------------------------- 1 | # Student Projects 2 | 3 | This directory consists of the work done by the students in ONPS Internation Summer School in Taipei, Taiwan (July 2020). 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Deep-Learning-in-Computer-Vision 2 | This repository contains material for the python workshop on deep learning in computer vision. 3 | 4 | We start with the basics of python, and get to work with some of the fundamental python libraries for data science such as: `pandas`, `numpy`, `matplotlib`, `seaborn`, `scikit-learn`, and more. We then dive into `Keras` with `tensorflow` backend and focus on image classification tasks. The Convolutional Neural Network (CNN) models we create varry from fully training to transfer learning to fine-tuning with transfer learning. Students will complete this workshop by working on a dataset of their choice to train a fully functioning model using Keras. 5 | 6 | This repository can be accessed via this short link: 7 | 8 | 9 | # [bit.ly/dl-in-cv](https://github.com/soltaniehha/Deep-Learning-in-Computer-Vision) 10 | 11 | ## Textbooks 12 | * [A Whirlwind Tour of the Python Language](https://www.oreilly.com/programming/free/files/a-whirlwind-tour-of-python.pdf) by Jake VanderPlas 13 | * [Deep Learning with Python (2nd edition)](https://www.manning.com/books/deep-learning-with-python-second-edition) by François Chollet 14 | 15 | 16 | ## Acknowledgment 17 | 18 | * Notebooks in Python Basics folder come from [*A Whirlwind Tour of Python*](http://www.oreilly.com/programming/free/a-whirlwind-tour-of-python.csp) (free [100-page pdf](http://www.oreilly.com/programming/free/files/a-whirlwind-tour-of-python.pdf)) by Jake VanderPlas (under [CC0](https://creativecommons.org/share-your-work/public-domain/cc0/) license). Slight modifications and updates have been made in some places to keep its content up to date. *A Whirlwind Tour of Python* is a fast-paced introduction to essential components of the Python language for data science and/or scientific programming. This material was written and tested using **Python 3.7**, and should work for any Python 3.X version. 19 | 20 | * Some content of a handful of notebooks come from [Python Data Science Handbook](http://shop.oreilly.com/product/0636920034919.do) by Jake VanderPlas (under the [MIT license](LICENSE-CODE). Read more at the [Open Source Initiative](https://opensource.org/licenses/MIT)). Modifications and updates have been made. 21 | 22 | * The content of notebook 12-Exploratory-Data-Analysis.ipynb comes primarily from [a Kaggle notebook](https://www.kaggle.com/jsaguiar/exploratory-analysis-with-seaborn) by *Aguiar*. 23 | 24 | * Notebooks on the second half of the workshop are corresponding chapters of [Deep Learning with Python (2nd edition)](https://www.manning.com/books/deep-learning-with-python-second-edition) 25 | -------------------------------------------------------------------------------- /chapter11_part02_sequence-models.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text" 7 | }, 8 | "source": [ 9 | "This is a companion notebook for the book [Deep Learning with Python, Second Edition](https://www.manning.com/books/deep-learning-with-python-second-edition?a_aid=keras&a_bid=76564dff). For readability, it only contains runnable code blocks and section titles, and omits everything else in the book: text paragraphs, figures, and pseudocode.\n\n**If you want to be able to follow what's going on, I recommend reading the notebook side by side with your copy of the book.**\n\nThis notebook was generated for TensorFlow 2.6." 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": { 15 | "colab_type": "text" 16 | }, 17 | "source": [ 18 | "### Processing words as a sequence: The sequence model approach" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "colab_type": "text" 25 | }, 26 | "source": [ 27 | "#### A first practical example" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": { 33 | "colab_type": "text" 34 | }, 35 | "source": [ 36 | "**Downloading the data**" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 0, 42 | "metadata": { 43 | "colab_type": "code" 44 | }, 45 | "outputs": [], 46 | "source": [ 47 | "!curl -O https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz\n", 48 | "!tar -xf aclImdb_v1.tar.gz\n", 49 | "!rm -r aclImdb/train/unsup" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": { 55 | "colab_type": "text" 56 | }, 57 | "source": [ 58 | "**Preparing the data**" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 0, 64 | "metadata": { 65 | "colab_type": "code" 66 | }, 67 | "outputs": [], 68 | "source": [ 69 | "import os, pathlib, shutil, random\n", 70 | "from tensorflow import keras\n", 71 | "batch_size = 32\n", 72 | "base_dir = pathlib.Path(\"aclImdb\")\n", 73 | "val_dir = base_dir / \"val\"\n", 74 | "train_dir = base_dir / \"train\"\n", 75 | "for category in (\"neg\", \"pos\"):\n", 76 | " os.makedirs(val_dir / category)\n", 77 | " files = os.listdir(train_dir / category)\n", 78 | " random.Random(1337).shuffle(files)\n", 79 | " num_val_samples = int(0.2 * len(files))\n", 80 | " val_files = files[-num_val_samples:]\n", 81 | " for fname in val_files:\n", 82 | " shutil.move(train_dir / category / fname,\n", 83 | " val_dir / category / fname)\n", 84 | "\n", 85 | "train_ds = keras.utils.text_dataset_from_directory(\n", 86 | " \"aclImdb/train\", batch_size=batch_size\n", 87 | ")\n", 88 | "val_ds = keras.utils.text_dataset_from_directory(\n", 89 | " \"aclImdb/val\", batch_size=batch_size\n", 90 | ")\n", 91 | "test_ds = keras.utils.text_dataset_from_directory(\n", 92 | " \"aclImdb/test\", batch_size=batch_size\n", 93 | ")\n", 94 | "text_only_train_ds = train_ds.map(lambda x, y: x)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": { 100 | "colab_type": "text" 101 | }, 102 | "source": [ 103 | "**Preparing integer sequence datasets**" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 0, 109 | "metadata": { 110 | "colab_type": "code" 111 | }, 112 | "outputs": [], 113 | "source": [ 114 | "from tensorflow.keras import layers\n", 115 | "\n", 116 | "max_length = 600\n", 117 | "max_tokens = 20000\n", 118 | "text_vectorization = layers.TextVectorization(\n", 119 | " max_tokens=max_tokens,\n", 120 | " output_mode=\"int\",\n", 121 | " output_sequence_length=max_length,\n", 122 | ")\n", 123 | "text_vectorization.adapt(text_only_train_ds)\n", 124 | "\n", 125 | "int_train_ds = train_ds.map(\n", 126 | " lambda x, y: (text_vectorization(x), y),\n", 127 | " num_parallel_calls=4)\n", 128 | "int_val_ds = val_ds.map(\n", 129 | " lambda x, y: (text_vectorization(x), y),\n", 130 | " num_parallel_calls=4)\n", 131 | "int_test_ds = test_ds.map(\n", 132 | " lambda x, y: (text_vectorization(x), y),\n", 133 | " num_parallel_calls=4)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": { 139 | "colab_type": "text" 140 | }, 141 | "source": [ 142 | "**A sequence model built on one-hot encoded vector sequences**" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 0, 148 | "metadata": { 149 | "colab_type": "code" 150 | }, 151 | "outputs": [], 152 | "source": [ 153 | "import tensorflow as tf\n", 154 | "inputs = keras.Input(shape=(None,), dtype=\"int64\")\n", 155 | "embedded = tf.one_hot(inputs, depth=max_tokens)\n", 156 | "x = layers.Bidirectional(layers.LSTM(32))(embedded)\n", 157 | "x = layers.Dropout(0.5)(x)\n", 158 | "outputs = layers.Dense(1, activation=\"sigmoid\")(x)\n", 159 | "model = keras.Model(inputs, outputs)\n", 160 | "model.compile(optimizer=\"rmsprop\",\n", 161 | " loss=\"binary_crossentropy\",\n", 162 | " metrics=[\"accuracy\"])\n", 163 | "model.summary()" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": { 169 | "colab_type": "text" 170 | }, 171 | "source": [ 172 | "**Training a first basic sequence model**" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 0, 178 | "metadata": { 179 | "colab_type": "code" 180 | }, 181 | "outputs": [], 182 | "source": [ 183 | "callbacks = [\n", 184 | " keras.callbacks.ModelCheckpoint(\"one_hot_bidir_lstm.keras\",\n", 185 | " save_best_only=True)\n", 186 | "]\n", 187 | "model.fit(int_train_ds, validation_data=int_val_ds, epochs=10, callbacks=callbacks)\n", 188 | "model = keras.models.load_model(\"one_hot_bidir_lstm.keras\")\n", 189 | "print(f\"Test acc: {model.evaluate(int_test_ds)[1]:.3f}\")" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": { 195 | "colab_type": "text" 196 | }, 197 | "source": [ 198 | "#### Understanding word embeddings" 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": { 204 | "colab_type": "text" 205 | }, 206 | "source": [ 207 | "#### Learning word embeddings with the Embedding layer" 208 | ] 209 | }, 210 | { 211 | "cell_type": "markdown", 212 | "metadata": { 213 | "colab_type": "text" 214 | }, 215 | "source": [ 216 | "**Instantiating an `Embedding` layer**" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 0, 222 | "metadata": { 223 | "colab_type": "code" 224 | }, 225 | "outputs": [], 226 | "source": [ 227 | "embedding_layer = layers.Embedding(input_dim=max_tokens, output_dim=256)" 228 | ] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "metadata": { 233 | "colab_type": "text" 234 | }, 235 | "source": [ 236 | "**Model that uses an `Embedding` layer trained from scratch**" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": 0, 242 | "metadata": { 243 | "colab_type": "code" 244 | }, 245 | "outputs": [], 246 | "source": [ 247 | "inputs = keras.Input(shape=(None,), dtype=\"int64\")\n", 248 | "embedded = layers.Embedding(input_dim=max_tokens, output_dim=256)(inputs)\n", 249 | "x = layers.Bidirectional(layers.LSTM(32))(embedded)\n", 250 | "x = layers.Dropout(0.5)(x)\n", 251 | "outputs = layers.Dense(1, activation=\"sigmoid\")(x)\n", 252 | "model = keras.Model(inputs, outputs)\n", 253 | "model.compile(optimizer=\"rmsprop\",\n", 254 | " loss=\"binary_crossentropy\",\n", 255 | " metrics=[\"accuracy\"])\n", 256 | "model.summary()\n", 257 | "\n", 258 | "callbacks = [\n", 259 | " keras.callbacks.ModelCheckpoint(\"embeddings_bidir_gru.keras\",\n", 260 | " save_best_only=True)\n", 261 | "]\n", 262 | "model.fit(int_train_ds, validation_data=int_val_ds, epochs=10, callbacks=callbacks)\n", 263 | "model = keras.models.load_model(\"embeddings_bidir_gru.keras\")\n", 264 | "print(f\"Test acc: {model.evaluate(int_test_ds)[1]:.3f}\")" 265 | ] 266 | }, 267 | { 268 | "cell_type": "markdown", 269 | "metadata": { 270 | "colab_type": "text" 271 | }, 272 | "source": [ 273 | "#### Understanding padding and masking" 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": { 279 | "colab_type": "text" 280 | }, 281 | "source": [ 282 | "**Using an `Embedding` layer with masking enabled**" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": 0, 288 | "metadata": { 289 | "colab_type": "code" 290 | }, 291 | "outputs": [], 292 | "source": [ 293 | "inputs = keras.Input(shape=(None,), dtype=\"int64\")\n", 294 | "embedded = layers.Embedding(\n", 295 | " input_dim=max_tokens, output_dim=256, mask_zero=True)(inputs)\n", 296 | "x = layers.Bidirectional(layers.LSTM(32))(embedded)\n", 297 | "x = layers.Dropout(0.5)(x)\n", 298 | "outputs = layers.Dense(1, activation=\"sigmoid\")(x)\n", 299 | "model = keras.Model(inputs, outputs)\n", 300 | "model.compile(optimizer=\"rmsprop\",\n", 301 | " loss=\"binary_crossentropy\",\n", 302 | " metrics=[\"accuracy\"])\n", 303 | "model.summary()\n", 304 | "\n", 305 | "callbacks = [\n", 306 | " keras.callbacks.ModelCheckpoint(\"embeddings_bidir_gru_with_masking.keras\",\n", 307 | " save_best_only=True)\n", 308 | "]\n", 309 | "model.fit(int_train_ds, validation_data=int_val_ds, epochs=10, callbacks=callbacks)\n", 310 | "model = keras.models.load_model(\"embeddings_bidir_gru_with_masking.keras\")\n", 311 | "print(f\"Test acc: {model.evaluate(int_test_ds)[1]:.3f}\")" 312 | ] 313 | }, 314 | { 315 | "cell_type": "markdown", 316 | "metadata": { 317 | "colab_type": "text" 318 | }, 319 | "source": [ 320 | "#### Using pretrained word embeddings" 321 | ] 322 | }, 323 | { 324 | "cell_type": "code", 325 | "execution_count": 0, 326 | "metadata": { 327 | "colab_type": "code" 328 | }, 329 | "outputs": [], 330 | "source": [ 331 | "!wget http://nlp.stanford.edu/data/glove.6B.zip\n", 332 | "!unzip -q glove.6B.zip" 333 | ] 334 | }, 335 | { 336 | "cell_type": "markdown", 337 | "metadata": { 338 | "colab_type": "text" 339 | }, 340 | "source": [ 341 | "**Parsing the GloVe word-embeddings file**" 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": 0, 347 | "metadata": { 348 | "colab_type": "code" 349 | }, 350 | "outputs": [], 351 | "source": [ 352 | "import numpy as np\n", 353 | "path_to_glove_file = \"glove.6B.100d.txt\"\n", 354 | "\n", 355 | "embeddings_index = {}\n", 356 | "with open(path_to_glove_file) as f:\n", 357 | " for line in f:\n", 358 | " word, coefs = line.split(maxsplit=1)\n", 359 | " coefs = np.fromstring(coefs, \"f\", sep=\" \")\n", 360 | " embeddings_index[word] = coefs\n", 361 | "\n", 362 | "print(f\"Found {len(embeddings_index)} word vectors.\")" 363 | ] 364 | }, 365 | { 366 | "cell_type": "markdown", 367 | "metadata": { 368 | "colab_type": "text" 369 | }, 370 | "source": [ 371 | "**Preparing the GloVe word-embeddings matrix**" 372 | ] 373 | }, 374 | { 375 | "cell_type": "code", 376 | "execution_count": 0, 377 | "metadata": { 378 | "colab_type": "code" 379 | }, 380 | "outputs": [], 381 | "source": [ 382 | "embedding_dim = 100\n", 383 | "\n", 384 | "vocabulary = text_vectorization.get_vocabulary()\n", 385 | "word_index = dict(zip(vocabulary, range(len(vocabulary))))\n", 386 | "\n", 387 | "embedding_matrix = np.zeros((max_tokens, embedding_dim))\n", 388 | "for word, i in word_index.items():\n", 389 | " if i < max_tokens:\n", 390 | " embedding_vector = embeddings_index.get(word)\n", 391 | " if embedding_vector is not None:\n", 392 | " embedding_matrix[i] = embedding_vector" 393 | ] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "execution_count": 0, 398 | "metadata": { 399 | "colab_type": "code" 400 | }, 401 | "outputs": [], 402 | "source": [ 403 | "embedding_layer = layers.Embedding(\n", 404 | " max_tokens,\n", 405 | " embedding_dim,\n", 406 | " embeddings_initializer=keras.initializers.Constant(embedding_matrix),\n", 407 | " trainable=False,\n", 408 | " mask_zero=True,\n", 409 | ")" 410 | ] 411 | }, 412 | { 413 | "cell_type": "markdown", 414 | "metadata": { 415 | "colab_type": "text" 416 | }, 417 | "source": [ 418 | "**Model that uses a pretrained Embedding layer**" 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "execution_count": 0, 424 | "metadata": { 425 | "colab_type": "code" 426 | }, 427 | "outputs": [], 428 | "source": [ 429 | "inputs = keras.Input(shape=(None,), dtype=\"int64\")\n", 430 | "embedded = embedding_layer(inputs)\n", 431 | "x = layers.Bidirectional(layers.LSTM(32))(embedded)\n", 432 | "x = layers.Dropout(0.5)(x)\n", 433 | "outputs = layers.Dense(1, activation=\"sigmoid\")(x)\n", 434 | "model = keras.Model(inputs, outputs)\n", 435 | "model.compile(optimizer=\"rmsprop\",\n", 436 | " loss=\"binary_crossentropy\",\n", 437 | " metrics=[\"accuracy\"])\n", 438 | "model.summary()\n", 439 | "\n", 440 | "callbacks = [\n", 441 | " keras.callbacks.ModelCheckpoint(\"glove_embeddings_sequence_model.keras\",\n", 442 | " save_best_only=True)\n", 443 | "]\n", 444 | "model.fit(int_train_ds, validation_data=int_val_ds, epochs=10, callbacks=callbacks)\n", 445 | "model = keras.models.load_model(\"glove_embeddings_sequence_model.keras\")\n", 446 | "print(f\"Test acc: {model.evaluate(int_test_ds)[1]:.3f}\")" 447 | ] 448 | } 449 | ], 450 | "metadata": { 451 | "colab": { 452 | "collapsed_sections": [], 453 | "name": "chapter11_part02_sequence-models.i", 454 | "private_outputs": false, 455 | "provenance": [], 456 | "toc_visible": true 457 | }, 458 | "kernelspec": { 459 | "display_name": "Python 3", 460 | "language": "python", 461 | "name": "python3" 462 | }, 463 | "language_info": { 464 | "codemirror_mode": { 465 | "name": "ipython", 466 | "version": 3 467 | }, 468 | "file_extension": ".py", 469 | "mimetype": "text/x-python", 470 | "name": "python", 471 | "nbconvert_exporter": "python", 472 | "pygments_lexer": "ipython3", 473 | "version": "3.7.0" 474 | } 475 | }, 476 | "nbformat": 4, 477 | "nbformat_minor": 0 478 | } 479 | -------------------------------------------------------------------------------- /chapter11_part03_transformer.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text" 7 | }, 8 | "source": [ 9 | "This is a companion notebook for the book [Deep Learning with Python, Second Edition](https://www.manning.com/books/deep-learning-with-python-second-edition?a_aid=keras&a_bid=76564dff). For readability, it only contains runnable code blocks and section titles, and omits everything else in the book: text paragraphs, figures, and pseudocode.\n\n**If you want to be able to follow what's going on, I recommend reading the notebook side by side with your copy of the book.**\n\nThis notebook was generated for TensorFlow 2.6." 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": { 15 | "colab_type": "text" 16 | }, 17 | "source": [ 18 | "## The Transformer architecture" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "colab_type": "text" 25 | }, 26 | "source": [ 27 | "### Understanding self-attention" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": { 33 | "colab_type": "text" 34 | }, 35 | "source": [ 36 | "#### Generalized self-attention: the query-key-value model" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": { 42 | "colab_type": "text" 43 | }, 44 | "source": [ 45 | "### Multi-head attention" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": { 51 | "colab_type": "text" 52 | }, 53 | "source": [ 54 | "### The Transformer encoder" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": { 60 | "colab_type": "text" 61 | }, 62 | "source": [ 63 | "**Getting the data**" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 0, 69 | "metadata": { 70 | "colab_type": "code" 71 | }, 72 | "outputs": [], 73 | "source": [ 74 | "!curl -O https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz\n", 75 | "!tar -xf aclImdb_v1.tar.gz\n", 76 | "!rm -r aclImdb/train/unsup" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": { 82 | "colab_type": "text" 83 | }, 84 | "source": [ 85 | "**Preparing the data**" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 0, 91 | "metadata": { 92 | "colab_type": "code" 93 | }, 94 | "outputs": [], 95 | "source": [ 96 | "import os, pathlib, shutil, random\n", 97 | "from tensorflow import keras\n", 98 | "batch_size = 32\n", 99 | "base_dir = pathlib.Path(\"aclImdb\")\n", 100 | "val_dir = base_dir / \"val\"\n", 101 | "train_dir = base_dir / \"train\"\n", 102 | "for category in (\"neg\", \"pos\"):\n", 103 | " os.makedirs(val_dir / category)\n", 104 | " files = os.listdir(train_dir / category)\n", 105 | " random.Random(1337).shuffle(files)\n", 106 | " num_val_samples = int(0.2 * len(files))\n", 107 | " val_files = files[-num_val_samples:]\n", 108 | " for fname in val_files:\n", 109 | " shutil.move(train_dir / category / fname,\n", 110 | " val_dir / category / fname)\n", 111 | "\n", 112 | "train_ds = keras.utils.text_dataset_from_directory(\n", 113 | " \"aclImdb/train\", batch_size=batch_size\n", 114 | ")\n", 115 | "val_ds = keras.utils.text_dataset_from_directory(\n", 116 | " \"aclImdb/val\", batch_size=batch_size\n", 117 | ")\n", 118 | "test_ds = keras.utils.text_dataset_from_directory(\n", 119 | " \"aclImdb/test\", batch_size=batch_size\n", 120 | ")\n", 121 | "text_only_train_ds = train_ds.map(lambda x, y: x)" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": { 127 | "colab_type": "text" 128 | }, 129 | "source": [ 130 | "**Vectorizing the data**" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 0, 136 | "metadata": { 137 | "colab_type": "code" 138 | }, 139 | "outputs": [], 140 | "source": [ 141 | "from tensorflow.keras import layers\n", 142 | "\n", 143 | "max_length = 600\n", 144 | "max_tokens = 20000\n", 145 | "text_vectorization = layers.TextVectorization(\n", 146 | " max_tokens=max_tokens,\n", 147 | " output_mode=\"int\",\n", 148 | " output_sequence_length=max_length,\n", 149 | ")\n", 150 | "text_vectorization.adapt(text_only_train_ds)\n", 151 | "\n", 152 | "int_train_ds = train_ds.map(\n", 153 | " lambda x, y: (text_vectorization(x), y),\n", 154 | " num_parallel_calls=4)\n", 155 | "int_val_ds = val_ds.map(\n", 156 | " lambda x, y: (text_vectorization(x), y),\n", 157 | " num_parallel_calls=4)\n", 158 | "int_test_ds = test_ds.map(\n", 159 | " lambda x, y: (text_vectorization(x), y),\n", 160 | " num_parallel_calls=4)" 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": { 166 | "colab_type": "text" 167 | }, 168 | "source": [ 169 | "**Transformer encoder implemented as a subclassed `Layer`**" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 0, 175 | "metadata": { 176 | "colab_type": "code" 177 | }, 178 | "outputs": [], 179 | "source": [ 180 | "import tensorflow as tf\n", 181 | "from tensorflow import keras\n", 182 | "from tensorflow.keras import layers\n", 183 | "\n", 184 | "class TransformerEncoder(layers.Layer):\n", 185 | " def __init__(self, embed_dim, dense_dim, num_heads, **kwargs):\n", 186 | " super().__init__(**kwargs)\n", 187 | " self.embed_dim = embed_dim\n", 188 | " self.dense_dim = dense_dim\n", 189 | " self.num_heads = num_heads\n", 190 | " self.attention = layers.MultiHeadAttention(\n", 191 | " num_heads=num_heads, key_dim=embed_dim)\n", 192 | " self.dense_proj = keras.Sequential(\n", 193 | " [layers.Dense(dense_dim, activation=\"relu\"),\n", 194 | " layers.Dense(embed_dim),]\n", 195 | " )\n", 196 | " self.layernorm_1 = layers.LayerNormalization()\n", 197 | " self.layernorm_2 = layers.LayerNormalization()\n", 198 | "\n", 199 | " def call(self, inputs, mask=None):\n", 200 | " if mask is not None:\n", 201 | " mask = mask[:, tf.newaxis, :]\n", 202 | " attention_output = self.attention(\n", 203 | " inputs, inputs, attention_mask=mask)\n", 204 | " proj_input = self.layernorm_1(inputs + attention_output)\n", 205 | " proj_output = self.dense_proj(proj_input)\n", 206 | " return self.layernorm_2(proj_input + proj_output)\n", 207 | "\n", 208 | " def get_config(self):\n", 209 | " config = super().get_config()\n", 210 | " config.update({\n", 211 | " \"embed_dim\": self.embed_dim,\n", 212 | " \"num_heads\": self.num_heads,\n", 213 | " \"dense_dim\": self.dense_dim,\n", 214 | " })\n", 215 | " return config" 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "metadata": { 221 | "colab_type": "text" 222 | }, 223 | "source": [ 224 | "**Using the Transformer encoder for text classification**" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": 0, 230 | "metadata": { 231 | "colab_type": "code" 232 | }, 233 | "outputs": [], 234 | "source": [ 235 | "vocab_size = 20000\n", 236 | "embed_dim = 256\n", 237 | "num_heads = 2\n", 238 | "dense_dim = 32\n", 239 | "\n", 240 | "inputs = keras.Input(shape=(None,), dtype=\"int64\")\n", 241 | "x = layers.Embedding(vocab_size, embed_dim)(inputs)\n", 242 | "x = TransformerEncoder(embed_dim, dense_dim, num_heads)(x)\n", 243 | "x = layers.GlobalMaxPooling1D()(x)\n", 244 | "x = layers.Dropout(0.5)(x)\n", 245 | "outputs = layers.Dense(1, activation=\"sigmoid\")(x)\n", 246 | "model = keras.Model(inputs, outputs)\n", 247 | "model.compile(optimizer=\"rmsprop\",\n", 248 | " loss=\"binary_crossentropy\",\n", 249 | " metrics=[\"accuracy\"])\n", 250 | "model.summary()" 251 | ] 252 | }, 253 | { 254 | "cell_type": "markdown", 255 | "metadata": { 256 | "colab_type": "text" 257 | }, 258 | "source": [ 259 | "**Training and evaluating the Transformer encoder based model**" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 0, 265 | "metadata": { 266 | "colab_type": "code" 267 | }, 268 | "outputs": [], 269 | "source": [ 270 | "callbacks = [\n", 271 | " keras.callbacks.ModelCheckpoint(\"transformer_encoder.keras\",\n", 272 | " save_best_only=True)\n", 273 | "]\n", 274 | "model.fit(int_train_ds, validation_data=int_val_ds, epochs=20, callbacks=callbacks)\n", 275 | "model = keras.models.load_model(\n", 276 | " \"transformer_encoder.keras\",\n", 277 | " custom_objects={\"TransformerEncoder\": TransformerEncoder})\n", 278 | "print(f\"Test acc: {model.evaluate(int_test_ds)[1]:.3f}\")" 279 | ] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "metadata": { 284 | "colab_type": "text" 285 | }, 286 | "source": [ 287 | "#### Using positional encoding to re-inject order information" 288 | ] 289 | }, 290 | { 291 | "cell_type": "markdown", 292 | "metadata": { 293 | "colab_type": "text" 294 | }, 295 | "source": [ 296 | "**Implementing positional embedding as a subclassed layer**" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 0, 302 | "metadata": { 303 | "colab_type": "code" 304 | }, 305 | "outputs": [], 306 | "source": [ 307 | "class PositionalEmbedding(layers.Layer):\n", 308 | " def __init__(self, sequence_length, input_dim, output_dim, **kwargs):\n", 309 | " super().__init__(**kwargs)\n", 310 | " self.token_embeddings = layers.Embedding(\n", 311 | " input_dim=input_dim, output_dim=output_dim)\n", 312 | " self.position_embeddings = layers.Embedding(\n", 313 | " input_dim=sequence_length, output_dim=output_dim)\n", 314 | " self.sequence_length = sequence_length\n", 315 | " self.input_dim = input_dim\n", 316 | " self.output_dim = output_dim\n", 317 | "\n", 318 | " def call(self, inputs):\n", 319 | " length = tf.shape(inputs)[-1]\n", 320 | " positions = tf.range(start=0, limit=length, delta=1)\n", 321 | " embedded_tokens = self.token_embeddings(inputs)\n", 322 | " embedded_positions = self.position_embeddings(positions)\n", 323 | " return embedded_tokens + embedded_positions\n", 324 | "\n", 325 | " def compute_mask(self, inputs, mask=None):\n", 326 | " return tf.math.not_equal(inputs, 0)\n", 327 | "\n", 328 | " def get_config(self):\n", 329 | " config = super().get_config()\n", 330 | " config.update({\n", 331 | " \"output_dim\": self.output_dim,\n", 332 | " \"sequence_length\": self.sequence_length,\n", 333 | " \"input_dim\": self.input_dim,\n", 334 | " })\n", 335 | " return config" 336 | ] 337 | }, 338 | { 339 | "cell_type": "markdown", 340 | "metadata": { 341 | "colab_type": "text" 342 | }, 343 | "source": [ 344 | "#### Putting it all together: A text-classification Transformer" 345 | ] 346 | }, 347 | { 348 | "cell_type": "markdown", 349 | "metadata": { 350 | "colab_type": "text" 351 | }, 352 | "source": [ 353 | "**Combining the Transformer encoder with positional embedding**" 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": 0, 359 | "metadata": { 360 | "colab_type": "code" 361 | }, 362 | "outputs": [], 363 | "source": [ 364 | "vocab_size = 20000\n", 365 | "sequence_length = 600\n", 366 | "embed_dim = 256\n", 367 | "num_heads = 2\n", 368 | "dense_dim = 32\n", 369 | "\n", 370 | "inputs = keras.Input(shape=(None,), dtype=\"int64\")\n", 371 | "x = PositionalEmbedding(sequence_length, vocab_size, embed_dim)(inputs)\n", 372 | "x = TransformerEncoder(embed_dim, dense_dim, num_heads)(x)\n", 373 | "x = layers.GlobalMaxPooling1D()(x)\n", 374 | "x = layers.Dropout(0.5)(x)\n", 375 | "outputs = layers.Dense(1, activation=\"sigmoid\")(x)\n", 376 | "model = keras.Model(inputs, outputs)\n", 377 | "model.compile(optimizer=\"rmsprop\",\n", 378 | " loss=\"binary_crossentropy\",\n", 379 | " metrics=[\"accuracy\"])\n", 380 | "model.summary()\n", 381 | "\n", 382 | "callbacks = [\n", 383 | " keras.callbacks.ModelCheckpoint(\"full_transformer_encoder.keras\",\n", 384 | " save_best_only=True)\n", 385 | "]\n", 386 | "model.fit(int_train_ds, validation_data=int_val_ds, epochs=20, callbacks=callbacks)\n", 387 | "model = keras.models.load_model(\n", 388 | " \"full_transformer_encoder.keras\",\n", 389 | " custom_objects={\"TransformerEncoder\": TransformerEncoder,\n", 390 | " \"PositionalEmbedding\": PositionalEmbedding})\n", 391 | "print(f\"Test acc: {model.evaluate(int_test_ds)[1]:.3f}\")" 392 | ] 393 | }, 394 | { 395 | "cell_type": "markdown", 396 | "metadata": { 397 | "colab_type": "text" 398 | }, 399 | "source": [ 400 | "### When to use sequence models over bag-of-words models?" 401 | ] 402 | } 403 | ], 404 | "metadata": { 405 | "colab": { 406 | "collapsed_sections": [], 407 | "name": "chapter11_part03_transformer.i", 408 | "private_outputs": false, 409 | "provenance": [], 410 | "toc_visible": true 411 | }, 412 | "kernelspec": { 413 | "display_name": "Python 3", 414 | "language": "python", 415 | "name": "python3" 416 | }, 417 | "language_info": { 418 | "codemirror_mode": { 419 | "name": "ipython", 420 | "version": 3 421 | }, 422 | "file_extension": ".py", 423 | "mimetype": "text/x-python", 424 | "name": "python", 425 | "nbconvert_exporter": "python", 426 | "pygments_lexer": "ipython3", 427 | "version": "3.7.0" 428 | } 429 | }, 430 | "nbformat": 4, 431 | "nbformat_minor": 0 432 | } -------------------------------------------------------------------------------- /chapter12_part01_text-generation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text" 7 | }, 8 | "source": [ 9 | "This is a companion notebook for the book [Deep Learning with Python, Second Edition](https://www.manning.com/books/deep-learning-with-python-second-edition?a_aid=keras&a_bid=76564dff). For readability, it only contains runnable code blocks and section titles, and omits everything else in the book: text paragraphs, figures, and pseudocode.\n\n**If you want to be able to follow what's going on, I recommend reading the notebook side by side with your copy of the book.**\n\nThis notebook was generated for TensorFlow 2.6." 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": { 15 | "colab_type": "text" 16 | }, 17 | "source": [ 18 | "# Generative deep learning" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "colab_type": "text" 25 | }, 26 | "source": [ 27 | "## Text generation" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": { 33 | "colab_type": "text" 34 | }, 35 | "source": [ 36 | "### A brief history of generative deep learning for sequence generation" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": { 42 | "colab_type": "text" 43 | }, 44 | "source": [ 45 | "### How do you generate sequence data?" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": { 51 | "colab_type": "text" 52 | }, 53 | "source": [ 54 | "### The importance of the sampling strategy" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": { 60 | "colab_type": "text" 61 | }, 62 | "source": [ 63 | "**Reweighting a probability distribution to a different temperature**" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 0, 69 | "metadata": { 70 | "colab_type": "code" 71 | }, 72 | "outputs": [], 73 | "source": [ 74 | "import numpy as np\n", 75 | "def reweight_distribution(original_distribution, temperature=0.5):\n", 76 | " distribution = np.log(original_distribution) / temperature\n", 77 | " distribution = np.exp(distribution)\n", 78 | " return distribution / np.sum(distribution)" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": { 84 | "colab_type": "text" 85 | }, 86 | "source": [ 87 | "### Implementing text generation with Keras" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": { 93 | "colab_type": "text" 94 | }, 95 | "source": [ 96 | "#### Preparing the data" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": { 102 | "colab_type": "text" 103 | }, 104 | "source": [ 105 | "**Downloading and uncompressing the IMDB movie reviews dataset**" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 0, 111 | "metadata": { 112 | "colab_type": "code" 113 | }, 114 | "outputs": [], 115 | "source": [ 116 | "!wget https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz\n", 117 | "!tar -xf aclImdb_v1.tar.gz" 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": { 123 | "colab_type": "text" 124 | }, 125 | "source": [ 126 | "**Creating a dataset from text files (one file = one sample)**" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 0, 132 | "metadata": { 133 | "colab_type": "code" 134 | }, 135 | "outputs": [], 136 | "source": [ 137 | "import tensorflow as tf\n", 138 | "from tensorflow import keras\n", 139 | "dataset = keras.utils.text_dataset_from_directory(\n", 140 | " directory=\"aclImdb\", label_mode=None, batch_size=256)\n", 141 | "dataset = dataset.map(lambda x: tf.strings.regex_replace(x, \"
\", \" \"))" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": { 147 | "colab_type": "text" 148 | }, 149 | "source": [ 150 | "**Preparing a `TextVectorization` layer**" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 0, 156 | "metadata": { 157 | "colab_type": "code" 158 | }, 159 | "outputs": [], 160 | "source": [ 161 | "from tensorflow.keras.layers import TextVectorization\n", 162 | "\n", 163 | "sequence_length = 100\n", 164 | "vocab_size = 15000\n", 165 | "text_vectorization = TextVectorization(\n", 166 | " max_tokens=vocab_size,\n", 167 | " output_mode=\"int\",\n", 168 | " output_sequence_length=sequence_length,\n", 169 | ")\n", 170 | "text_vectorization.adapt(dataset)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": { 176 | "colab_type": "text" 177 | }, 178 | "source": [ 179 | "**Setting up a language modeling dataset**" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 0, 185 | "metadata": { 186 | "colab_type": "code" 187 | }, 188 | "outputs": [], 189 | "source": [ 190 | "def prepare_lm_dataset(text_batch):\n", 191 | " vectorized_sequences = text_vectorization(text_batch)\n", 192 | " x = vectorized_sequences[:, :-1]\n", 193 | " y = vectorized_sequences[:, 1:]\n", 194 | " return x, y\n", 195 | "\n", 196 | "lm_dataset = dataset.map(prepare_lm_dataset, num_parallel_calls=4)" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": { 202 | "colab_type": "text" 203 | }, 204 | "source": [ 205 | "#### A Transformer-based sequence-to-sequence model" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 0, 211 | "metadata": { 212 | "colab_type": "code" 213 | }, 214 | "outputs": [], 215 | "source": [ 216 | "import tensorflow as tf\n", 217 | "from tensorflow.keras import layers\n", 218 | "\n", 219 | "class PositionalEmbedding(layers.Layer):\n", 220 | " def __init__(self, sequence_length, input_dim, output_dim, **kwargs):\n", 221 | " super().__init__(**kwargs)\n", 222 | " self.token_embeddings = layers.Embedding(\n", 223 | " input_dim=input_dim, output_dim=output_dim)\n", 224 | " self.position_embeddings = layers.Embedding(\n", 225 | " input_dim=sequence_length, output_dim=output_dim)\n", 226 | " self.sequence_length = sequence_length\n", 227 | " self.input_dim = input_dim\n", 228 | " self.output_dim = output_dim\n", 229 | "\n", 230 | " def call(self, inputs):\n", 231 | " length = tf.shape(inputs)[-1]\n", 232 | " positions = tf.range(start=0, limit=length, delta=1)\n", 233 | " embedded_tokens = self.token_embeddings(inputs)\n", 234 | " embedded_positions = self.position_embeddings(positions)\n", 235 | " return embedded_tokens + embedded_positions\n", 236 | "\n", 237 | " def compute_mask(self, inputs, mask=None):\n", 238 | " return tf.math.not_equal(inputs, 0)\n", 239 | "\n", 240 | " def get_config(self):\n", 241 | " config = super(PositionalEmbedding, self).get_config()\n", 242 | " config.update({\n", 243 | " \"output_dim\": self.output_dim,\n", 244 | " \"sequence_length\": self.sequence_length,\n", 245 | " \"input_dim\": self.input_dim,\n", 246 | " })\n", 247 | " return config\n", 248 | "\n", 249 | "\n", 250 | "class TransformerDecoder(layers.Layer):\n", 251 | " def __init__(self, embed_dim, dense_dim, num_heads, **kwargs):\n", 252 | " super().__init__(**kwargs)\n", 253 | " self.embed_dim = embed_dim\n", 254 | " self.dense_dim = dense_dim\n", 255 | " self.num_heads = num_heads\n", 256 | " self.attention_1 = layers.MultiHeadAttention(\n", 257 | " num_heads=num_heads, key_dim=embed_dim)\n", 258 | " self.attention_2 = layers.MultiHeadAttention(\n", 259 | " num_heads=num_heads, key_dim=embed_dim)\n", 260 | " self.dense_proj = keras.Sequential(\n", 261 | " [layers.Dense(dense_dim, activation=\"relu\"),\n", 262 | " layers.Dense(embed_dim),]\n", 263 | " )\n", 264 | " self.layernorm_1 = layers.LayerNormalization()\n", 265 | " self.layernorm_2 = layers.LayerNormalization()\n", 266 | " self.layernorm_3 = layers.LayerNormalization()\n", 267 | " self.supports_masking = True\n", 268 | "\n", 269 | " def get_config(self):\n", 270 | " config = super(TransformerDecoder, self).get_config()\n", 271 | " config.update({\n", 272 | " \"embed_dim\": self.embed_dim,\n", 273 | " \"num_heads\": self.num_heads,\n", 274 | " \"dense_dim\": self.dense_dim,\n", 275 | " })\n", 276 | " return config\n", 277 | "\n", 278 | " def get_causal_attention_mask(self, inputs):\n", 279 | " input_shape = tf.shape(inputs)\n", 280 | " batch_size, sequence_length = input_shape[0], input_shape[1]\n", 281 | " i = tf.range(sequence_length)[:, tf.newaxis]\n", 282 | " j = tf.range(sequence_length)\n", 283 | " mask = tf.cast(i >= j, dtype=\"int32\")\n", 284 | " mask = tf.reshape(mask, (1, input_shape[1], input_shape[1]))\n", 285 | " mult = tf.concat(\n", 286 | " [tf.expand_dims(batch_size, -1),\n", 287 | " tf.constant([1, 1], dtype=tf.int32)], axis=0)\n", 288 | " return tf.tile(mask, mult)\n", 289 | "\n", 290 | " def call(self, inputs, encoder_outputs, mask=None):\n", 291 | " causal_mask = self.get_causal_attention_mask(inputs)\n", 292 | " if mask is not None:\n", 293 | " padding_mask = tf.cast(\n", 294 | " mask[:, tf.newaxis, :], dtype=\"int32\")\n", 295 | " padding_mask = tf.minimum(padding_mask, causal_mask)\n", 296 | " attention_output_1 = self.attention_1(\n", 297 | " query=inputs,\n", 298 | " value=inputs,\n", 299 | " key=inputs,\n", 300 | " attention_mask=causal_mask)\n", 301 | " attention_output_1 = self.layernorm_1(inputs + attention_output_1)\n", 302 | " attention_output_2 = self.attention_2(\n", 303 | " query=attention_output_1,\n", 304 | " value=encoder_outputs,\n", 305 | " key=encoder_outputs,\n", 306 | " attention_mask=padding_mask,\n", 307 | " )\n", 308 | " attention_output_2 = self.layernorm_2(\n", 309 | " attention_output_1 + attention_output_2)\n", 310 | " proj_output = self.dense_proj(attention_output_2)\n", 311 | " return self.layernorm_3(attention_output_2 + proj_output)" 312 | ] 313 | }, 314 | { 315 | "cell_type": "markdown", 316 | "metadata": { 317 | "colab_type": "text" 318 | }, 319 | "source": [ 320 | "**A simple Transformer-based language model**" 321 | ] 322 | }, 323 | { 324 | "cell_type": "code", 325 | "execution_count": 0, 326 | "metadata": { 327 | "colab_type": "code" 328 | }, 329 | "outputs": [], 330 | "source": [ 331 | "from tensorflow.keras import layers\n", 332 | "embed_dim = 256\n", 333 | "latent_dim = 2048\n", 334 | "num_heads = 2\n", 335 | "\n", 336 | "inputs = keras.Input(shape=(None,), dtype=\"int64\")\n", 337 | "x = PositionalEmbedding(sequence_length, vocab_size, embed_dim)(inputs)\n", 338 | "x = TransformerDecoder(embed_dim, latent_dim, num_heads)(x, x)\n", 339 | "outputs = layers.Dense(vocab_size, activation=\"softmax\")(x)\n", 340 | "model = keras.Model(inputs, outputs)\n", 341 | "model.compile(loss=\"sparse_categorical_crossentropy\", optimizer=\"rmsprop\")" 342 | ] 343 | }, 344 | { 345 | "cell_type": "markdown", 346 | "metadata": { 347 | "colab_type": "text" 348 | }, 349 | "source": [ 350 | "### A text-generation callback with variable-temperature sampling" 351 | ] 352 | }, 353 | { 354 | "cell_type": "markdown", 355 | "metadata": { 356 | "colab_type": "text" 357 | }, 358 | "source": [ 359 | "**The text-generation callback**" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": 0, 365 | "metadata": { 366 | "colab_type": "code" 367 | }, 368 | "outputs": [], 369 | "source": [ 370 | "import numpy as np\n", 371 | "\n", 372 | "tokens_index = dict(enumerate(text_vectorization.get_vocabulary()))\n", 373 | "\n", 374 | "def sample_next(predictions, temperature=1.0):\n", 375 | " predictions = np.asarray(predictions).astype(\"float64\")\n", 376 | " predictions = np.log(predictions) / temperature\n", 377 | " exp_preds = np.exp(predictions)\n", 378 | " predictions = exp_preds / np.sum(exp_preds)\n", 379 | " probas = np.random.multinomial(1, predictions, 1)\n", 380 | " return np.argmax(probas)\n", 381 | "\n", 382 | "class TextGenerator(keras.callbacks.Callback):\n", 383 | " def __init__(self,\n", 384 | " prompt,\n", 385 | " generate_length,\n", 386 | " model_input_length,\n", 387 | " temperatures=(1.,),\n", 388 | " print_freq=1):\n", 389 | " self.prompt = prompt\n", 390 | " self.generate_length = generate_length\n", 391 | " self.model_input_length = model_input_length\n", 392 | " self.temperatures = temperatures\n", 393 | " self.print_freq = print_freq\n", 394 | "\n", 395 | " def on_epoch_end(self, epoch, logs=None):\n", 396 | " if (epoch + 1) % self.print_freq != 0:\n", 397 | " return\n", 398 | " for temperature in self.temperatures:\n", 399 | " print(\"== Generating with temperature\", temperature)\n", 400 | " sentence = self.prompt\n", 401 | " for i in range(self.generate_length):\n", 402 | " tokenized_sentence = text_vectorization([sentence])\n", 403 | " predictions = self.model(tokenized_sentence)\n", 404 | " next_token = sample_next(predictions[0, i, :])\n", 405 | " sampled_token = tokens_index[next_token]\n", 406 | " sentence += \" \" + sampled_token\n", 407 | " print(sentence)\n", 408 | "\n", 409 | "prompt = \"This movie\"\n", 410 | "text_gen_callback = TextGenerator(\n", 411 | " prompt,\n", 412 | " generate_length=50,\n", 413 | " model_input_length=sequence_length,\n", 414 | " temperatures=(0.2, 0.5, 0.7, 1., 1.5))" 415 | ] 416 | }, 417 | { 418 | "cell_type": "markdown", 419 | "metadata": { 420 | "colab_type": "text" 421 | }, 422 | "source": [ 423 | "**Fitting the language model**" 424 | ] 425 | }, 426 | { 427 | "cell_type": "code", 428 | "execution_count": 0, 429 | "metadata": { 430 | "colab_type": "code" 431 | }, 432 | "outputs": [], 433 | "source": [ 434 | "model.fit(lm_dataset, epochs=200, callbacks=[text_gen_callback])" 435 | ] 436 | }, 437 | { 438 | "cell_type": "markdown", 439 | "metadata": { 440 | "colab_type": "text" 441 | }, 442 | "source": [ 443 | "### Wrapping up" 444 | ] 445 | } 446 | ], 447 | "metadata": { 448 | "colab": { 449 | "collapsed_sections": [], 450 | "name": "chapter12_part01_text-generation.i", 451 | "private_outputs": false, 452 | "provenance": [], 453 | "toc_visible": true 454 | }, 455 | "kernelspec": { 456 | "display_name": "Python 3", 457 | "language": "python", 458 | "name": "python3" 459 | }, 460 | "language_info": { 461 | "codemirror_mode": { 462 | "name": "ipython", 463 | "version": 3 464 | }, 465 | "file_extension": ".py", 466 | "mimetype": "text/x-python", 467 | "name": "python", 468 | "nbconvert_exporter": "python", 469 | "pygments_lexer": "ipython3", 470 | "version": "3.7.0" 471 | } 472 | }, 473 | "nbformat": 4, 474 | "nbformat_minor": 0 475 | } -------------------------------------------------------------------------------- /chapter14_conclusions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text" 7 | }, 8 | "source": [ 9 | "This is a companion notebook for the book [Deep Learning with Python, Second Edition](https://www.manning.com/books/deep-learning-with-python-second-edition?a_aid=keras&a_bid=76564dff). For readability, it only contains runnable code blocks and section titles, and omits everything else in the book: text paragraphs, figures, and pseudocode.\n\n**If you want to be able to follow what's going on, I recommend reading the notebook side by side with your copy of the book.**\n\nThis notebook was generated for TensorFlow 2.6." 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": { 15 | "colab_type": "text" 16 | }, 17 | "source": [ 18 | "# Conclusions" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "colab_type": "text" 25 | }, 26 | "source": [ 27 | "## Key concepts in review" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": { 33 | "colab_type": "text" 34 | }, 35 | "source": [ 36 | "### Various approaches to AI" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": { 42 | "colab_type": "text" 43 | }, 44 | "source": [ 45 | "### What makes deep learning special within the field of machine learning" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": { 51 | "colab_type": "text" 52 | }, 53 | "source": [ 54 | "### How to think about deep learning" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": { 60 | "colab_type": "text" 61 | }, 62 | "source": [ 63 | "### Key enabling technologies" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": { 69 | "colab_type": "text" 70 | }, 71 | "source": [ 72 | "### The universal machine-learning workflow" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": { 78 | "colab_type": "text" 79 | }, 80 | "source": [ 81 | "### Key network architectures" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": { 87 | "colab_type": "text" 88 | }, 89 | "source": [ 90 | "#### Densely connected networks" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 0, 96 | "metadata": { 97 | "colab_type": "code" 98 | }, 99 | "outputs": [], 100 | "source": [ 101 | "from tensorflow import keras\n", 102 | "from tensorflow.keras\u00a0import\u00a0layers\n", 103 | "inputs = keras.Input(shape=(num_input_features,))\n", 104 | "x = layers.Dense(32,\u00a0activation=\"relu\")(inputs)\n", 105 | "x = layers.Dense(32,\u00a0activation=\"relu\")(x)\n", 106 | "outputs = layers.Dense(1,\u00a0activation=\"sigmoid\")(x)\n", 107 | "model = keras.Model(inputs, outputs)\n", 108 | "model.compile(optimizer=\"rmsprop\",\u00a0loss=\"binary_crossentropy\")" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 0, 114 | "metadata": { 115 | "colab_type": "code" 116 | }, 117 | "outputs": [], 118 | "source": [ 119 | "inputs = keras.Input(shape=(num_input_features,))\n", 120 | "x = layers.Dense(32,\u00a0activation=\"relu\")(inputs)\n", 121 | "x = layers.Dense(32,\u00a0activation=\"relu\")(x)\n", 122 | "outputs = layers.Dense(num_classes,\u00a0activation=\"softmax\")(x)\n", 123 | "model = keras.Model(inputs, outputs)\n", 124 | "model.compile(optimizer=\"rmsprop\",\u00a0loss=\"categorical_crossentropy\")" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 0, 130 | "metadata": { 131 | "colab_type": "code" 132 | }, 133 | "outputs": [], 134 | "source": [ 135 | "inputs = keras.Input(shape=(num_input_features,))\n", 136 | "x = layers.Dense(32,\u00a0activation=\"relu\")(inputs)\n", 137 | "x = layers.Dense(32,\u00a0activation=\"relu\")(x)\n", 138 | "outputs = layers.Dense(num_classes,\u00a0activation=\"sigmoid\")(x)\n", 139 | "model = keras.Model(inputs, outputs)\n", 140 | "model.compile(optimizer=\"rmsprop\",\u00a0loss=\"binary_crossentropy\")" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 0, 146 | "metadata": { 147 | "colab_type": "code" 148 | }, 149 | "outputs": [], 150 | "source": [ 151 | "inputs = keras.Input(shape=(num_input_features,))\n", 152 | "x = layers.Dense(32,\u00a0activation=\"relu\")(inputs)\n", 153 | "x = layers.Dense(32,\u00a0activation=\"relu\")(x)\n", 154 | "outputs layers.Dense(num_values)(x)\n", 155 | "model = keras.Model(inputs, outputs)\n", 156 | "model.compile(optimizer=\"rmsprop\",\u00a0loss=\"mse\")" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": { 162 | "colab_type": "text" 163 | }, 164 | "source": [ 165 | "#### Convnets" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 0, 171 | "metadata": { 172 | "colab_type": "code" 173 | }, 174 | "outputs": [], 175 | "source": [ 176 | "inputs = keras.Input(shape=(height,\u00a0width,\u00a0channels))\n", 177 | "x = layers.SeparableConv2D(32,\u00a03,\u00a0activation=\"relu\")(inputs)\n", 178 | "x = layers.SeparableConv2D(64,\u00a03,\u00a0activation=\"relu\")(x)\n", 179 | "x = layers.MaxPooling2D(2)(x)\n", 180 | "x = layers.SeparableConv2D(64,\u00a03,\u00a0activation=\"relu\")(x)\n", 181 | "x = layers.SeparableConv2D(128,\u00a03,\u00a0activation=\"relu\")(x)\n", 182 | "x = layers.MaxPooling2D(2)(x)\n", 183 | "x = layers.SeparableConv2D(64,\u00a03,\u00a0activation=\"relu\")(x)\n", 184 | "x = layers.SeparableConv2D(128,\u00a03,\u00a0activation=\"relu\")(x)\n", 185 | "x = layers.GlobalAveragePooling2D()(x)\n", 186 | "x = layers.Dense(32,\u00a0activation=\"relu\")(x)\n", 187 | "outputs = layers.Dense(num_classes,\u00a0activation=\"softmax\")(x)\n", 188 | "model = keras.Model(inputs, outputs)\n", 189 | "model.compile(optimizer=\"rmsprop\",\u00a0loss=\"categorical_crossentropy\")" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": { 195 | "colab_type": "text" 196 | }, 197 | "source": [ 198 | "#### RNNs" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 0, 204 | "metadata": { 205 | "colab_type": "code" 206 | }, 207 | "outputs": [], 208 | "source": [ 209 | "inputs = keras.Input(shape=(num_timesteps,\u00a0num_features))\n", 210 | "x = layers.LSTM(32)(inputs)\n", 211 | "outputs = layers.Dense(num_classes,\u00a0activation=\"sigmoid\")(x)\n", 212 | "model = keras.Model(inputs, outputs)\n", 213 | "model.compile(optimizer=\"rmsprop\",\u00a0loss=\"binary_crossentropy\")" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 0, 219 | "metadata": { 220 | "colab_type": "code" 221 | }, 222 | "outputs": [], 223 | "source": [ 224 | "inputs = keras.Input(shape=(num_timesteps,\u00a0num_features))\n", 225 | "x = layers.LSTM(32,\u00a0return_sequences=True)(inputs)\n", 226 | "x = layers.LSTM(32,\u00a0return_sequences=True)(x)\n", 227 | "x = layers.LSTM(32)(x)\n", 228 | "outputs = layers.Dense(num_classes,\u00a0activation=\"sigmoid\")(x)\n", 229 | "model = keras.Model(inputs, outputs)\n", 230 | "model.compile(optimizer=\"rmsprop\",\u00a0loss=\"binary_crossentropy\")" 231 | ] 232 | }, 233 | { 234 | "cell_type": "markdown", 235 | "metadata": { 236 | "colab_type": "text" 237 | }, 238 | "source": [ 239 | "#### Transformers" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 0, 245 | "metadata": { 246 | "colab_type": "code" 247 | }, 248 | "outputs": [], 249 | "source": [ 250 | "encoder_inputs = keras.Input(shape=(sequence_length,), dtype=\"int64\")\n", 251 | "x = PositionalEmbedding(sequence_length, vocab_size, embed_dim)(encoder_inputs)\n", 252 | "encoder_outputs = TransformerEncoder(embed_dim, dense_dim, num_heads)(x)\n", 253 | "decoder_inputs = keras.Input(shape=(None,), dtype=\"int64\")\n", 254 | "x = PositionalEmbedding(sequence_length, vocab_size, embed_dim)(decoder_inputs)\n", 255 | "x = TransformerDecoder(embed_dim, dense_dim, num_heads)(x, encoder_outputs)\n", 256 | "decoder_outputs = layers.Dense(vocab_size, activation=\"softmax\")(x)\n", 257 | "transformer = keras.Model([encoder_inputs, decoder_inputs], decoder_outputs)\n", 258 | "transformer.compile(optimizer=\"rmsprop\", loss=\"categorical_crossentropy\")" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 0, 264 | "metadata": { 265 | "colab_type": "code" 266 | }, 267 | "outputs": [], 268 | "source": [ 269 | "inputs = keras.Input(shape=(sequence_length,), dtype=\"int64\")\n", 270 | "x = PositionalEmbedding(sequence_length, vocab_size, embed_dim)(inputs)\n", 271 | "x = TransformerEncoder(embed_dim, dense_dim, num_heads)(x)\n", 272 | "x = layers.GlobalMaxPooling1D()(x)\n", 273 | "outputs = layers.Dense(1, activation=\"sigmoid\")(x)\n", 274 | "model = keras.Model(inputs, outputs)\n", 275 | "model.compile(optimizer=\"rmsprop\", loss=\"binary_crossentropy\")" 276 | ] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": { 281 | "colab_type": "text" 282 | }, 283 | "source": [ 284 | "### The space of possibilities" 285 | ] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "metadata": { 290 | "colab_type": "text" 291 | }, 292 | "source": [ 293 | "## The limitations of deep learning" 294 | ] 295 | }, 296 | { 297 | "cell_type": "markdown", 298 | "metadata": { 299 | "colab_type": "text" 300 | }, 301 | "source": [ 302 | "### The risk of anthropomorphizing machine-learning models" 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "metadata": { 308 | "colab_type": "text" 309 | }, 310 | "source": [ 311 | "### Automatons vs. intelligent agents" 312 | ] 313 | }, 314 | { 315 | "cell_type": "markdown", 316 | "metadata": { 317 | "colab_type": "text" 318 | }, 319 | "source": [ 320 | "### Local generalization vs. extreme generalization" 321 | ] 322 | }, 323 | { 324 | "cell_type": "markdown", 325 | "metadata": { 326 | "colab_type": "text" 327 | }, 328 | "source": [ 329 | "### The purpose of intelligence" 330 | ] 331 | }, 332 | { 333 | "cell_type": "markdown", 334 | "metadata": { 335 | "colab_type": "text" 336 | }, 337 | "source": [ 338 | "### Climbing the spectrum of generalization" 339 | ] 340 | }, 341 | { 342 | "cell_type": "markdown", 343 | "metadata": { 344 | "colab_type": "text" 345 | }, 346 | "source": [ 347 | "## Setting the course toward greater generality in AI" 348 | ] 349 | }, 350 | { 351 | "cell_type": "markdown", 352 | "metadata": { 353 | "colab_type": "text" 354 | }, 355 | "source": [ 356 | "### On the importance of setting the right objective: The shortcut rule" 357 | ] 358 | }, 359 | { 360 | "cell_type": "markdown", 361 | "metadata": { 362 | "colab_type": "text" 363 | }, 364 | "source": [ 365 | "### A new target" 366 | ] 367 | }, 368 | { 369 | "cell_type": "markdown", 370 | "metadata": { 371 | "colab_type": "text" 372 | }, 373 | "source": [ 374 | "## Implementing intelligence: The missing ingredients" 375 | ] 376 | }, 377 | { 378 | "cell_type": "markdown", 379 | "metadata": { 380 | "colab_type": "text" 381 | }, 382 | "source": [ 383 | "### Intelligence as sensitivity to abstract analogies" 384 | ] 385 | }, 386 | { 387 | "cell_type": "markdown", 388 | "metadata": { 389 | "colab_type": "text" 390 | }, 391 | "source": [ 392 | "### The two poles of abstraction" 393 | ] 394 | }, 395 | { 396 | "cell_type": "markdown", 397 | "metadata": { 398 | "colab_type": "text" 399 | }, 400 | "source": [ 401 | "#### Value-centric analogy" 402 | ] 403 | }, 404 | { 405 | "cell_type": "markdown", 406 | "metadata": { 407 | "colab_type": "text" 408 | }, 409 | "source": [ 410 | "#### Program-centric analogy" 411 | ] 412 | }, 413 | { 414 | "cell_type": "markdown", 415 | "metadata": { 416 | "colab_type": "text" 417 | }, 418 | "source": [ 419 | "#### Cognition as a combination of both kinds of abstraction" 420 | ] 421 | }, 422 | { 423 | "cell_type": "markdown", 424 | "metadata": { 425 | "colab_type": "text" 426 | }, 427 | "source": [ 428 | "### The missing half of the picture" 429 | ] 430 | }, 431 | { 432 | "cell_type": "markdown", 433 | "metadata": { 434 | "colab_type": "text" 435 | }, 436 | "source": [ 437 | "## The future of deep learning" 438 | ] 439 | }, 440 | { 441 | "cell_type": "markdown", 442 | "metadata": { 443 | "colab_type": "text" 444 | }, 445 | "source": [ 446 | "### Models as programs" 447 | ] 448 | }, 449 | { 450 | "cell_type": "markdown", 451 | "metadata": { 452 | "colab_type": "text" 453 | }, 454 | "source": [ 455 | "### Blending together deep learning and program synthesis" 456 | ] 457 | }, 458 | { 459 | "cell_type": "markdown", 460 | "metadata": { 461 | "colab_type": "text" 462 | }, 463 | "source": [ 464 | "#### Integrating deep-learning modules and algorithmic modules into hybrid systems" 465 | ] 466 | }, 467 | { 468 | "cell_type": "markdown", 469 | "metadata": { 470 | "colab_type": "text" 471 | }, 472 | "source": [ 473 | "#### Using deep learning to guide program search" 474 | ] 475 | }, 476 | { 477 | "cell_type": "markdown", 478 | "metadata": { 479 | "colab_type": "text" 480 | }, 481 | "source": [ 482 | "### Lifelong learning and modular subroutine reuse" 483 | ] 484 | }, 485 | { 486 | "cell_type": "markdown", 487 | "metadata": { 488 | "colab_type": "text" 489 | }, 490 | "source": [ 491 | "### The long-term vision" 492 | ] 493 | }, 494 | { 495 | "cell_type": "markdown", 496 | "metadata": { 497 | "colab_type": "text" 498 | }, 499 | "source": [ 500 | "## Staying up to date in a fast-moving field" 501 | ] 502 | }, 503 | { 504 | "cell_type": "markdown", 505 | "metadata": { 506 | "colab_type": "text" 507 | }, 508 | "source": [ 509 | "### Practice on real-world problems using Kaggle" 510 | ] 511 | }, 512 | { 513 | "cell_type": "markdown", 514 | "metadata": { 515 | "colab_type": "text" 516 | }, 517 | "source": [ 518 | "### Read about the latest developments on arXiv" 519 | ] 520 | }, 521 | { 522 | "cell_type": "markdown", 523 | "metadata": { 524 | "colab_type": "text" 525 | }, 526 | "source": [ 527 | "### Explore the Keras ecosystem" 528 | ] 529 | }, 530 | { 531 | "cell_type": "markdown", 532 | "metadata": { 533 | "colab_type": "text" 534 | }, 535 | "source": [ 536 | "## Final words" 537 | ] 538 | } 539 | ], 540 | "metadata": { 541 | "colab": { 542 | "collapsed_sections": [], 543 | "name": "chapter14_conclusions.i", 544 | "private_outputs": false, 545 | "provenance": [], 546 | "toc_visible": true 547 | }, 548 | "kernelspec": { 549 | "display_name": "Python 3", 550 | "language": "python", 551 | "name": "python3" 552 | }, 553 | "language_info": { 554 | "codemirror_mode": { 555 | "name": "ipython", 556 | "version": 3 557 | }, 558 | "file_extension": ".py", 559 | "mimetype": "text/x-python", 560 | "name": "python", 561 | "nbconvert_exporter": "python", 562 | "pygments_lexer": "ipython3", 563 | "version": "3.7.0" 564 | } 565 | }, 566 | "nbformat": 4, 567 | "nbformat_minor": 0 568 | } -------------------------------------------------------------------------------- /data/120-years-of-olympic-history-athletes-and-results/noc_regions.csv: -------------------------------------------------------------------------------- 1 | NOC,region,notes AFG,Afghanistan, AHO,Curacao,Netherlands Antilles ALB,Albania, ALG,Algeria, AND,Andorra, ANG,Angola, ANT,Antigua,Antigua and Barbuda ANZ,Australia,Australasia ARG,Argentina, ARM,Armenia, ARU,Aruba, ASA,American Samoa, AUS,Australia, AUT,Austria, AZE,Azerbaijan, BAH,Bahamas, BAN,Bangladesh, BAR,Barbados, BDI,Burundi, BEL,Belgium, BEN,Benin, BER,Bermuda, BHU,Bhutan, BIH,Bosnia and Herzegovina, BIZ,Belize, BLR,Belarus, BOH,Czech Republic,Bohemia BOL,Boliva, BOT,Botswana, BRA,Brazil, BRN,Bahrain, BRU,Brunei, BUL,Bulgaria, BUR,Burkina Faso, CAF,Central African Republic, CAM,Cambodia, CAN,Canada, CAY,Cayman Islands, CGO,Republic of Congo, CHA,Chad, CHI,Chile, CHN,China, CIV,Ivory Coast, CMR,Cameroon, COD,Democratic Republic of the Congo, COK,Cook Islands, COL,Colombia, COM,Comoros, CPV,Cape Verde, CRC,Costa Rica, CRO,Croatia, CRT,Greece,Crete CUB,Cuba, CYP,Cyprus, CZE,Czech Republic, DEN,Denmark, DJI,Djibouti, DMA,Dominica, DOM,Dominican Republic, ECU,Ecuador, EGY,Egypt, ERI,Eritrea, ESA,El Salvador, ESP,Spain, EST,Estonia, ETH,Ethiopia, EUN,Russia, FIJ,Fiji, FIN,Finland, FRA,France, FRG,Germany, FSM,Micronesia, GAB,Gabon, GAM,Gambia, GBR,UK, GBS,Guinea-Bissau, GDR,Germany, GEO,Georgia, GEQ,Equatorial Guinea, GER,Germany, GHA,Ghana, GRE,Greece, GRN,Grenada, GUA,Guatemala, GUI,Guinea, GUM,Guam, GUY,Guyana, HAI,Haiti, HKG,China,Hong Kong HON,Honduras, HUN,Hungary, INA,Indonesia, IND,India, IOA,Individual Olympic Athletes,Individual Olympic Athletes IRI,Iran, IRL,Ireland, IRQ,Iraq, ISL,Iceland, ISR,Israel, ISV,"Virgin Islands, US",Virgin Islands ITA,Italy, IVB,"Virgin Islands, British", JAM,Jamaica, JOR,Jordan, JPN,Japan, KAZ,Kazakhstan, KEN,Kenya, KGZ,Kyrgyzstan, KIR,Kiribati, KOR,South Korea, KOS,Kosovo, KSA,Saudi Arabia, KUW,Kuwait, LAO,Laos, LAT,Latvia, LBA,Libya, LBR,Liberia, LCA,Saint Lucia, LES,Lesotho, LIB,Lebanon, LIE,Liechtenstein, LTU,Lithuania, LUX,Luxembourg, MAD,Madagascar, MAL,Malaysia, MAR,Morocco, MAS,Malaysia, MAW,Malawi, MDA,Moldova, MDV,Maldives, MEX,Mexico, MGL,Mongolia, MHL,Marshall Islands, MKD,Macedonia, MLI,Mali, MLT,Malta, MNE,Montenegro, MON,Monaco, MOZ,Mozambique, MRI,Mauritius, MTN,Mauritania, MYA,Myanmar, NAM,Namibia, NBO,Malaysia,North Borneo NCA,Nicaragua, NED,Netherlands, NEP,Nepal, NFL,Canada,Newfoundland NGR,Nigeria, NIG,Niger, NOR,Norway, NRU,Nauru, NZL,New Zealand, OMA,Oman, PAK,Pakistan, PAN,Panama, PAR,Paraguay, PER,Peru, PHI,Philippines, PLE,Palestine, PLW,Palau, PNG,Papua New Guinea, POL,Poland, POR,Portugal, PRK,North Korea, PUR,Puerto Rico, QAT,Qatar, RHO,Zimbabwe, ROT,NA,Refugee Olympic Team ROU,Romania, RSA,South Africa, RUS,Russia, RWA,Rwanda, SAA,Germany, SAM,Samoa, SCG,Serbia,Serbia and Montenegro SEN,Senegal, SEY,Seychelles, SIN,Singapore, SKN,Saint Kitts,Turks and Caicos Islands SLE,Sierra Leone, SLO,Slovenia, SMR,San Marino, SOL,Solomon Islands, SOM,Somalia, SRB,Serbia, SRI,Sri Lanka, SSD,South Sudan, STP,Sao Tome and Principe, SUD,Sudan, SUI,Switzerland, SUR,Suriname, SVK,Slovakia, SWE,Sweden, SWZ,Swaziland, SYR,Syria, TAN,Tanzania, TCH,Czech Republic, TGA,Tonga, THA,Thailand, TJK,Tajikistan, TKM,Turkmenistan, TLS,Timor-Leste, TOG,Togo, TPE,Taiwan, TTO,Trinidad,Trinidad and Tobago TUN,Tunisia, TUR,Turkey, TUV,NA,Tuvalu UAE,United Arab Emirates, UAR,Syria,United Arab Republic UGA,Uganda, UKR,Ukraine, UNK,NA,Unknown URS,Russia, URU,Uruguay, USA,USA, UZB,Uzbekistan, VAN,Vanuatu, VEN,Venezuela, VIE,Vietnam, VIN,Saint Vincent, VNM,Vietnam, WIF,Trinidad,West Indies Federation YAR,Yemen,North Yemen YEM,Yemen, YMD,Yemen,South Yemen YUG,Serbia,Yugoslavia ZAM,Zambia, ZIM,Zimbabwe, -------------------------------------------------------------------------------- /data/GOT-battles.csv: -------------------------------------------------------------------------------- 1 | name,year,battle_number,attacker_king,defender_king,attacker_1,attacker_2,attacker_3,attacker_4,defender_1,defender_2,defender_3,defender_4,attacker_outcome,battle_type,major_death,major_capture,attacker_size,defender_size,attacker_commander,defender_commander,summer,location,region,note Battle of the Golden Tooth,298,1,Joffrey/Tommen Baratheon,Robb Stark,Lannister,,,,Tully,,,,win,pitched battle,1,0,15000,4000,Jaime Lannister,"Clement Piper, Vance",1,Golden Tooth,The Westerlands, Battle at the Mummer's Ford,298,2,Joffrey/Tommen Baratheon,Robb Stark,Lannister,,,,Baratheon,,,,win,ambush,1,0,,120,Gregor Clegane,Beric Dondarrion,1,Mummer's Ford,The Riverlands, Battle of Riverrun,298,3,Joffrey/Tommen Baratheon,Robb Stark,Lannister,,,,Tully,,,,win,pitched battle,0,1,15000,10000,"Jaime Lannister, Andros Brax","Edmure Tully, Tytos Blackwood",1,Riverrun,The Riverlands, Battle of the Green Fork,298,4,Robb Stark,Joffrey/Tommen Baratheon,Stark,,,,Lannister,,,,loss,pitched battle,1,1,18000,20000,"Roose Bolton, Wylis Manderly, Medger Cerwyn, Harrion Karstark, Halys Hornwood","Tywin Lannister, Gregor Clegane, Kevan Lannister, Addam Marbrand",1,Green Fork,The Riverlands, Battle of the Whispering Wood,298,5,Robb Stark,Joffrey/Tommen Baratheon,Stark,Tully,,,Lannister,,,,win,ambush,1,1,1875,6000,"Robb Stark, Brynden Tully",Jaime Lannister,1,Whispering Wood,The Riverlands, Battle of the Camps,298,6,Robb Stark,Joffrey/Tommen Baratheon,Stark,Tully,,,Lannister,,,,win,ambush,0,0,6000,12625,"Robb Stark, Tytos Blackwood, Brynden Tully","Lord Andros Brax, Forley Prester",1,Riverrun,The Riverlands, Sack of Darry,298,7,Joffrey/Tommen Baratheon,Robb Stark,Lannister,,,,Darry,,,,win,pitched battle,0,0,,,Gregor Clegane,Lyman Darry,1,Darry,The Riverlands, Battle of Moat Cailin,299,8,Balon/Euron Greyjoy,Robb Stark,Greyjoy,,,,Stark,,,,win,pitched battle,0,0,,,Victarion Greyjoy,,1,Moat Cailin,The North, Battle of Deepwood Motte,299,9,Balon/Euron Greyjoy,Robb Stark,Greyjoy,,,,Stark,,,,win,siege,0,0,1000,,Asha Greyjoy,,1,Deepwood Motte,The North, Battle of the Stony Shore,299,10,Balon/Euron Greyjoy,Robb Stark,Greyjoy,,,,Stark,,,,win,ambush,0,0,264,,Theon Greyjoy,,1,Stony Shore,The North,"Greyjoy's troop number based on the Battle of Deepwood Motte, in which Asha had 1000 soldier on 30 longships. That comes out to ~33 per longship. In the Battle of the Stony Shore, Theon has 8 longships, and just we can estimate that he has 8*33 =265 troops." Battle of Torrhen's Square,299,11,Robb Stark,Balon/Euron Greyjoy,Stark,,,,Greyjoy,,,,win,pitched battle,0,0,244,900,"Rodrik Cassel, Cley Cerwyn",Dagmer Cleftjaw,1,Torrhen's Square,The North,Greyjoy's troop number comes from the 264 estimate to have arrived on the stony shore minus the 20 Theon takes to attack Winterfell. Thus 264-20=244 Battle of Winterfell,299,12,Balon/Euron Greyjoy,Robb Stark,Greyjoy,,,,Stark,,,,win,ambush,0,1,20,,Theon Greyjoy,Bran Stark,1,Winterfell,The North,"It isn't mentioned how many Stark men are left in Winterfell, other than ""very few""." Sack of Torrhen's Square,299,13,Balon/Euron Greyjoy,Balon/Euron Greyjoy,Greyjoy,,,,Stark,,,,win,siege,0,1,,,Dagmer Cleftjaw,,1,Torrhen's Square,The North, Sack of Winterfell,299,14,Joffrey/Tommen Baratheon,Robb Stark,Bolton,Greyjoy,,,Stark,,,,win,ambush,1,0,618,2000,"Ramsay Snow, Theon Greyjoy ","Rodrik Cassel, Cley Cerwyn, Leobald Tallhart",1,Winterfell,The North,"Since House Bolton betrays the Starks for House Lannister, we code this battle as between these two houses. Greyjoy men, numbering only 20, don't play a major part in the fighting and end up dying anyway." Battle of Oxcross,299,15,Robb Stark,Joffrey/Tommen Baratheon,Stark,Tully,,,Lannister,,,,win,ambush,1,1,6000,10000,"Robb Stark, Brynden Tully","Stafford Lannister, Roland Crakehall, Antario Jast",1,Oxcross,The Westerlands, Siege of Storm's End,299,16,Stannis Baratheon,Renly Baratheon,Baratheon,,,,Baratheon,,,,win,siege,1,0,5000,20000,"Stannis Baratheon, Davos Seaworth","Renly Baratheon, Cortnay Penrose, Loras Tyrell, Randyll Tarly, Mathis Rowan",1,Storm's End,The Stormlands, Battle of the Fords,299,17,Joffrey/Tommen Baratheon,Robb Stark,Lannister,,,,Tully,,,,loss,pitched battle,0,0,20000,10000,"Tywin Lannister, Flement Brax, Gregor Clegane, Addam Marbrand, Lyle Crakehall, Leo Lefford","Edmure Tully, Jason Mallister, Karyl Vance",1,Red Fork,The Riverlands, Sack of Harrenhal,299,18,Robb Stark,Joffrey/Tommen Baratheon,Stark,,,,Lannister,,,,win,ambush,1,0,100,100,"Roose Bolton, Vargo Hoat, Robett Glover",Amory Lorch,1,Harrenhal,The Riverlands, Battle of the Crag,299,19,Robb Stark,Joffrey/Tommen Baratheon,Stark,,,,Lannister,,,,win,ambush,0,0,6000,,"Robb Stark, Smalljon Umber, Black Walder Frey",Rolph Spicer,1,Crag,The Westerlands, Battle of the Blackwater,299,20,Stannis Baratheon,Joffrey/Tommen Baratheon,Baratheon,,,,Lannister,,,,loss,pitched battle,1,1,21000,7250,"Stannis Baratheon, Imry Florent, Guyard Morrigen, Rolland Storm, Salladhor Saan, Davos Seaworth","Tyrion Lannister, Jacelyn Bywater, Sandor Clegane, Tywin Lannister, Garlan Tyrell, Mace Tyrell, Randyll Tarly",1,King's Landing,The Crownlands, Siege of Darry,299,21,Robb Stark,Joffrey/Tommen Baratheon,Darry,,,,Lannister,,,,win,siege,0,0,,,Helman Tallhart,,1,Darry,The Riverlands, Battle of Duskendale,299,22,Robb Stark,Joffrey/Tommen Baratheon,Stark,,,,Lannister,,,,loss,pitched battle,1,0,3000,,"Robertt Glover, Helman Tallhart","Randyll Tarly, Gregor Clegane",1,Duskendale,The Crownlands, Battle of the Burning Septry,299,23,,,Brotherhood without Banners,,,,Brave Companions,,,,win,pitched battle,0,0,,,,,1,,The Riverlands, Battle of the Ruby Ford,299,24,Joffrey/Tommen Baratheon,Robb Stark,Lannister,,,,Stark,,,,win,pitched battle,0,0,,6000,Gregor Clegane,"Roose Bolton, Wylis Manderly",,Ruby Ford,The Riverlands, Retaking of Harrenhal,299,25,Joffrey/Tommen Baratheon,,Lannister,,,,Brave Companions,,,,win,pitched battle,1,0,,,Gregor Clegane,Vargo Hoat,1,Harrenhal,The Riverlands, The Red Wedding,299,26,Joffrey/Tommen Baratheon,Robb Stark,Frey,Bolton,,,Stark,,,,win,ambush,1,1,3500,3500,"Walder Frey, Roose Bolton, Walder Rivers",Robb Stark,1,The Twins,The Riverlands,"This observation refers to the battle against the Stark men, not the attack on the wedding" Siege of Seagard,299,27,Robb Stark,Joffrey/Tommen Baratheon,Frey,,,,Mallister,,,,win,siege,0,1,,,Walder Frey,Jason Mallister,1,Seagard,The Riverlands, Battle of Castle Black,300,28,Stannis Baratheon,Mance Rayder,Free folk,Thenns,Giants,,Night's Watch,Baratheon,,,loss,siege,1,1,100000,1240,"Mance Rayder, Tormund Giantsbane, Harma Dogshead, Magnar Styr, Varamyr","Stannis Baratheon, Jon Snow, Donal Noye, Cotter Pyke",0,Castle Black,Beyond the Wall, Fall of Moat Cailin,300,29,Joffrey/Tommen Baratheon,Balon/Euron Greyjoy,Bolton,,,,Greyjoy,,,,win,siege,0,0,,,Ramsey Bolton,,0,Moat Cailin,The North, Sack of Saltpans,300,30,,,Brave Companions,,,,,,,,win,razing,0,0,,,Rorge,,0,Saltpans,The Riverlands, Retaking of Deepwood Motte,300,31,Stannis Baratheon,Balon/Euron Greyjoy,Baratheon,Karstark,Mormont,Glover,Greyjoy,,,,win,pitched battle,0,0,4500,200,"Stannis Baratheon, Alysane Mormot",Asha Greyjoy,0,Deepwood Motte,The North, Battle of the Shield Islands,300,32,Balon/Euron Greyjoy,Joffrey/Tommen Baratheon,Greyjoy,,,,Tyrell,,,,win,pitched battle,0,0,,,"Euron Greyjoy, Victarion Greyjoy",,0,Shield Islands,The Reach, "Invasion of Ryamsport, Vinetown, and Starfish Harbor",300,33,Balon/Euron Greyjoy,Joffrey/Tommen Baratheon,Greyjoy,,,,Tyrell,,,,win,razing,0,0,,,"Euron Greyjoy, Victarion Greyjoy",,0,"Ryamsport, Vinetown, Starfish Harbor",The Reach, Second Seige of Storm's End,300,34,Joffrey/Tommen Baratheon,Stannis Baratheon,Baratheon,,,,Baratheon,,,,win,siege,0,0,,200,"Mace Tyrell, Mathis Rowan",Gilbert Farring,0,Storm's End,The Stormlands, Siege of Dragonstone,300,35,Joffrey/Tommen Baratheon,Stannis Baratheon,Baratheon,,,,Baratheon,,,,win,siege,0,0,2000,,"Loras Tyrell, Raxter Redwyne",Rolland Storm,0,Dragonstone,The Stormlands, Siege of Riverrun,300,36,Joffrey/Tommen Baratheon,Robb Stark,Lannister,Frey,,,Tully,,,,win,siege,0,0,3000,,"Daven Lannister, Ryman Fey, Jaime Lannister",Brynden Tully,0,Riverrun,The Riverlands, Siege of Raventree,300,37,Joffrey/Tommen Baratheon,Robb Stark,Bracken,Lannister,,,Blackwood,,,,win,siege,0,1,1500,,"Jonos Bracken, Jaime Lannister",Tytos Blackwood,0,Raventree,The Riverlands, Siege of Winterfell,300,38,Stannis Baratheon,Joffrey/Tommen Baratheon,Baratheon,Karstark,Mormont,Glover,Bolton,Frey,,,,,,,5000,8000,Stannis Baratheon,Roose Bolton,0,Winterfell,The North, -------------------------------------------------------------------------------- /data/Pandas-Example.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Deep-Learning-in-Computer-Vision/5251ac143cd8ef39da75ffb7ff5ede29b7c2d447/data/Pandas-Example.xlsx -------------------------------------------------------------------------------- /data/anscombe.json: -------------------------------------------------------------------------------- 1 | [ 2 | {"Series":"I", "X":10.0, "Y":8.04}, 3 | {"Series":"I", "X":8.0, "Y":6.95}, 4 | {"Series":"I", "X":13.0, "Y":7.58}, 5 | {"Series":"I", "X":9.0, "Y":8.81}, 6 | {"Series":"I", "X":11.0, "Y":8.33}, 7 | {"Series":"I", "X":14.0, "Y":9.96}, 8 | {"Series":"I", "X":6.0, "Y":7.24}, 9 | {"Series":"I", "X":4.0, "Y":4.26}, 10 | {"Series":"I", "X":12.0, "Y":10.84}, 11 | {"Series":"I", "X":7.0, "Y":4.81}, 12 | {"Series":"I", "X":5.0, "Y":5.68}, 13 | 14 | {"Series":"II", "X":10.0, "Y":9.14}, 15 | {"Series":"II", "X":8.0, "Y":8.14}, 16 | {"Series":"II", "X":13.0, "Y":8.74}, 17 | {"Series":"II", "X":9.0, "Y":8.77}, 18 | {"Series":"II", "X":11.0, "Y":9.26}, 19 | {"Series":"II", "X":14.0, "Y":8.10}, 20 | {"Series":"II", "X":6.0, "Y":6.13}, 21 | {"Series":"II", "X":4.0, "Y":3.10}, 22 | {"Series":"II", "X":12.0, "Y":9.13}, 23 | {"Series":"II", "X":7.0, "Y":7.26}, 24 | {"Series":"II", "X":5.0, "Y":4.74}, 25 | 26 | {"Series":"III", "X":10.0, "Y":7.46}, 27 | {"Series":"III", "X":8.0, "Y":6.77}, 28 | {"Series":"III", "X":13.0, "Y":12.74}, 29 | {"Series":"III", "X":9.0, "Y":7.11}, 30 | {"Series":"III", "X":11.0, "Y":7.81}, 31 | {"Series":"III", "X":14.0, "Y":8.84}, 32 | {"Series":"III", "X":6.0, "Y":6.08}, 33 | {"Series":"III", "X":4.0, "Y":5.39}, 34 | {"Series":"III", "X":12.0, "Y":8.15}, 35 | {"Series":"III", "X":7.0, "Y":6.42}, 36 | {"Series":"III", "X":5.0, "Y":5.73}, 37 | 38 | {"Series":"IV", "X":8.0, "Y":6.58}, 39 | {"Series":"IV", "X":8.0, "Y":5.76}, 40 | {"Series":"IV", "X":8.0, "Y":7.71}, 41 | {"Series":"IV", "X":8.0, "Y":8.84}, 42 | {"Series":"IV", "X":8.0, "Y":8.47}, 43 | {"Series":"IV", "X":8.0, "Y":7.04}, 44 | {"Series":"IV", "X":8.0, "Y":5.25}, 45 | {"Series":"IV", "X":19.0, "Y":12.50}, 46 | {"Series":"IV", "X":8.0, "Y":5.56}, 47 | {"Series":"IV", "X":8.0, "Y":7.91}, 48 | {"Series":"IV", "X":8.0, "Y":6.89} 49 | ] 50 | -------------------------------------------------------------------------------- /data/datasets.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Deep-Learning-in-Computer-Vision/5251ac143cd8ef39da75ffb7ff5ede29b7c2d447/data/datasets.xlsx -------------------------------------------------------------------------------- /data/movies_metadata.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soltaniehha/Deep-Learning-in-Computer-Vision/5251ac143cd8ef39da75ffb7ff5ede29b7c2d447/data/movies_metadata.csv -------------------------------------------------------------------------------- /data/mtcars.csv: -------------------------------------------------------------------------------- 1 | ,mpg,cyl,disp,hp,drat,wt,qsec,vs,am,gear,carb 2 | 0,21.0,6,160.0,110,3.9,2.62,16.46,0,1,4,4 3 | 1,21.0,6,160.0,110,3.9,2.875,17.02,0,1,4,4 4 | 2,22.8,4,108.0,93,3.85,2.32,18.61,1,1,4,1 5 | 3,21.4,6,258.0,110,3.08,3.215,19.44,1,0,3,1 6 | 4,18.7,8,360.0,175,3.15,3.44,17.02,0,0,3,2 7 | 5,18.1,6,225.0,105,2.76,3.46,20.22,1,0,3,1 8 | 6,14.3,8,360.0,245,3.21,3.57,15.84,0,0,3,4 9 | 7,24.4,4,146.7,62,3.69,3.19,20.0,1,0,4,2 10 | 8,22.8,4,140.8,95,3.92,3.15,22.9,1,0,4,2 11 | 9,19.2,6,167.6,123,3.92,3.44,18.3,1,0,4,4 12 | 10,17.8,6,167.6,123,3.92,3.44,18.9,1,0,4,4 13 | 11,16.4,8,275.8,180,3.07,4.07,17.4,0,0,3,3 14 | 12,17.3,8,275.8,180,3.07,3.73,17.6,0,0,3,3 15 | 13,15.2,8,275.8,180,3.07,3.78,18.0,0,0,3,3 16 | 14,10.4,8,472.0,205,2.93,5.25,17.98,0,0,3,4 17 | 15,10.4,8,460.0,215,3.0,5.424,17.82,0,0,3,4 18 | 16,14.7,8,440.0,230,3.23,5.345,17.42,0,0,3,4 19 | 17,32.4,4,78.7,66,4.08,2.2,19.47,1,1,4,1 20 | 18,30.4,4,75.7,52,4.93,1.615,18.52,1,1,4,2 21 | 19,33.9,4,71.1,65,4.22,1.835,19.9,1,1,4,1 22 | 20,21.5,4,120.1,97,3.7,2.465,20.01,1,0,3,1 23 | 21,15.5,8,318.0,150,2.76,3.52,16.87,0,0,3,2 24 | 22,15.2,8,304.0,150,3.15,3.435,17.3,0,0,3,2 25 | 23,13.3,8,350.0,245,3.73,3.84,15.41,0,0,3,4 26 | 24,19.2,8,400.0,175,3.08,3.845,17.05,0,0,3,2 27 | 25,27.3,4,79.0,66,4.08,1.935,18.9,1,1,4,1 28 | 26,26.0,4,120.3,91,4.43,2.14,16.7,0,1,5,2 29 | 27,30.4,4,95.1,113,3.77,1.513,16.9,1,1,5,2 30 | 28,15.8,8,351.0,264,4.22,3.17,14.5,0,1,5,4 31 | 29,19.7,6,145.0,175,3.62,2.77,15.5,0,1,5,6 32 | 30,15.0,8,301.0,335,3.54,3.57,14.6,0,1,5,8 33 | 31,21.4,4,121.0,109,4.11,2.78,18.6,1,1,4,2 34 | -------------------------------------------------------------------------------- /data/state-abbrevs.csv: -------------------------------------------------------------------------------- 1 | "state","abbreviation" 2 | "Alabama","AL" 3 | "Alaska","AK" 4 | "Arizona","AZ" 5 | "Arkansas","AR" 6 | "California","CA" 7 | "Colorado","CO" 8 | "Connecticut","CT" 9 | "Delaware","DE" 10 | "District of Columbia","DC" 11 | "Florida","FL" 12 | "Georgia","GA" 13 | "Hawaii","HI" 14 | "Idaho","ID" 15 | "Illinois","IL" 16 | "Indiana","IN" 17 | "Iowa","IA" 18 | "Kansas","KS" 19 | "Kentucky","KY" 20 | "Louisiana","LA" 21 | "Maine","ME" 22 | "Montana","MT" 23 | "Nebraska","NE" 24 | "Nevada","NV" 25 | "New Hampshire","NH" 26 | "New Jersey","NJ" 27 | "New Mexico","NM" 28 | "New York","NY" 29 | "North Carolina","NC" 30 | "North Dakota","ND" 31 | "Ohio","OH" 32 | "Oklahoma","OK" 33 | "Oregon","OR" 34 | "Maryland","MD" 35 | "Massachusetts","MA" 36 | "Michigan","MI" 37 | "Minnesota","MN" 38 | "Mississippi","MS" 39 | "Missouri","MO" 40 | "Pennsylvania","PA" 41 | "Rhode Island","RI" 42 | "South Carolina","SC" 43 | "South Dakota","SD" 44 | "Tennessee","TN" 45 | "Texas","TX" 46 | "Utah","UT" 47 | "Vermont","VT" 48 | "Virginia","VA" 49 | "Washington","WA" 50 | "West Virginia","WV" 51 | "Wisconsin","WI" 52 | "Wyoming","WY" -------------------------------------------------------------------------------- /data/state-areas.csv: -------------------------------------------------------------------------------- 1 | state,area (sq. mi) 2 | Alabama,52423 3 | Alaska,656425 4 | Arizona,114006 5 | Arkansas,53182 6 | California,163707 7 | Colorado,104100 8 | Connecticut,5544 9 | Delaware,1954 10 | Florida,65758 11 | Georgia,59441 12 | Hawaii,10932 13 | Idaho,83574 14 | Illinois,57918 15 | Indiana,36420 16 | Iowa,56276 17 | Kansas,82282 18 | Kentucky,40411 19 | Louisiana,51843 20 | Maine,35387 21 | Maryland,12407 22 | Massachusetts,10555 23 | Michigan,96810 24 | Minnesota,86943 25 | Mississippi,48434 26 | Missouri,69709 27 | Montana,147046 28 | Nebraska,77358 29 | Nevada,110567 30 | New Hampshire,9351 31 | New Jersey,8722 32 | New Mexico,121593 33 | New York,54475 34 | North Carolina,53821 35 | North Dakota,70704 36 | Ohio,44828 37 | Oklahoma,69903 38 | Oregon,98386 39 | Pennsylvania,46058 40 | Rhode Island,1545 41 | South Carolina,32007 42 | South Dakota,77121 43 | Tennessee,42146 44 | Texas,268601 45 | Utah,84904 46 | Vermont,9615 47 | Virginia,42769 48 | Washington,71303 49 | West Virginia,24231 50 | Wisconsin,65503 51 | Wyoming,97818 52 | District of Columbia,68 53 | Puerto Rico,3515 54 | --------------------------------------------------------------------------------