├── 00_00_preface.ipynb ├── 01_02_python_installation.ipynb ├── 01_03.ipynb ├── 01_04.ipynb ├── 01_05.ipynb ├── 01_06.ipynb ├── 01_07.ipynb ├── 01_GettingStarted.ipynb ├── 02_00.ipynb ├── 02_01.ipynb ├── 02_02.ipynb ├── 02_03.ipynb ├── 02_04.ipynb ├── 02_05.ipynb ├── 02_06.ipynb ├── 02_07.ipynb ├── 02_08.ipynb ├── 03_01.ipynb ├── 03_02.ipynb ├── 03_03.ipynb ├── 03_04.ipynb ├── 03_05.ipynb ├── 04_01.ipynb ├── 04_02.ipynb ├── 04_03.ipynb ├── 04_04.ipynb ├── 04_05.ipynb ├── 04_06.ipynb ├── 04_07.ipynb ├── 04_08.ipynb ├── 04_09.ipynb ├── 04_10.ipynb ├── 05_01.ipynb ├── 05_02.ipynb ├── 05_03.ipynb ├── 05_04.ipynb ├── 05_05.ipynb ├── 05_06.ipynb ├── 05_07.ipynb ├── 05_08.ipynb ├── 05_09.ipynb ├── 05_10.ipynb ├── 05_11.ipynb ├── 05_12.ipynb ├── 06_01.ipynb ├── 06_02.ipynb ├── 06_04.ipynb ├── 06_05.ipynb ├── 06_06.ipynb ├── 06_07.ipynb ├── 06_08.ipynb ├── 06_09.ipynb ├── 06_10.ipynb ├── 07_01.ipynb ├── 07_03.ipynb ├── 07_04.ipynb ├── 07_05.ipynb ├── 07_06.ipynb ├── 07_07.ipynb ├── 07_08.ipynb ├── 07_09.ipynb ├── 07_11.ipynb ├── 07_12.ipynb ├── 08_01.ipynb ├── 08_03.ipynb ├── 08_04.ipynb ├── 08_05.ipynb ├── 09_01.ipynb ├── 09_02.ipynb ├── 09_03.ipynb ├── 10_01.ipynb ├── 10_02.ipynb ├── 10_03.ipynb ├── 10_04.ipynb ├── 10_05.ipynb ├── 10_06.ipynb ├── LICENSE ├── README.md ├── data.xls └── index.ipynb /00_00_preface.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "collapsed_sections": [], 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "#History\n", 33 | "\n", 34 | "I started using Python in July 2010. I was looking for a programming language, which is:\n", 35 | "* open source,\n", 36 | "* capable of combining many codes/modules/softwares,\n", 37 | "* easy to learn, and\n", 38 | "* help is available on the internet. \n", 39 | "\n", 40 | "I came across Python and Perl, even though there might be many more options available. I googled the use of Python and Perl in the field of general scientific usage, hydrology, Geographic Information System (GIS), and statistics etc. Basically, I tried to search for the examples I thought I might be facing in the future. \n", 41 | "\n", 42 | "I somehow felt Python had more help related to my field and felt it to be the language of my need. I did not know at that time if my conclusions (in the areas mentioned above) about the Python versus Perl were true or not? But, now I am certain of making a good choice as I feel happy for choosing the Python for my work. Python has evolved a lot in the last decade and has become much more valuable for the kind of work I am into and find it to be fulfilling my requirements completely.\n", 43 | "\n", 44 | "After getting a lot of help from open source community, I felt that I should also make a small contribution into the free world. I tried to contribute in few areas, but could not go ahead because of my less knowledge in those areas. After spending extensive one year with Python, I thought to make some contribution into Python world. I wrote few small packages for the Python, and started writing this book.\n", 45 | "\n", 46 | "I tried the book to keep it very simple as I would like to have read some Python book. If you find something complicated, do let me know, I will try my best to simplify it. \n", 47 | "\n", 48 | "I hope that you will find this book helpful and enjoyable.\n", 49 | "\n", 50 | "\n", 51 | "Sat Kumar Tomer \n", 52 | "Bangalore, India\n", 53 | "\n", 54 | "\n", 55 | "Sat Kumar Tomer did PhD from the Department of Civil Engineering at Indian Institute of Science, Bangalore, India. He did post-doc from CESBIO, Toulouse, France and co-founder of Satyukt Analytics Pvt Ltd. " 56 | ], 57 | "metadata": { 58 | "id": "nW-uxA89Q1ET" 59 | } 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "source": [ 64 | "#Acknowledgements\n", 65 | "\n", 66 | "I am thankful to you for reading the book, and hope to receive feedback from you.\n", 67 | "\n", 68 | "I am thankful to Allen B. Downey who provided the latex source code of his books, which helped me in formatting the book in a better way. Apart from it, he has written a nice history for his book titled ‘Think Python: How to Think Like a Computer Scientist’, which encouraged me for writing this book. And, I adapted many sentences from his book, mainly the basic about Python.\n", 69 | "\n", 70 | "I am thankful to the Free Software Foundation for developing the GNU Free Documentation License. I am thankful to Green Tea Press.\n", 71 | "\n", 72 | "# Suggestions/corrections\n", 73 | "If you have a suggestion or correction, you are more than welcome to drop your comment on Github. \n", 74 | "\n", 75 | "# Contribution\n", 76 | "If you want to contribute to the book, you can send an email at satkumartomer@gmail.com\n", 77 | "\n", 78 | "# Notification about the update in the book\n", 79 | "You can follow the update by installing GitHub app and following the book in GitHub." 80 | ], 81 | "metadata": { 82 | "id": "Cj82DpOKT1P2" 83 | } 84 | } 85 | ] 86 | } -------------------------------------------------------------------------------- /01_02_python_installation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyPsvkhgnFI9w3HGzdZtG0nz", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "##1.2 Python Installation\n", 33 | "\n", 34 | "Usually all the Linux/Unix system has basic Python installed by-default. If you are in some other platform (Windows or Mac), or for some reason your Linux/Unix do not have a basic version of Python installed, you can use the below instructions to install the basic version of Python depending upon on your platform. The basic version of Python includes minimal packages required to run the python. Often additional packages are required to do the required tasks, specially for most of the hydrological applications, the basic version of python is not enough, and additional packages are must.\n", 35 | "\n", 36 | "The next section will describe the installation procedure for required additional packages. \n", 37 | "\n", 38 | "The installation of Python for the various operating system is done in the following ways\n", 39 | "### 1.2.1 Ubuntu/Debian\n", 40 | "In the Ubuntu/Debian, the Python is installed by running the usual installation command, i.e.:\n" 41 | ], 42 | "metadata": { 43 | "id": "XI9vH4q34I9o" 44 | } 45 | }, 46 | { 47 | "cell_type": "code", 48 | "source": [ 49 | "$ sudo apt-get install python" 50 | ], 51 | "metadata": { 52 | "id": "jONAOUSN4gW7" 53 | }, 54 | "execution_count": null, 55 | "outputs": [] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "source": [ 60 | "Kindly note that $ symbole means that you need to run the command in your Shell.\n", 61 | "\n", 62 | "### 1.2.2 Fedora\n", 63 | "On Fedora, the installation of the Python is performed by using the following command:" 64 | ], 65 | "metadata": { 66 | "id": "9-xcCHft46Wd" 67 | } 68 | }, 69 | { 70 | "cell_type": "code", 71 | "source": [ 72 | "$ sudo yum install python" 73 | ], 74 | "metadata": { 75 | "id": "JBHxQxaUFFvl" 76 | }, 77 | "execution_count": null, 78 | "outputs": [] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "source": [ 83 | "### 1.2.3 FreeBSD\n", 84 | "Python is installed on FreeBSD by running:" 85 | ], 86 | "metadata": { 87 | "id": "kTGLAwym5RLW" 88 | } 89 | }, 90 | { 91 | "cell_type": "code", 92 | "source": [ 93 | "$ sudo pkg_add install python" 94 | ], 95 | "metadata": { 96 | "id": "L1aUz8I25ZBs" 97 | }, 98 | "execution_count": null, 99 | "outputs": [] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "source": [ 104 | "The Linux user might be familiar with sudo. It allows user to run programs with the security privileges of root or administrator. Window user may ignore sudo, as they do not need to specify this." 105 | ], 106 | "metadata": { 107 | "id": "qCIWrQRp5evW" 108 | } 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "source": [ 113 | "### 1.2.4 Windows\n", 114 | "For Windows users, the suitable version of Python can be downloaded from http://www.python.org/getit/. It provides a .msi file, which can be easily installed by double\n", 115 | "clicking on it." 116 | ], 117 | "metadata": { 118 | "id": "jEeK2wV55Jpk" 119 | } 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "source": [ 124 | "### 1.2.5 Mac OS\n", 125 | "Mac OS users also can download a suitable version of Python from http://www.python.org/getit/ and install it." 126 | ], 127 | "metadata": { 128 | "id": "NxAhZPkY4_C9" 129 | } 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "source": [ 134 | "### 1.2.6 Google Colab\n", 135 | "For easy colloboration, sharing and running high resources code, it may be run in a cloud. One such popular tool is *Google Colab*. With the help of it, you can immidiately start running your Python code without worrying about the installation and other required configurations.\n", 136 | "\n", 137 | "\n", 138 | "Since there are many existing tools for learning about the *Colab*, it is not repeated here. You may use the following resources to learn about it:\n", 139 | "\n", 140 | "* Watch on Youtube [Introduction to Colab](https://www.youtube.com/watch?v=inN8seMm7UI) to learn more.\n", 141 | "\n", 142 | "* Read on [Spatialthoughts](https://courses.spatialthoughts.com/python-dataviz.html#notebooks-and-datasets)\n", 143 | "\n", 144 | "Since this book is written in Google Colab, it will be useful to know about this tool and practice the examples provided in this book. \n", 145 | "\n" 146 | ], 147 | "metadata": { 148 | "id": "rht4M7KS6A4u" 149 | } 150 | } 151 | ] 152 | } -------------------------------------------------------------------------------- /01_03.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyNQ9W3C8TI6NVaS7jwDuTwZ", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 1.3 Install Additional Packages\n", 33 | "Pip is a useful program to install additional packages in Python, which provides an easiest way to install additional packages. Before installing pip, distribute should be installed. To do so, first we need to download Distribute, which is done by downloading distribute_setup.py file from\n", 34 | "http://python-distribute.org/distribute_setup.py, and running the following command:" 35 | ], 36 | "metadata": { 37 | "id": "6LTYv3Iy9qYy" 38 | } 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "metadata": { 44 | "id": "yBgn6OwX9ptH" 45 | }, 46 | "outputs": [], 47 | "source": [ 48 | "$ sudo python distribute_setup.py" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "source": [ 54 | "Note again for Windows users the sudo is not needed, and you can use the command without using it.\n", 55 | "\n", 56 | "Now download the get-pip.py from https://github.com/pypa/pip/raw/master/contrib/get-pip.py, and run as root:" 57 | ], 58 | "metadata": { 59 | "id": "1pEFiRdc94Gn" 60 | } 61 | }, 62 | { 63 | "cell_type": "code", 64 | "source": [ 65 | "$ sudo python get-pip.py" 66 | ], 67 | "metadata": { 68 | "id": "jXB6BYjo-HtF" 69 | }, 70 | "execution_count": null, 71 | "outputs": [] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "source": [ 76 | "Note, that the distribute_setup.py and get-pip.py should be in your current working directory while installing, otherwise give the full path name of file. If you did not get any error in this\n", 77 | "procedure, pip should be ready to install new packages. The procedure to install packages is simple, let’s say we want to install package SomePackage, then we need to run the following command:" 78 | ], 79 | "metadata": { 80 | "id": "Ow9dj7Sg-Ksc" 81 | } 82 | }, 83 | { 84 | "cell_type": "code", 85 | "source": [ 86 | "$ sudo pip install SomePackage" 87 | ], 88 | "metadata": { 89 | "id": "L0Sudgqq-SVR" 90 | }, 91 | "execution_count": null, 92 | "outputs": [] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "source": [ 97 | "Some frequently used packages are Numpy, Scipy, xlrd, xlwt, and gdal, so we will install all of them at this stage. Late, whenever a new package/library will be required, instructions to\n", 98 | "download and install them will be given there." 99 | ], 100 | "metadata": { 101 | "id": "6GEJTtLk-VP-" 102 | } 103 | }, 104 | { 105 | "cell_type": "code", 106 | "source": [ 107 | "$ sudo pip install Numpy\n", 108 | "$ sudo pip install Scipy\n", 109 | "$ sudo pip install xlrd\n", 110 | "$ sudo pip install xlwt\n", 111 | "$ sudo pip install gdal" 112 | ], 113 | "metadata": { 114 | "id": "lmQSohxr-evy" 115 | }, 116 | "execution_count": null, 117 | "outputs": [] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "source": [ 122 | "These packages/libraries can be installed by specifying all packages name in one line, i.e." 123 | ], 124 | "metadata": { 125 | "id": "e2ajWxOu-hjd" 126 | } 127 | }, 128 | { 129 | "cell_type": "code", 130 | "source": [ 131 | "$ sudo pip install Numpy Scipy xlrd xlwt gdal" 132 | ], 133 | "metadata": { 134 | "id": "aOugRO2g-lRw" 135 | }, 136 | "execution_count": null, 137 | "outputs": [] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "source": [ 142 | "But, at this time it is better to install them in separate line, so that if you get some error, you can easily find out which package is giving error. The most common problem with pip is that, it is not able to download library/package from internet. It happens mainly when you use some proxy to connect to the internet. In that case, we can download the *.tar.gz file of the library using internet browser, and then run the pip in the following way:" 143 | ], 144 | "metadata": { 145 | "id": "bPxPqhk1-pGy" 146 | } 147 | }, 148 | { 149 | "cell_type": "code", 150 | "source": [ 151 | "$ sudo pip install /path/to/*.tar.gz" 152 | ], 153 | "metadata": { 154 | "id": "VQSxYu0i-wzP" 155 | }, 156 | "execution_count": null, 157 | "outputs": [] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "source": [ 162 | "Windows user can download *.exe or *.msi file if available and then install by double clicking it. If this also fails then, as a last option, you can download *.tar.gz file and extract it. Then, go to the folder where you have extracted the file. There, you should see the setup.py file. Now, run the\n", 163 | "following command:" 164 | ], 165 | "metadata": { 166 | "id": "MjqZUvdG-zM4" 167 | } 168 | }, 169 | { 170 | "cell_type": "code", 171 | "source": [ 172 | "$ sudo python setup.py install" 173 | ], 174 | "metadata": { 175 | "id": "z-2-tp0n-4ii" 176 | }, 177 | "execution_count": null, 178 | "outputs": [] 179 | }, 180 | { 181 | "cell_type": "markdown", 182 | "source": [ 183 | "If you see any error in this, which could possibly arise because of non-availability of some\n", 184 | "dependent package/library in your computer. You should read the README file provided with the\n", 185 | "package, it can provide you details of required package/library, and how to install them.\n", 186 | "The package/libraries are upgraded using the pip in the following way." 187 | ], 188 | "metadata": { 189 | "id": "lrejvYDK-6Tk" 190 | } 191 | }, 192 | { 193 | "cell_type": "code", 194 | "source": [ 195 | "$ sudo pip install --upgrade some_package" 196 | ], 197 | "metadata": { 198 | "id": "vi8XX5N5-8qZ" 199 | }, 200 | "execution_count": null, 201 | "outputs": [] 202 | } 203 | ] 204 | } -------------------------------------------------------------------------------- /01_04.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyM6YLGjMXlLuI8T49fICkRQ", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": { 33 | "id": "_lfUMP9F_MGm" 34 | }, 35 | "outputs": [], 36 | "source": [] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "source": [ 41 | "## 1.4 Interactive Development Environment\n", 42 | "Simple text editors can be used to write Python programs, but these do not provide options\n", 43 | "for easy formatting of text, auto completion of text, and debugging options etc. IDE (Interactive Development Environment) provides many options to quickly format the program in Python way, and easily debug them. There are various IDE available for use e.g. PyDev, Spyder, IDLE, and many many others. A list of them can be found at http://wiki.python.org/moin/IntegratedDevelopmentEnvironments. I am using Spyder for my work, which is similar to MATLAB. The reason to use Spyder was since earlier I used to work on MATLAB, and Spyder is\n", 44 | "similar to it, so I found myself to be familiar with it. However, you can use any IDE, and after being familiar, it doesn’t matter which one you use.\n", 45 | "\n", 46 | "You can also use Cloud IDE such as Google Colab. You can read about it in section 1.2.6. " 47 | ], 48 | "metadata": { 49 | "id": "pmrJChOF_RUu" 50 | } 51 | } 52 | ] 53 | } -------------------------------------------------------------------------------- /01_05.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyNlysESsV9z0k3txPKZTJuw", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 1.5 Execute the Program\n", 33 | "\n", 34 | "Python is an interpreted language as the programs are executed by an interpreter. There are two ways to use the interpreter: interactive mode and script mode. In the interactive mode, you type Python programs (after invoking the python, which is done by typing python in a terminal or command window) and interpreter prints the result, e.g. we do 1+1 in it." 35 | ], 36 | "metadata": { 37 | "id": "xSVhK6R7BCpS" 38 | } 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 1, 43 | "metadata": { 44 | "colab": { 45 | "base_uri": "https://localhost:8080/" 46 | }, 47 | "id": "0rjCf_AKAusv", 48 | "outputId": "db210f2d-cda4-4977-c1f8-a811917ac58c" 49 | }, 50 | "outputs": [ 51 | { 52 | "output_type": "execute_result", 53 | "data": { 54 | "text/plain": [ 55 | "2" 56 | ] 57 | }, 58 | "metadata": {}, 59 | "execution_count": 1 60 | } 61 | ], 62 | "source": [ 63 | "$ python\n", 64 | ">>> 1 + 1\n", 65 | "2" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "source": [ 71 | "When you invoked Python, you will also see other details like version of Python.\n", 72 | "\n", 73 | "The chevron, >>>, is the prompt which interpreter uses to indicate that it is ready. If you type 1 + 1, the interpreter replies 2. Alternatively, you can store code in a file and use the interpreter to execute the contents of the file, which is called a script. By convention, Python scripts have names\n", 74 | "that end with .pt. Suppose, you have named you script as my script.pt, and you want to execute it, in a Unix/Linux shell, you would do:" 75 | ], 76 | "metadata": { 77 | "id": "JZiBbSIwBcy7" 78 | } 79 | }, 80 | { 81 | "cell_type": "code", 82 | "source": [ 83 | "$ python myscript.py" 84 | ], 85 | "metadata": { 86 | "id": "qkVfktPOBzkN" 87 | }, 88 | "execution_count": null, 89 | "outputs": [] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "source": [ 94 | "or, you can give your script executable permission and simply run the script. The syntax to do is:" 95 | ], 96 | "metadata": { 97 | "id": "TYHbCUcOB0v-" 98 | } 99 | }, 100 | { 101 | "cell_type": "code", 102 | "source": [ 103 | "$ chmod +x myscript.py\n", 104 | "$ ./myscript.py" 105 | ], 106 | "metadata": { 107 | "id": "GTG5vBgfB3P4" 108 | }, 109 | "execution_count": null, 110 | "outputs": [] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "source": [ 115 | "In KDE’s, the details of executing scripts are different. You can find instructions for your environment at the Python website python.org.\n", 116 | "\n", 117 | "Working in interactive mode is convenient for testing small pieces of code because you can type and execute them immediately. But for anything more than a few lines, you should save your code as a script so you can modify and execute it in the future.\n", 118 | "\n", 119 | "In Colab, there are two modes:\n", 120 | "* Code\n", 121 | "* Text\n", 122 | "\n", 123 | "In the *Code* mode, you can type Python code and you can run the section of code by clicking on Play icon provided in the left. In the *Text* mode, you can write any text for the explaination which Python will not run. You can click on *+ Code* or *+Text* to add a new section for it. \n", 124 | "\n" 125 | ], 126 | "metadata": { 127 | "id": "GCWmlJflB5q0" 128 | } 129 | } 130 | ] 131 | } -------------------------------------------------------------------------------- /01_06.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyPFQX0swaoqdRsQO1lmzUxc", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 1.6 Type of Errors\n", 33 | "Three kinds of errors can occur in a program: syntax errors, runtime errors, and semantic errors. It is useful to distinguish between them in order to track them down more quickly.\n", 34 | "\n", 35 | "### 1.6.1 Syntax Errors\n", 36 | "Python can only execute a program if the syntax is correct; otherwise, the interpreter displays an error message. Syntax refers to the structure of a program and the rules about that structure. For example, parentheses have to come in matching pairs, so (1 + 2) is legal, but 8) is a syntax error.\n", 37 | "\n", 38 | "### 1.6.2 Runtime Errors\n", 39 | "The second type of error is a runtime error, so called because the error does not appear until after the program has started running. These errors are also called exceptions because they usually indicate that something exceptional (and bad) has happened.\n", 40 | "\n", 41 | "### 1.6.3 Semantic Errors\n", 42 | "The third type of error is the semantic error. If there is a semantic error in your program, it will run successfully in the sense that the computer will not generate any error messages, but it will not do the right thing." 43 | ], 44 | "metadata": { 45 | "id": "T0hZhq92C5ra" 46 | } 47 | } 48 | ] 49 | } -------------------------------------------------------------------------------- /01_07.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyNRnXHfoEQho4zjITuCAyvt", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 1.7 The First Program\n", 33 | "Traditionally, the first program you start with in a new language is “Hello, World!” program, because all it does is display the words, “Hello, World!”. In Python, it is done by following syntax of codes:" 34 | ], 35 | "metadata": { 36 | "id": "wBHGPXiaD9zz" 37 | } 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 2, 42 | "metadata": { 43 | "colab": { 44 | "base_uri": "https://localhost:8080/" 45 | }, 46 | "id": "nsyvE6N-D5yx", 47 | "outputId": "ad0defde-dd56-4d9c-b60c-5b77047f4737" 48 | }, 49 | "outputs": [ 50 | { 51 | "output_type": "stream", 52 | "name": "stdout", 53 | "text": [ 54 | "Hello, World!\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "print('Hello, World!')" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "source": [ 65 | "This is an example of a print statement, which prints on computer screen. In this case, the result is\n", 66 | "the sentence:\n", 67 | "\n", 68 | "Hello, World!\n", 69 | "\n", 70 | "The quotation marks in the program mark the beginning and end of the string." 71 | ], 72 | "metadata": { 73 | "id": "1tPq73E4EVT-" 74 | } 75 | } 76 | ] 77 | } -------------------------------------------------------------------------------- /01_GettingStarted.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyNlp3SaVnUmmiIwaxXgFWmF", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "# Getting Started\n", 33 | "\n", 34 | "## 1.1 Why Python?\n", 35 | "Python is a simple and powerful programming language. By simple I mean, that it is much more forgiving than languages like C though slow also. By powerful, I mean it can glue many existing\n", 36 | "codes, which were written in C, C++, Fortran etc. easily. It has a growing user community, which makes many tools easily available. Python Package Index, which is a major host of the Python code, has more than 15,000 packages listed as of now, speaks about it popularity. The use of Python in hydrology community is not so fast as compared to other field, but now-a-days many new hydrological packages are being developed. Python provides access to a nice combination of GIS tools,\n", 37 | "Mathematics, and Statistics etc., which makes it a useful language for the hydrologist. Following are the major advantages of Python for the hydrologist:\n", 38 | "1. A clear and readable syntax.\n", 39 | "2. Modules can be easily written in C, C++.\n", 40 | "3. It can be used on all major platforms (Windows, Linux/Unix, Mac etc.)\n", 41 | "4. Easy to learn.\n", 42 | "5. And, it is free." 43 | ], 44 | "metadata": { 45 | "id": "8HaCMzYizhvz" 46 | } 47 | } 48 | ] 49 | } -------------------------------------------------------------------------------- /02_00.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyNI22EZ7uggfzYwSln/afU/", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "# A Bit of Python\n", 33 | "\n", 34 | "Before jumping into applications of Python programming language into hydrology, which would involve writing many lines of codes, it is better to first learn some basics of Python, e.g. the types of data, looping (performing same task many times), and writing functions etc. Let’s begin with the types of data that we are going to deal with. Note that there are few more data types, and you may create your own data type, we will not touch on these issues, as they involve more in-depth\n", 35 | "knowledge of Python.\n", 36 | "\n", 37 | "\n" 38 | ], 39 | "metadata": { 40 | "id": "16mbKkFGGgDl" 41 | } 42 | } 43 | ] 44 | } -------------------------------------------------------------------------------- /02_01.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyOQTQk9XaHpPd4MO8LEn3DJ", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 2.1 Data Types\n", 33 | "There are basically two types of data in Python: numbers and strings. The type function can be used to check the type of data.\n", 34 | "\n", 35 | "### 2.1.1 Numbers\n", 36 | "There are three types of number in Python: integers, floating point and complex numbers. Integers are needed for indexing the arrays (vector, matrix), and counting etc. In Python there is no need to define the variable types a priori, and you are even allowed to change the data type later in the program, wherever needed." 37 | ], 38 | "metadata": { 39 | "id": "zrbBP1fxG_6I" 40 | } 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 1, 45 | "metadata": { 46 | "colab": { 47 | "base_uri": "https://localhost:8080/" 48 | }, 49 | "id": "4bknbpavG7hC", 50 | "outputId": "2a208078-61e8-4c6f-a570-5e61028de87e" 51 | }, 52 | "outputs": [ 53 | { 54 | "output_type": "execute_result", 55 | "data": { 56 | "text/plain": [ 57 | "int" 58 | ] 59 | }, 60 | "metadata": {}, 61 | "execution_count": 1 62 | } 63 | ], 64 | "source": [ 65 | "a = 5\n", 66 | "type(a)" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "source": [ 72 | "This means that, the data type of variable *a* is integer.\n", 73 | "\n", 74 | "Another most commonly used data type is float. Most of\n", 75 | "the real life data we use belongs to this category of data type." 76 | ], 77 | "metadata": { 78 | "id": "Dl3bIW63HUrB" 79 | } 80 | }, 81 | { 82 | "cell_type": "code", 83 | "source": [ 84 | "b = 7.8\n", 85 | "type(b)" 86 | ], 87 | "metadata": { 88 | "colab": { 89 | "base_uri": "https://localhost:8080/" 90 | }, 91 | "id": "HOxaBGH-HiMp", 92 | "outputId": "935670ec-f2f5-4ed1-bd83-4614eeb1a68c" 93 | }, 94 | "execution_count": 2, 95 | "outputs": [ 96 | { 97 | "output_type": "execute_result", 98 | "data": { 99 | "text/plain": [ 100 | "float" 101 | ] 102 | }, 103 | "metadata": {}, 104 | "execution_count": 2 105 | } 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "source": [ 111 | "This tells that the data type is floating point. Another data type is complex, which is not frequently\n", 112 | "needed in the analysis usually we do." 113 | ], 114 | "metadata": { 115 | "id": "kkj-GNyAHmyq" 116 | } 117 | }, 118 | { 119 | "cell_type": "code", 120 | "source": [ 121 | "c = 5 + 2j\n", 122 | "type(c)" 123 | ], 124 | "metadata": { 125 | "colab": { 126 | "base_uri": "https://localhost:8080/" 127 | }, 128 | "id": "8en0J9sxHsOU", 129 | "outputId": "cfe24ed1-1d10-4fed-c808-67adb4ff0d21" 130 | }, 131 | "execution_count": 3, 132 | "outputs": [ 133 | { 134 | "output_type": "execute_result", 135 | "data": { 136 | "text/plain": [ 137 | "complex" 138 | ] 139 | }, 140 | "metadata": {}, 141 | "execution_count": 3 142 | } 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "source": [ 148 | "The variable *c* represents the complex data type.\n", 149 | "\n", 150 | "### 2.1.2 Strings\n", 151 | "A string is a sequence of characters. There are mainly three following way to specify a string.\n", 152 | "\n", 153 | "#### Single Quotes\n", 154 | "The text written inside single quotes is treated as string by Python." 155 | ], 156 | "metadata": { 157 | "id": "bl7TUJbRHyDy" 158 | } 159 | }, 160 | { 161 | "cell_type": "code", 162 | "source": [ 163 | "foo = 'my name is Joe'\n", 164 | "print(foo)" 165 | ], 166 | "metadata": { 167 | "colab": { 168 | "base_uri": "https://localhost:8080/" 169 | }, 170 | "id": "bve2alTNIGR5", 171 | "outputId": "d48bb088-f00c-4e89-ddef-ea4dbb0a0781" 172 | }, 173 | "execution_count": 4, 174 | "outputs": [ 175 | { 176 | "output_type": "stream", 177 | "name": "stdout", 178 | "text": [ 179 | "my name is Joe\n" 180 | ] 181 | } 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "source": [ 187 | "#### Double Quotes\n", 188 | "Double quotes are also used to define a string. One may ask the question, \"If single quotes are able to\n", 189 | "define a string, then why are double quotes needed?\" Let’s try to write What's your name? using single quotes." 190 | ], 191 | "metadata": { 192 | "id": "JQfrMIqxICDT" 193 | } 194 | }, 195 | { 196 | "cell_type": "code", 197 | "source": [ 198 | "foo = 'what's your name?'" 199 | ], 200 | "metadata": { 201 | "colab": { 202 | "base_uri": "https://localhost:8080/", 203 | "height": 133 204 | }, 205 | "id": "S-LPaZZ4IQcr", 206 | "outputId": "c67c2cbc-ddfc-4d8f-c812-04c8f79b62ab" 207 | }, 208 | "execution_count": 5, 209 | "outputs": [ 210 | { 211 | "output_type": "error", 212 | "ename": "SyntaxError", 213 | "evalue": "ignored", 214 | "traceback": [ 215 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m foo = 'what's your name?'\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" 216 | ] 217 | } 218 | ] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "source": [ 223 | "This produces SyntaxError. Because Python did not find pairs of single quotes, and hence Python\n", 224 | "could not decide which two single quotes to consider for identifying the string. Now, let’s try\n", 225 | "defining the same string using double quotes." 226 | ], 227 | "metadata": { 228 | "id": "Ue5YVNgbIU1c" 229 | } 230 | }, 231 | { 232 | "cell_type": "code", 233 | "source": [ 234 | "foo = \"what's your name?\"\n", 235 | "print(foo)" 236 | ], 237 | "metadata": { 238 | "colab": { 239 | "base_uri": "https://localhost:8080/" 240 | }, 241 | "id": "WWo6BudQIZVb", 242 | "outputId": "707996c7-8999-48e1-e27f-e60142939f54" 243 | }, 244 | "execution_count": 6, 245 | "outputs": [ 246 | { 247 | "output_type": "stream", 248 | "name": "stdout", 249 | "text": [ 250 | "what's your name?\n" 251 | ] 252 | } 253 | ] 254 | }, 255 | { 256 | "cell_type": "markdown", 257 | "source": [ 258 | "As you might have noticed that double quotes provide an easy way to define strings, which involve single quotes. The same task can be performed using single quote also, by telling Python which of the single quote should not be used to identify the begininning and ending of the string. The \\ is\n", 259 | "used befor null single quote. Lets write the same string by using the single quotes only where we will use \\ before '." 260 | ], 261 | "metadata": { 262 | "id": "O48LleVJIU_C" 263 | } 264 | }, 265 | { 266 | "cell_type": "code", 267 | "source": [ 268 | "foo = 'what\\'s your name?'\n", 269 | "print(foo)" 270 | ], 271 | "metadata": { 272 | "colab": { 273 | "base_uri": "https://localhost:8080/" 274 | }, 275 | "id": "tgkSvqFNI0fd", 276 | "outputId": "4482ebba-7e91-4574-87ba-97352c6dc9fd" 277 | }, 278 | "execution_count": 7, 279 | "outputs": [ 280 | { 281 | "output_type": "stream", 282 | "name": "stdout", 283 | "text": [ 284 | "what's your name?\n" 285 | ] 286 | } 287 | ] 288 | }, 289 | { 290 | "cell_type": "markdown", 291 | "source": [ 292 | "#### Triple Quotes\n", 293 | "When the strings spans over more than one line, triples quotes are best to define them. Multi-line\n", 294 | "strings can also be specified using escape sequence *\\n* in single or double quote strings, however triple quotes make it easier to write. Triple quotes are also useful for other things (making help content for functions) also, which you will read later in the book." 295 | ], 296 | "metadata": { 297 | "id": "PYSxEmNwI7oe" 298 | } 299 | }, 300 | { 301 | "cell_type": "code", 302 | "source": [ 303 | "foo = \"\"\"My name is Sat Kumar.\n", 304 | "I am going to write multiline strings\"\"\"\n", 305 | "print(foo)" 306 | ], 307 | "metadata": { 308 | "colab": { 309 | "base_uri": "https://localhost:8080/" 310 | }, 311 | "id": "t_MT_T1sJO-Q", 312 | "outputId": "90c8db4f-a77c-491a-c9b7-2f7a6f06b018" 313 | }, 314 | "execution_count": 8, 315 | "outputs": [ 316 | { 317 | "output_type": "stream", 318 | "name": "stdout", 319 | "text": [ 320 | "My name is Sat Kumar.\n", 321 | "I am going to write multiline strings\n" 322 | ] 323 | } 324 | ] 325 | } 326 | ] 327 | } -------------------------------------------------------------------------------- /02_03.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyM0HLjKILo2TTDR1+RGegeT", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 2.3 Choosing the name of variable\n", 33 | "Then name of the variables should be meaningful, and possibly should be documented what the variable is used for.\n", 34 | "Variable names can be arbitrarily long. They can contain both letters and numbers, but they have to begin with a letter. It is legal to use uppercase letters, but it is a good idea to begin variable names with a lowercase letter, as conventionally uppercase letters are used to denote classes.\n", 35 | "\n", 36 | "The underscore character (_) can appear in a name. It is often used in names with multiple words, such as *my_name* or *air_velocity*. A variable name can be started with underscore, but should be avoided because this is used for something else conventionally.\n", 37 | "\n", 38 | "Python has some of the reserved keywords, which can not be used as variable name. If the interpreter gives some error about one of your variable names and you don’t know why, you should check if your variable name is not in the reserved keyword list. It is a good idea to remember the list, or keep it handy. But I find it difficulty to remember myself, and in fact even never tried to remember the list. I just type the name of variable, I want to use in python, and look for the output by\n", 39 | "python, and then decide whether to use this name for variable or not." 40 | ], 41 | "metadata": { 42 | "id": "pT_o11yIWBep" 43 | } 44 | }, 45 | { 46 | "cell_type": "code", 47 | "source": [ 48 | "500_mangoes" 49 | ], 50 | "metadata": { 51 | "colab": { 52 | "base_uri": "https://localhost:8080/", 53 | "height": 133 54 | }, 55 | "id": "oC5ICbczWsqu", 56 | "outputId": "f4e9e0b0-2b5d-4ca5-aa64-fbc0652c06da" 57 | }, 58 | "execution_count": 1, 59 | "outputs": [ 60 | { 61 | "output_type": "error", 62 | "ename": "SyntaxError", 63 | "evalue": "ignored", 64 | "traceback": [ 65 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 500_mangoes\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid token\n" 66 | ] 67 | } 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "source": [ 73 | "First variable name 500_mangoes gives SyntaxError, so we cant use this name as variable. " 74 | ], 75 | "metadata": { 76 | "id": "9h5qihfFW3L5" 77 | } 78 | }, 79 | { 80 | "cell_type": "code", 81 | "source": [ 82 | "class" 83 | ], 84 | "metadata": { 85 | "colab": { 86 | "base_uri": "https://localhost:8080/", 87 | "height": 133 88 | }, 89 | "id": "JZeGyh3xW9vM", 90 | "outputId": "695508e6-63c9-4433-8e17-c40e787b7ab6" 91 | }, 92 | "execution_count": 2, 93 | "outputs": [ 94 | { 95 | "output_type": "error", 96 | "ename": "SyntaxError", 97 | "evalue": "ignored", 98 | "traceback": [ 99 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m class\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" 100 | ] 101 | } 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "source": [ 107 | "The *class* gives the SyntaxError too, so it also can not be used. " 108 | ], 109 | "metadata": { 110 | "id": "YjRWTIOJXEvG" 111 | } 112 | }, 113 | { 114 | "cell_type": "code", 115 | "source": [ 116 | "import numpy as np\n", 117 | "np" 118 | ], 119 | "metadata": { 120 | "colab": { 121 | "base_uri": "https://localhost:8080/" 122 | }, 123 | "id": "-ZKtIgooXIh0", 124 | "outputId": "21d78d10-7888-46e7-95b8-4465b202c370" 125 | }, 126 | "execution_count": 4, 127 | "outputs": [ 128 | { 129 | "output_type": "execute_result", 130 | "data": { 131 | "text/plain": [ 132 | "" 133 | ] 134 | }, 135 | "metadata": {}, 136 | "execution_count": 4 137 | } 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "source": [ 143 | "The np gives some output (which means np is referring to something), so if we use this as variable name, the reference will be destroyed (overwritten). " 144 | ], 145 | "metadata": { 146 | "id": "UULJzGgzXV1Z" 147 | } 148 | }, 149 | { 150 | "cell_type": "code", 151 | "source": [ 152 | "foo" 153 | ], 154 | "metadata": { 155 | "colab": { 156 | "base_uri": "https://localhost:8080/", 157 | "height": 169 158 | }, 159 | "id": "0gbPEpzbXbM3", 160 | "outputId": "b6da6afe-b1fb-487c-8ed0-67f9b3ef6599" 161 | }, 162 | "execution_count": 5, 163 | "outputs": [ 164 | { 165 | "output_type": "error", 166 | "ename": "NameError", 167 | "evalue": "ignored", 168 | "traceback": [ 169 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 170 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 171 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfoo\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 172 | "\u001b[0;31mNameError\u001b[0m: name 'foo' is not defined" 173 | ] 174 | } 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "source": [ 180 | "The foo gives NameError that the variable is not defined, this makes it a valid choice for the variable name. Apart from these scenarios, one more output is possible." 181 | ], 182 | "metadata": { 183 | "id": "DQX7aHqRWrqa" 184 | } 185 | }, 186 | { 187 | "cell_type": "code", 188 | "source": [ 189 | "a = 5\n", 190 | "a" 191 | ], 192 | "metadata": { 193 | "colab": { 194 | "base_uri": "https://localhost:8080/" 195 | }, 196 | "id": "OjbtHqTJXvLz", 197 | "outputId": "8c1b2e08-4904-4de4-e9e9-3bf41d2afbec" 198 | }, 199 | "execution_count": 6, 200 | "outputs": [ 201 | { 202 | "output_type": "execute_result", 203 | "data": { 204 | "text/plain": [ 205 | "5" 206 | ] 207 | }, 208 | "metadata": {}, 209 | "execution_count": 6 210 | } 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "source": [ 216 | "This means that variable a is defined before, now it is upto you, if you want to change its old value." 217 | ], 218 | "metadata": { 219 | "id": "fhygQ7W0Xvwh" 220 | } 221 | } 222 | ] 223 | } -------------------------------------------------------------------------------- /02_05.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyN1WhVHRiFvF7gd7T2WW1Dt", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 2.5 Expressions\n", 33 | "\n", 34 | "An expression is a combination of values, variables, and operators. A value is an expression, and so is a variable. Following are all legal expressions (assuming that the variable x has been assigned a value:" 35 | ], 36 | "metadata": { 37 | "id": "-jDvLXZpgiF0" 38 | } 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 1, 43 | "metadata": { 44 | "colab": { 45 | "base_uri": "https://localhost:8080/" 46 | }, 47 | "id": "q_gCvK2Ze0Ss", 48 | "outputId": "a6f14da1-c868-4d35-bc56-1c1be9f917ee" 49 | }, 50 | "outputs": [ 51 | { 52 | "output_type": "execute_result", 53 | "data": { 54 | "text/plain": [ 55 | "32" 56 | ] 57 | }, 58 | "metadata": {}, 59 | "execution_count": 1 60 | } 61 | ], 62 | "source": [ 63 | "x = 17\n", 64 | "x + 15" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "source": [ 70 | "If you type an expression in interactive mode, the interpreter evaluates it and displays the result.\n", 71 | "\n", 72 | "But in a script, an expression all by itself doesn’t do anything! This is a common source of confusion for beginners." 73 | ], 74 | "metadata": { 75 | "id": "KwadNym1hJyN" 76 | } 77 | } 78 | ] 79 | } -------------------------------------------------------------------------------- /02_06.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyMHGZ1Vc3MtKQq0A2MPirFL", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 2.6 Control Flow\n", 33 | "\n", 34 | "If we want to do same task many times, restrict the execution of task only when some condition is met, **control flow** is the way to do it.\n", 35 | "\n", 36 | "### 2.6.1 for\n", 37 | "*for* is used to repeatedly executing a piece of code. It also can be used to iterate over some list. Suppose\n", 38 | "you have some list, and you want to square the each item in list and print." 39 | ], 40 | "metadata": { 41 | "id": "m7uRmpyJgj4a" 42 | } 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 1, 47 | "metadata": { 48 | "colab": { 49 | "base_uri": "https://localhost:8080/" 50 | }, 51 | "id": "gLzgG8U-e4zb", 52 | "outputId": "63381572-762b-4123-8f56-1e2616428415" 53 | }, 54 | "outputs": [ 55 | { 56 | "output_type": "stream", 57 | "name": "stdout", 58 | "text": [ 59 | "25\n", 60 | "121\n", 61 | "196\n", 62 | "0\n", 63 | "36\n", 64 | "0\n", 65 | "64\n" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "foo = [5, 11, 14, 0, 6, 0, 8] # define the list\n", 71 | "for item in foo:\n", 72 | " print(item**2)\n" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "source": [ 78 | "To define which piece of code will be part of a **control flow**, we need to use indentation. Indentation refers to the spaces at the beginning of a code line. We can use any number of white space/s for indentation, however, we should follow the same intendation throughout the code. \n", 79 | "\n", 80 | "Please note that while in other programming languages, indentation is used for readability only, but in Python, indentation is very important as it uses indentation to indicate a block of code.\n", 81 | "\n", 82 | "The item in the list can be iterated by defining another list having integers, and iterating over it. Let\n", 83 | "us try this way to perform the above task." 84 | ], 85 | "metadata": { 86 | "id": "srA3wixliujK" 87 | } 88 | }, 89 | { 90 | "cell_type": "code", 91 | "source": [ 92 | "foo = [5, 11, 14, 0, 6, 0, 8] # define the list\n", 93 | "a = range(7) # define the list having integers\n", 94 | "\n", 95 | "for item in a:\n", 96 | " print(foo[item]**2)" 97 | ], 98 | "metadata": { 99 | "colab": { 100 | "base_uri": "https://localhost:8080/" 101 | }, 102 | "id": "A8dg3EObio_K", 103 | "outputId": "c1505865-81c5-4fe9-b988-3cf8100642e0" 104 | }, 105 | "execution_count": 2, 106 | "outputs": [ 107 | { 108 | "output_type": "stream", 109 | "name": "stdout", 110 | "text": [ 111 | "range(0, 7)\n", 112 | "25\n", 113 | "121\n", 114 | "196\n", 115 | "0\n", 116 | "36\n", 117 | "0\n", 118 | "64\n" 119 | ] 120 | } 121 | ] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "source": [ 126 | "### 2.6.2 while\n", 127 | "\n", 128 | "*while* statement is used, when we want to keep on executing the some code unless some condition is met or violated. Suppose, we want to print some numbers ranging from 15 to 20, we could do like this." 129 | ], 130 | "metadata": { 131 | "id": "879A4M5-j4UR" 132 | } 133 | }, 134 | { 135 | "cell_type": "code", 136 | "source": [ 137 | "n = 15 # initialize the n\n", 138 | "while n<=20:\n", 139 | " print(n)\n", 140 | " n = n+1" 141 | ], 142 | "metadata": { 143 | "colab": { 144 | "base_uri": "https://localhost:8080/" 145 | }, 146 | "id": "2T4TXlTmj-Q-", 147 | "outputId": "8b23ea29-619e-4806-d070-bd44d58acfd8" 148 | }, 149 | "execution_count": 4, 150 | "outputs": [ 151 | { 152 | "output_type": "stream", 153 | "name": "stdout", 154 | "text": [ 155 | "15\n", 156 | "16\n", 157 | "17\n", 158 | "18\n", 159 | "19\n", 160 | "20\n" 161 | ] 162 | } 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "source": [ 168 | "### 2.6.3 if\n", 169 | "*if* statement executes some portion of the code, if the conditions are met, otherwise it skips that\n", 170 | "portion. Suppose you have some list, and you want to compute its inverse, but want to skip if the entry in list is zero:" 171 | ], 172 | "metadata": { 173 | "id": "V7ylGPmZkCDZ" 174 | } 175 | }, 176 | { 177 | "cell_type": "code", 178 | "source": [ 179 | "foo = [5, 11, 14, 0, 6, 0, 8] # define an array\n", 180 | "\n", 181 | "for item in foo:\n", 182 | " if item is not 0:\n", 183 | " print(1/item)" 184 | ], 185 | "metadata": { 186 | "colab": { 187 | "base_uri": "https://localhost:8080/" 188 | }, 189 | "id": "Dy5fck8rkBpo", 190 | "outputId": "1c157664-8d8b-498c-96d8-4c1bf5b28c0e" 191 | }, 192 | "execution_count": 5, 193 | "outputs": [ 194 | { 195 | "output_type": "stream", 196 | "name": "stdout", 197 | "text": [ 198 | "0.2\n", 199 | "0.09090909090909091\n", 200 | "0.07142857142857142\n", 201 | "0.16666666666666666\n", 202 | "0.125\n" 203 | ] 204 | } 205 | ] 206 | }, 207 | { 208 | "cell_type": "markdown", 209 | "source": [ 210 | "The *if-else* allows alternative portions of code to execute depending upon the condition. In *if-else* only one portion of code is executed from the given alternatives. Suppose in the previous example, you want to issue some statement when there is 0 in the list." 211 | ], 212 | "metadata": { 213 | "id": "07ISxLBMzgx1" 214 | } 215 | }, 216 | { 217 | "cell_type": "code", 218 | "source": [ 219 | "foo = [5, 11, 14, 0, 6, 0, 8] # define the array\n", 220 | "\n", 221 | "for item in foo:\n", 222 | " if item is not 0:\n", 223 | " print(1.0/item)\n", 224 | " else:\n", 225 | " print('0 found in list')" 226 | ], 227 | "metadata": { 228 | "colab": { 229 | "base_uri": "https://localhost:8080/" 230 | }, 231 | "id": "vWO4SFrlzzdI", 232 | "outputId": "16e4e979-e71e-479b-ebfe-a305bc563cba" 233 | }, 234 | "execution_count": 7, 235 | "outputs": [ 236 | { 237 | "output_type": "stream", 238 | "name": "stdout", 239 | "text": [ 240 | "0.2\n", 241 | "0.09090909090909091\n", 242 | "0.07142857142857142\n", 243 | "0 found in list\n", 244 | "0.16666666666666666\n", 245 | "0 found in list\n", 246 | "0.125\n" 247 | ] 248 | } 249 | ] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "source": [ 254 | "if-elif-else is used when depending upon the condition, you want to execute some portion of code. You can specify as many conditions you want, and their corresponding code to execute. Lets take one example, suppose we have one list, and we want to print some statement if the item in list is negative, positive or 0." 255 | ], 256 | "metadata": { 257 | "id": "tCYvjlanzxej" 258 | } 259 | }, 260 | { 261 | "cell_type": "code", 262 | "source": [ 263 | "foo = [5, -11, 14, 0, -6, 0, 8] # define the array\n", 264 | "\n", 265 | "for item in foo:\n", 266 | " if item < 0:\n", 267 | " print('item is negative')\n", 268 | " elif(item>0):\n", 269 | " print('item is positive')\n", 270 | " else:\n", 271 | " print('item is 0')" 272 | ], 273 | "metadata": { 274 | "colab": { 275 | "base_uri": "https://localhost:8080/" 276 | }, 277 | "id": "MlO8EVPU0MZ7", 278 | "outputId": "670840cb-e356-48df-c656-75a1de31fa21" 279 | }, 280 | "execution_count": 8, 281 | "outputs": [ 282 | { 283 | "output_type": "stream", 284 | "name": "stdout", 285 | "text": [ 286 | "item is positive\n", 287 | "item is negative\n", 288 | "item is positive\n", 289 | "item is 0\n", 290 | "item is negative\n", 291 | "item is 0\n", 292 | "item is positive\n" 293 | ] 294 | } 295 | ] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "source": [ 300 | "### 2.6.4 break\n", 301 | "The *break* statement, breaks out of the loop. Suppose you want to print all the items in the list, but you want to stop the loop if you encounter 0." 302 | ], 303 | "metadata": { 304 | "id": "fV_7yl7PkHUr" 305 | } 306 | }, 307 | { 308 | "cell_type": "code", 309 | "source": [ 310 | "for item in foo:\n", 311 | " if item==0:\n", 312 | " print('zero found in the list, stopping iterations')\n", 313 | " break\n", 314 | " else:\n", 315 | " print(item)" 316 | ], 317 | "metadata": { 318 | "colab": { 319 | "base_uri": "https://localhost:8080/" 320 | }, 321 | "id": "sefyjysK0ndN", 322 | "outputId": "d99e081d-4c08-4576-877e-e099e9dad0cd" 323 | }, 324 | "execution_count": 9, 325 | "outputs": [ 326 | { 327 | "output_type": "stream", 328 | "name": "stdout", 329 | "text": [ 330 | "5\n", 331 | "-11\n", 332 | "14\n", 333 | "zero found in the list, stopping iterations\n" 334 | ] 335 | } 336 | ] 337 | }, 338 | { 339 | "cell_type": "markdown", 340 | "source": [ 341 | "The break statement becomes useful when you want to want if something strange happens to your program, and in that condition you want to stop the execution." 342 | ], 343 | "metadata": { 344 | "id": "T7c4rh2w0xqP" 345 | } 346 | }, 347 | { 348 | "cell_type": "markdown", 349 | "source": [ 350 | "### 2.6.5 continue\n", 351 | "The continue statement provides opportunity to jump out of the current loop (iteration) when some condition is met. Suppose you do not want to print items in the list which are negative." 352 | ], 353 | "metadata": { 354 | "id": "LSssQ5tWkPSS" 355 | } 356 | }, 357 | { 358 | "cell_type": "code", 359 | "source": [ 360 | "foo = [5, -11, 14, 0, -6, 0, 8] # define the array\n", 361 | "\n", 362 | "for item in foo:\n", 363 | " if item<0:\n", 364 | " continue\n", 365 | " print(item)" 366 | ], 367 | "metadata": { 368 | "colab": { 369 | "base_uri": "https://localhost:8080/" 370 | }, 371 | "id": "58P0yZWE1CRP", 372 | "outputId": "abed6084-f57b-4deb-f2d2-bca5e8e23218" 373 | }, 374 | "execution_count": 10, 375 | "outputs": [ 376 | { 377 | "output_type": "stream", 378 | "name": "stdout", 379 | "text": [ 380 | "5\n", 381 | "14\n", 382 | "0\n", 383 | "0\n", 384 | "8\n" 385 | ] 386 | } 387 | ] 388 | }, 389 | { 390 | "cell_type": "markdown", 391 | "source": [ 392 | "### 2.6.6 pass\n", 393 | "The *pass* statement does nothing. It can be used when a statement is required syntactically but the\n", 394 | "program requires no action." 395 | ], 396 | "metadata": { 397 | "id": "lCneltU-kQDf" 398 | } 399 | }, 400 | { 401 | "cell_type": "code", 402 | "source": [ 403 | "foo = [5, -11, 14, 0, -6, 0, 8] # define the array\n", 404 | "\n", 405 | "for item in foo:\n", 406 | " pass" 407 | ], 408 | "metadata": { 409 | "id": "Zlz7UCB01a2q" 410 | }, 411 | "execution_count": 11, 412 | "outputs": [] 413 | }, 414 | { 415 | "cell_type": "markdown", 416 | "source": [ 417 | "This is often used, when you are developing your code, and intent to put something later. If you leave without pass, Python will issue the error." 418 | ], 419 | "metadata": { 420 | "id": "WUzqDR7G1beN" 421 | } 422 | } 423 | ] 424 | } -------------------------------------------------------------------------------- /02_07.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyPIc1XTZIRAlG7YVNQzuVgA", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "# 2.7 Function\n", 33 | "\n", 34 | "Function is a some sequence of statements that does some processing. When you define a function,\n", 35 | "you specify the name and the sequence of statements. Later, you can call the function by name.\n", 36 | "\n", 37 | "There are many built in functions in the Python, and each library provides some functions. You can also specify your functions. When you need to do some task many times, it is better to define function to do that task, and later call the function. The thumb rule is that, whenever you feel to\n", 38 | "define one function define it." 39 | ], 40 | "metadata": { 41 | "id": "DJtqiSr2gqgr" 42 | } 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "source": [ 47 | "### 2.7.1 In-built functions\n", 48 | "Python has some in-built functions, some of them we have already used." 49 | ], 50 | "metadata": { 51 | "id": "YeDWmfxjkY9k" 52 | } 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 2, 57 | "metadata": { 58 | "colab": { 59 | "base_uri": "https://localhost:8080/" 60 | }, 61 | "id": "kh9oIcDGe9WA", 62 | "outputId": "6d3d2aad-9d2a-4086-b490-76bfeeed9ff5" 63 | }, 64 | "outputs": [ 65 | { 66 | "output_type": "stream", 67 | "name": "stdout", 68 | "text": [ 69 | "\n", 70 | "7\n", 71 | "14\n" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "foo = [5, -11, 14, 0, -6, 0, 8] # define an array\n", 77 | "print(type(foo))\n", 78 | "print(len(foo))\n", 79 | "print(max(foo))" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "source": [ 85 | "Here, *type*, *len*, and *max* are in-built functions, which returns the type, length of the list, and maximum\n", 86 | "value in the list, respectively" 87 | ], 88 | "metadata": { 89 | "id": "S4RrTqkEtGZq" 90 | } 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "source": [ 95 | "### 2.7.2 User defines functions\n", 96 | "If you do not find the function that you intent to use, you can define one. In fact, it is a good practice to define functions whenever they are needed, it increase the readability of the codes. A function definition specifies the name of a new function and the sequence of statements that execute when the function is called. Suppose, you want to define a function which adds the 2 into the input." 97 | ], 98 | "metadata": { 99 | "id": "yQgUx-k7kZk5" 100 | } 101 | }, 102 | { 103 | "cell_type": "code", 104 | "source": [ 105 | "def add2(temp):\n", 106 | " return(temp+2)\n", 107 | "\n", 108 | "add2(5)" 109 | ], 110 | "metadata": { 111 | "colab": { 112 | "base_uri": "https://localhost:8080/" 113 | }, 114 | "id": "XL4_PH11utm-", 115 | "outputId": "3b8c8676-8509-4f46-f6c6-762a0149959d" 116 | }, 117 | "execution_count": 3, 118 | "outputs": [ 119 | { 120 | "output_type": "execute_result", 121 | "data": { 122 | "text/plain": [ 123 | "7" 124 | ] 125 | }, 126 | "metadata": {}, 127 | "execution_count": 3 128 | } 129 | ] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "source": [ 134 | "For functions also, we need to follow the indentation. In the above function, we are using two whitespace for the indentation. When we do not use indentation, it tells Python, that now the function is completed. The *return* defines the output from the function. *return* is optional, some functions may not return anything." 135 | ], 136 | "metadata": { 137 | "id": "hNkvOjAbvCvB" 138 | } 139 | }, 140 | { 141 | "cell_type": "code", 142 | "source": [ 143 | "import numpy as np\n", 144 | "foo = np.array( [5, -11, 14, 0, -6, 0, 8]) # define an array\n", 145 | "new_foo = add2(foo)\n", 146 | "print(new_foo)" 147 | ], 148 | "metadata": { 149 | "colab": { 150 | "base_uri": "https://localhost:8080/" 151 | }, 152 | "id": "47WrrNOAvA1b", 153 | "outputId": "09180b8a-5b1d-4fdf-8bfa-2af3dbfbf17e" 154 | }, 155 | "execution_count": 4, 156 | "outputs": [ 157 | { 158 | "output_type": "stream", 159 | "name": "stdout", 160 | "text": [ 161 | "[ 7 -9 16 2 -4 2 10]\n" 162 | ] 163 | } 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "source": [ 169 | "Function can take any input type and can return any output type. Here, the input and output are numpy array. " 170 | ], 171 | "metadata": { 172 | "id": "fHv5bj_yvd64" 173 | } 174 | }, 175 | { 176 | "cell_type": "code", 177 | "source": [ 178 | "foo = [5, -11, 14, 0, -6, 0, 8] # define the array\n", 179 | "\n", 180 | "def add2(temp):\n", 181 | " print(temp[-1]+2) # add 2 to only the last entry in the list\n", 182 | "\n", 183 | "print(add2(foo))\n", 184 | "\n", 185 | "new_foo = add2(foo)\n", 186 | "print(new_foo)" 187 | ], 188 | "metadata": { 189 | "colab": { 190 | "base_uri": "https://localhost:8080/" 191 | }, 192 | "id": "DamZTqmeu6Ma", 193 | "outputId": "c45a2bd0-2f93-43ec-93f7-6b4c311f649d" 194 | }, 195 | "execution_count": 6, 196 | "outputs": [ 197 | { 198 | "output_type": "stream", 199 | "name": "stdout", 200 | "text": [ 201 | "10\n", 202 | "None\n", 203 | "10\n", 204 | "None\n" 205 | ] 206 | } 207 | ] 208 | }, 209 | { 210 | "cell_type": "markdown", 211 | "source": [ 212 | "This is clear from this example that functions need not to return any values. Like in this example,\n", 213 | "function only print the last entry of the list after adding 2 to it, and returns none." 214 | ], 215 | "metadata": { 216 | "id": "nmvhYOK-kdAI" 217 | } 218 | } 219 | ] 220 | } -------------------------------------------------------------------------------- /03_01.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyOWfcQdK9Ocmbpbcb4q43xH", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "# 3 Array and dataframe\n", 33 | "\n", 34 | "When we need to store more than one value of a variable e.g. daily temperature over a long time period, or data over space and sometime in space and time, we can make use of array to store this data. Array can be more than 1-dimensional. \n", 35 | "\n", 36 | "When we store multiple variables, DataFrame are a useful structure.\n", 37 | "\n", 38 | "## 3.1 Generating sequential arrays\n", 39 | "Often we need vectors whose elements follow a simple order, for example a vector containing elements:\n", 40 | "* [10, 11, 12, 13], or \n", 41 | "* [5, 10, 15, 20], or\n", 42 | "* [1.0, 1.2, 1.4, 1.6, 1.8, 2.0]. \n", 43 | "\n", 44 | "We see that in these vectors, items follow some simple order, so it would be nicer if there are easy way to define these kinds of vectors. Some of the way to create these vectors are following: \n", 45 | "\n", 46 | "### 3.1.1 linspace\n", 47 | "If we are interested in generating the vector, whose elements are uniformly spaced and we know the upper, lower limit and the number of elements, then in that case linspace is the preferred choice." 48 | ], 49 | "metadata": { 50 | "id": "T68NFlxX_Vii" 51 | } 52 | }, 53 | { 54 | "cell_type": "code", 55 | "source": [ 56 | "import numpy as np\n", 57 | "np.linspace(0, 2, 9)" 58 | ], 59 | "metadata": { 60 | "colab": { 61 | "base_uri": "https://localhost:8080/" 62 | }, 63 | "id": "PnUTBOWwARuo", 64 | "outputId": "f606f4af-2d56-46bd-e144-e68e245c5b5e" 65 | }, 66 | "execution_count": 1, 67 | "outputs": [ 68 | { 69 | "output_type": "execute_result", 70 | "data": { 71 | "text/plain": [ 72 | "array([0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ])" 73 | ] 74 | }, 75 | "metadata": {}, 76 | "execution_count": 1 77 | } 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "source": [ 83 | "Because linspace lies in numpy library, so first we have imported the library and have given it an abbreviated name. Then we call the linspace with lower limit, upper limit and the number of element to be generated. In this example, 0 is the lower limit, 2 is the upper limit, and number of elements are 9. \n", 84 | "\n", 85 | "Let us generate one more vector to understand more about this function, this time we take lower limit as 0, upper limit as $2\\pi$, and number of elements to be 100." 86 | ], 87 | "metadata": { 88 | "id": "2AC3HirPBxZi" 89 | } 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 4, 94 | "metadata": { 95 | "colab": { 96 | "base_uri": "https://localhost:8080/" 97 | }, 98 | "id": "SLMjVnjp_TYA", 99 | "outputId": "905fb436-9b18-4636-b7c1-f519ef47d66b" 100 | }, 101 | "outputs": [ 102 | { 103 | "output_type": "stream", 104 | "name": "stdout", 105 | "text": [ 106 | "[0. 0.06346652 0.12693304 0.19039955 0.25386607 0.31733259\n", 107 | " 0.38079911 0.44426563 0.50773215 0.57119866 0.63466518 0.6981317\n", 108 | " 0.76159822 0.82506474 0.88853126 0.95199777 1.01546429 1.07893081\n", 109 | " 1.14239733 1.20586385 1.26933037 1.33279688 1.3962634 1.45972992\n", 110 | " 1.52319644 1.58666296 1.65012947 1.71359599 1.77706251 1.84052903\n", 111 | " 1.90399555 1.96746207 2.03092858 2.0943951 2.15786162 2.22132814\n", 112 | " 2.28479466 2.34826118 2.41172769 2.47519421 2.53866073 2.60212725\n", 113 | " 2.66559377 2.72906028 2.7925268 2.85599332 2.91945984 2.98292636\n", 114 | " 3.04639288 3.10985939 3.17332591 3.23679243 3.30025895 3.36372547\n", 115 | " 3.42719199 3.4906585 3.55412502 3.61759154 3.68105806 3.74452458\n", 116 | " 3.8079911 3.87145761 3.93492413 3.99839065 4.06185717 4.12532369\n", 117 | " 4.1887902 4.25225672 4.31572324 4.37918976 4.44265628 4.5061228\n", 118 | " 4.56958931 4.63305583 4.69652235 4.75998887 4.82345539 4.88692191\n", 119 | " 4.95038842 5.01385494 5.07732146 5.14078798 5.2042545 5.26772102\n", 120 | " 5.33118753 5.39465405 5.45812057 5.52158709 5.58505361 5.64852012\n", 121 | " 5.71198664 5.77545316 5.83891968 5.9023862 5.96585272 6.02931923\n", 122 | " 6.09278575 6.15625227 6.21971879 6.28318531]\n" 123 | ] 124 | } 125 | ], 126 | "source": [ 127 | "x = np.linspace(0, 2*np.pi, 100)\n", 128 | "print(x)" 129 | ] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "source": [ 134 | "By default the number of elements are 50, so if we do not specify the number of elements, we get 50 elements with equal spacing. We can use len function to get the length of any array." 135 | ], 136 | "metadata": { 137 | "id": "wNa0w8VCCHAp" 138 | } 139 | }, 140 | { 141 | "cell_type": "code", 142 | "source": [ 143 | "foo = np.linspace(0,1)\n", 144 | "len(foo)" 145 | ], 146 | "metadata": { 147 | "colab": { 148 | "base_uri": "https://localhost:8080/" 149 | }, 150 | "id": "aSuOE_brCS74", 151 | "outputId": "a8fbec2e-4c40-46f4-e616-0c17d96f7056" 152 | }, 153 | "execution_count": 5, 154 | "outputs": [ 155 | { 156 | "output_type": "execute_result", 157 | "data": { 158 | "text/plain": [ 159 | "50" 160 | ] 161 | }, 162 | "metadata": {}, 163 | "execution_count": 5 164 | } 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "source": [ 170 | "### 3.1.2 arange\n", 171 | "Suppose again we want to generate a vector whose elements are uniformly spaced, but this time we do not know the number of elements, we just know the increment between elements. In such situation arange is used. arange also requires lower and upper bounds. In the following example we are generating the vector having lower element as 10, upper element as 30 and having an increment of 30. So from the knowledge of linspace we will do something like this.\n" 172 | ], 173 | "metadata": { 174 | "id": "aiNqnIOdCZK4" 175 | } 176 | }, 177 | { 178 | "cell_type": "code", 179 | "source": [ 180 | "np.arange(10, 30, 5)" 181 | ], 182 | "metadata": { 183 | "colab": { 184 | "base_uri": "https://localhost:8080/" 185 | }, 186 | "id": "ewP_baz8CgWg", 187 | "outputId": "ceacf5c2-95f6-449e-ed86-4617c6bbef86" 188 | }, 189 | "execution_count": 6, 190 | "outputs": [ 191 | { 192 | "output_type": "execute_result", 193 | "data": { 194 | "text/plain": [ 195 | "array([10, 15, 20, 25])" 196 | ] 197 | }, 198 | "metadata": {}, 199 | "execution_count": 6 200 | } 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "source": [ 206 | "Oh! What happened? Why did Python not print 30. Because arange function does not include second argument in the elements. So we want to print upto 30, we would do." 207 | ], 208 | "metadata": { 209 | "id": "zs9ZsXpQCn88" 210 | } 211 | }, 212 | { 213 | "cell_type": "code", 214 | "source": [ 215 | "np.arange(10, 31, 5)" 216 | ], 217 | "metadata": { 218 | "colab": { 219 | "base_uri": "https://localhost:8080/" 220 | }, 221 | "id": "AYtTSoNKCvSe", 222 | "outputId": "90da6c9c-5e89-4ec4-d614-6e4b98355df4" 223 | }, 224 | "execution_count": 7, 225 | "outputs": [ 226 | { 227 | "output_type": "execute_result", 228 | "data": { 229 | "text/plain": [ 230 | "array([10, 15, 20, 25, 30])" 231 | ] 232 | }, 233 | "metadata": {}, 234 | "execution_count": 7 235 | } 236 | ] 237 | }, 238 | { 239 | "cell_type": "markdown", 240 | "source": [ 241 | "This time we get the required output. The arange can also take a float increment. Let us generate some vector with lower bound of 0, upper bound of 2 and with an increment of 0.3." 242 | ], 243 | "metadata": { 244 | "id": "rz3Bkw_5CxMG" 245 | } 246 | }, 247 | { 248 | "cell_type": "code", 249 | "source": [ 250 | "np.arange(0, 2, 0.3) # it accepts float arguments" 251 | ], 252 | "metadata": { 253 | "colab": { 254 | "base_uri": "https://localhost:8080/" 255 | }, 256 | "id": "a0CZQ4y4C5k1", 257 | "outputId": "1757cffc-e20a-4c8d-fa23-cfb67676cdae" 258 | }, 259 | "execution_count": 8, 260 | "outputs": [ 261 | { 262 | "output_type": "execute_result", 263 | "data": { 264 | "text/plain": [ 265 | "array([0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])" 266 | ] 267 | }, 268 | "metadata": {}, 269 | "execution_count": 8 270 | } 271 | ] 272 | }, 273 | { 274 | "cell_type": "markdown", 275 | "source": [ 276 | "In the case of float increment also, the maximum value of generated elements is lesser than the second argument given to the arange.\n", 277 | "\n", 278 | "### 3.1.3 zeros\n", 279 | "zeros is used when we want to generate all the items in vector as 0." 280 | ], 281 | "metadata": { 282 | "id": "oWSvrwK3C8My" 283 | } 284 | }, 285 | { 286 | "cell_type": "code", 287 | "source": [ 288 | "foo = np.zeros(5)\n", 289 | "print(foo)" 290 | ], 291 | "metadata": { 292 | "colab": { 293 | "base_uri": "https://localhost:8080/" 294 | }, 295 | "id": "G4907gLQDREu", 296 | "outputId": "71591076-5664-4a99-80aa-93ca4e8bcb4b" 297 | }, 298 | "execution_count": 9, 299 | "outputs": [ 300 | { 301 | "output_type": "stream", 302 | "name": "stdout", 303 | "text": [ 304 | "[0. 0. 0. 0. 0.]\n" 305 | ] 306 | } 307 | ] 308 | }, 309 | { 310 | "cell_type": "markdown", 311 | "source": [ 312 | "### 3.1.4 ones\n", 313 | "ones is used when all the required elements in vector are 1. Let us say, we want to generate a variable foo which has all the elements equal to one, and has the dimension of 3×2." 314 | ], 315 | "metadata": { 316 | "id": "VOaJeU-3DZaF" 317 | } 318 | }, 319 | { 320 | "cell_type": "code", 321 | "source": [ 322 | "foo = np.ones((3,2))\n", 323 | "foo" 324 | ], 325 | "metadata": { 326 | "colab": { 327 | "base_uri": "https://localhost:8080/" 328 | }, 329 | "id": "XntwkEnQEVWY", 330 | "outputId": "f2742c32-3fff-44c5-8efe-d7226de3aa94" 331 | }, 332 | "execution_count": 10, 333 | "outputs": [ 334 | { 335 | "output_type": "execute_result", 336 | "data": { 337 | "text/plain": [ 338 | "array([[1., 1.],\n", 339 | " [1., 1.],\n", 340 | " [1., 1.]])" 341 | ] 342 | }, 343 | "metadata": {}, 344 | "execution_count": 10 345 | } 346 | ] 347 | }, 348 | { 349 | "cell_type": "markdown", 350 | "source": [ 351 | "Remember that if the number of dimensions are more than one, the dimension are given as tuple, e.g. (2,5).\n", 352 | "\n", 353 | "\n", 354 | "### 3.1.5 empty\n", 355 | "*empty* is useful in initializing the variables. This assigns the garbage values to the elements, which are to be modified/updated later." 356 | ], 357 | "metadata": { 358 | "id": "At59jspYEZYc" 359 | } 360 | }, 361 | { 362 | "cell_type": "code", 363 | "source": [ 364 | "foo = np.empty((2,5))\n", 365 | "foo" 366 | ], 367 | "metadata": { 368 | "colab": { 369 | "base_uri": "https://localhost:8080/" 370 | }, 371 | "id": "JZiOiQJqEqWe", 372 | "outputId": "bd6f1982-56ad-4bc5-b87b-bdfe42ec4d35" 373 | }, 374 | "execution_count": 11, 375 | "outputs": [ 376 | { 377 | "output_type": "execute_result", 378 | "data": { 379 | "text/plain": [ 380 | "array([[1.27430123e-316, 1.77863633e-322, 0.00000000e+000,\n", 381 | " 0.00000000e+000, 0.00000000e+000],\n", 382 | " [2.23289529e+180, 1.74254942e-076, 4.95643980e-090,\n", 383 | " 6.28756268e-066, 3.99473231e-315]])" 384 | ] 385 | }, 386 | "metadata": {}, 387 | "execution_count": 11 388 | } 389 | ] 390 | }, 391 | { 392 | "cell_type": "markdown", 393 | "source": [ 394 | "Additionally in zeros, ones, empty, the data type (e.g. int, float etc.) also can be defined." 395 | ], 396 | "metadata": { 397 | "id": "NJJFpQqFEvSx" 398 | } 399 | }, 400 | { 401 | "cell_type": "code", 402 | "source": [ 403 | "foo = np.empty((2,5),int)\n", 404 | "foo" 405 | ], 406 | "metadata": { 407 | "colab": { 408 | "base_uri": "https://localhost:8080/" 409 | }, 410 | "id": "DV6K58T9ExoE", 411 | "outputId": "82a90600-d280-42c1-aad8-d0d3e0863e64" 412 | }, 413 | "execution_count": 12, 414 | "outputs": [ 415 | { 416 | "output_type": "execute_result", 417 | "data": { 418 | "text/plain": [ 419 | "array([[ 25792144, 36, 0,\n", 420 | " 0, 0],\n", 421 | " [7305181858796548918, 3473451130437056301, 3270793523756872760,\n", 422 | " 3631361885789761844, 808542821]])" 423 | ] 424 | }, 425 | "metadata": {}, 426 | "execution_count": 12 427 | } 428 | ] 429 | }, 430 | { 431 | "cell_type": "markdown", 432 | "source": [ 433 | "You can see that all the elements of foo are now integer, even though the values are useless.\n", 434 | "\n", 435 | "### 3.1.6 rand\n", 436 | "*rand* is used to generate uniformly distributed random variables over the range of 0 to 1." 437 | ], 438 | "metadata": { 439 | "id": "hjavygi6E1Vh" 440 | } 441 | }, 442 | { 443 | "cell_type": "code", 444 | "source": [ 445 | "foo = np.random.rand(3,2)\n", 446 | "foo" 447 | ], 448 | "metadata": { 449 | "colab": { 450 | "base_uri": "https://localhost:8080/" 451 | }, 452 | "id": "5NaLhy1nE_fA", 453 | "outputId": "f032ede2-c83b-4605-c9d5-8fd33fc6f19e" 454 | }, 455 | "execution_count": 13, 456 | "outputs": [ 457 | { 458 | "output_type": "execute_result", 459 | "data": { 460 | "text/plain": [ 461 | "array([[0.0603704 , 0.24580037],\n", 462 | " [0.2110914 , 0.96110024],\n", 463 | " [0.9935456 , 0.33570921]])" 464 | ] 465 | }, 466 | "metadata": {}, 467 | "execution_count": 13 468 | } 469 | ] 470 | }, 471 | { 472 | "cell_type": "markdown", 473 | "source": [ 474 | "### 3.1.7 randn\n", 475 | "*randn* is used to generate random variable having normal distribution with mean equal to zero and variance equal to one." 476 | ], 477 | "metadata": { 478 | "id": "aut8_RDVFGHv" 479 | } 480 | }, 481 | { 482 | "cell_type": "code", 483 | "source": [ 484 | "foo = np.random.randn(2,4)\n", 485 | "foo" 486 | ], 487 | "metadata": { 488 | "colab": { 489 | "base_uri": "https://localhost:8080/" 490 | }, 491 | "id": "s9zpNPDGFNsG", 492 | "outputId": "8b18527a-0478-4ee6-a101-966e5413ed9d" 493 | }, 494 | "execution_count": 14, 495 | "outputs": [ 496 | { 497 | "output_type": "execute_result", 498 | "data": { 499 | "text/plain": [ 500 | "array([[ 0.90902554, 0.43302782, -0.38472946, -0.56662238],\n", 501 | " [ 0.08164287, 0.30499331, 1.934709 , -0.60454243]])" 502 | ] 503 | }, 504 | "metadata": {}, 505 | "execution_count": 14 506 | } 507 | ] 508 | } 509 | ] 510 | } -------------------------------------------------------------------------------- /03_03.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyNfHLDwAZiad9upJCYYJVNW", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 3.3 Indexing\n", 33 | "In this section, we will discuss how to refer to some elements in the numpy array. Remember that in Python first indices is 0. We shall generate some array, say some array whose elements are powered to 3 of the sequence [0,1, ..., 9]." 34 | ], 35 | "metadata": { 36 | "id": "m4Wh6Cp8GrBT" 37 | } 38 | }, 39 | { 40 | "cell_type": "code", 41 | "source": [ 42 | "import numpy as np\n", 43 | "foo = np.arange(10)**3\n", 44 | "foo" 45 | ], 46 | "metadata": { 47 | "colab": { 48 | "base_uri": "https://localhost:8080/" 49 | }, 50 | "id": "w7TR6WxN8z0Z", 51 | "outputId": "0d63d4dc-f7f1-403d-ebe1-a61b33ebf052" 52 | }, 53 | "execution_count": 2, 54 | "outputs": [ 55 | { 56 | "output_type": "execute_result", 57 | "data": { 58 | "text/plain": [ 59 | "array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729])" 60 | ] 61 | }, 62 | "metadata": {}, 63 | "execution_count": 2 64 | } 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "source": [ 70 | "Print the third item in the array. Third item means we need to put indices as 2." 71 | ], 72 | "metadata": { 73 | "id": "hhWdJZno8vSE" 74 | } 75 | }, 76 | { 77 | "cell_type": "code", 78 | "source": [ 79 | "foo[2]" 80 | ], 81 | "metadata": { 82 | "colab": { 83 | "base_uri": "https://localhost:8080/" 84 | }, 85 | "id": "SgvZCPHM8sg_", 86 | "outputId": "7dd185f7-9aa2-495a-ac5b-34408614319c" 87 | }, 88 | "execution_count": 3, 89 | "outputs": [ 90 | { 91 | "output_type": "execute_result", 92 | "data": { 93 | "text/plain": [ 94 | "8" 95 | ] 96 | }, 97 | "metadata": {}, 98 | "execution_count": 3 99 | } 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "source": [ 105 | "Suppose, we would like to print some sequence of array, say at indices of 2, 3, and 4." 106 | ], 107 | "metadata": { 108 | "id": "CpGm6LVN8qqM" 109 | } 110 | }, 111 | { 112 | "cell_type": "code", 113 | "source": [ 114 | "foo[2:5]" 115 | ], 116 | "metadata": { 117 | "colab": { 118 | "base_uri": "https://localhost:8080/" 119 | }, 120 | "id": "qgWNQvae8e6I", 121 | "outputId": "36d129d7-9fe6-4a1f-e150-3aa6aab43904" 122 | }, 123 | "execution_count": 4, 124 | "outputs": [ 125 | { 126 | "output_type": "execute_result", 127 | "data": { 128 | "text/plain": [ 129 | "array([ 8, 27, 64])" 130 | ] 131 | }, 132 | "metadata": {}, 133 | "execution_count": 4 134 | } 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "source": [ 140 | "We used 2:5 to get the values at indices of 2, 3 and 4. This is same as saying that foo[np.arange(2,5,1)]. When we do not specify the third value in the indices for array, it is\n", 141 | "by default taken as 1. If we want to print value at 2 to 8, with an interval of 3. Now because the\n", 142 | "interval is not 1, so we need to define it." 143 | ], 144 | "metadata": { 145 | "id": "sQFtRIx_8cNu" 146 | } 147 | }, 148 | { 149 | "cell_type": "code", 150 | "source": [ 151 | "foo[2:10:3]" 152 | ], 153 | "metadata": { 154 | "colab": { 155 | "base_uri": "https://localhost:8080/" 156 | }, 157 | "id": "FLXHbPHv8X2x", 158 | "outputId": "007432f7-cef4-442e-a59a-32eae9cf0e88" 159 | }, 160 | "execution_count": 5, 161 | "outputs": [ 162 | { 163 | "output_type": "execute_result", 164 | "data": { 165 | "text/plain": [ 166 | "array([ 8, 125, 512])" 167 | ] 168 | }, 169 | "metadata": {}, 170 | "execution_count": 5 171 | } 172 | ] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "source": [ 177 | "If we leave the first entry in the index as blank i.e. to get array elements form the beginning of array\n", 178 | "with an interval of 2 and upto 6, we issue the following command:" 179 | ], 180 | "metadata": { 181 | "id": "Wphr0CL98Vef" 182 | } 183 | }, 184 | { 185 | "cell_type": "code", 186 | "source": [ 187 | "foo[:6:2] # gives the element at 0,2,4" 188 | ], 189 | "metadata": { 190 | "colab": { 191 | "base_uri": "https://localhost:8080/" 192 | }, 193 | "id": "_K2JMbgT8RRt", 194 | "outputId": "0ad2c153-e940-4003-f8d2-7492daf0a7d8" 195 | }, 196 | "execution_count": 6, 197 | "outputs": [ 198 | { 199 | "output_type": "execute_result", 200 | "data": { 201 | "text/plain": [ 202 | "array([ 0, 8, 64])" 203 | ] 204 | }, 205 | "metadata": {}, 206 | "execution_count": 6 207 | } 208 | ] 209 | }, 210 | { 211 | "cell_type": "markdown", 212 | "source": [ 213 | "We get element upto the indices of 4, because arange does not go upto the second argument. We\n", 214 | "can use indices also to modify the existing elements in the array, in the same way as we accessed\n", 215 | "them. Let us replace the existing value of elements at 0,2 and 4 indices, by -1000." 216 | ], 217 | "metadata": { 218 | "id": "BecthTp_8Oz5" 219 | } 220 | }, 221 | { 222 | "cell_type": "code", 223 | "source": [ 224 | "foo[:6:2] = -1000 # modify the elements at 0,2,4\n", 225 | "foo" 226 | ], 227 | "metadata": { 228 | "colab": { 229 | "base_uri": "https://localhost:8080/" 230 | }, 231 | "id": "o-Vo9BbI8MA_", 232 | "outputId": "d8c6e3af-75b6-49e0-91a1-6325f77d5ca8" 233 | }, 234 | "execution_count": 7, 235 | "outputs": [ 236 | { 237 | "output_type": "execute_result", 238 | "data": { 239 | "text/plain": [ 240 | "array([-1000, 1, -1000, 27, -1000, 125, 216, 343, 512,\n", 241 | " 729])" 242 | ] 243 | }, 244 | "metadata": {}, 245 | "execution_count": 7 246 | } 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "source": [ 252 | "We get the last elements of an array by indices -1. We can also use this to reverse the array, by\n", 253 | "giving the increment of -1." 254 | ], 255 | "metadata": { 256 | "id": "N7rMzQqd8HON" 257 | } 258 | }, 259 | { 260 | "cell_type": "code", 261 | "source": [ 262 | "foo[::-1] # reverse an array" 263 | ], 264 | "metadata": { 265 | "colab": { 266 | "base_uri": "https://localhost:8080/" 267 | }, 268 | "id": "5_SIMJk-78Q8", 269 | "outputId": "9164210b-6c75-4f4b-89f6-27f598866638" 270 | }, 271 | "execution_count": 8, 272 | "outputs": [ 273 | { 274 | "output_type": "execute_result", 275 | "data": { 276 | "text/plain": [ 277 | "array([ 729, 512, 343, 216, 125, -1000, 27, -1000, 1,\n", 278 | " -1000])" 279 | ] 280 | }, 281 | "metadata": {}, 282 | "execution_count": 8 283 | } 284 | ] 285 | }, 286 | { 287 | "cell_type": "markdown", 288 | "source": [ 289 | "We can perform the calculation on entire numpy array at once. Suppose we are interested in estimating the square root of the numpy array, we can use sqrt function of numpy library." 290 | ], 291 | "metadata": { 292 | "id": "hYazxh5U8B-i" 293 | } 294 | }, 295 | { 296 | "cell_type": "code", 297 | "source": [ 298 | "np.sqrt(foo) # compute the square root" 299 | ], 300 | "metadata": { 301 | "colab": { 302 | "base_uri": "https://localhost:8080/" 303 | }, 304 | "id": "xF-8JDXB7nRZ", 305 | "outputId": "c460cf78-9ed8-4caa-8457-826d591f3063" 306 | }, 307 | "execution_count": 9, 308 | "outputs": [ 309 | { 310 | "output_type": "stream", 311 | "name": "stderr", 312 | "text": [ 313 | "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in sqrt\n", 314 | " \"\"\"Entry point for launching an IPython kernel.\n" 315 | ] 316 | }, 317 | { 318 | "output_type": "execute_result", 319 | "data": { 320 | "text/plain": [ 321 | "array([ nan, 1. , nan, 5.19615242, nan,\n", 322 | " 11.18033989, 14.69693846, 18.52025918, 22.627417 , 27. ])" 323 | ] 324 | }, 325 | "metadata": {}, 326 | "execution_count": 9 327 | } 328 | ] 329 | }, 330 | { 331 | "cell_type": "markdown", 332 | "source": [ 333 | "*nan* represents that the element is ‘Not A Number’. \n", 334 | "So when the value of element is negative the output of sqrt become nan. The Warning issued by Python tells that there were some invalid values in the input for which sqrt can not produce any sensible output, and it provides warning (not errors).\n", 335 | "In reality, the square root of negative number is complex number, but because we did not define the variable as complex, numpy can not perform operations of complex numbers on this. We need library which handles complex number for such situation." 336 | ], 337 | "metadata": { 338 | "id": "Sy5qVs5F7g7s" 339 | } 340 | } 341 | ] 342 | } -------------------------------------------------------------------------------- /03_04.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyOoQ14Ghumaiuidn4DNAZwF", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 3.4 Array Manipulation\n", 33 | "Often we need to change the array, transpose it, get some elements, or change some elements. This is illustrated by this example, in which first we create the array and then play with it. We have already seen in the previous section, that we can change the value of any element by calling it by the indices, and then assigning new value to it. First, we generate normally distributed random number of size (2×5) to create an array, which we would like to manipulate." 34 | ], 35 | "metadata": { 36 | "id": "9cX6vG4fG1bb" 37 | } 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 2, 42 | "metadata": { 43 | "colab": { 44 | "base_uri": "https://localhost:8080/" 45 | }, 46 | "id": "ipuOCE03G041", 47 | "outputId": "1f269772-42ef-4cd8-b940-81da3b554fdc" 48 | }, 49 | "outputs": [ 50 | { 51 | "output_type": "execute_result", 52 | "data": { 53 | "text/plain": [ 54 | "array([[ 0.36283246, 2.61402225, -1.04827498],\n", 55 | " [-0.49774083, -0.52976937, 0.75586423]])" 56 | ] 57 | }, 58 | "metadata": {}, 59 | "execution_count": 2 60 | } 61 | ], 62 | "source": [ 63 | "import numpy as np\n", 64 | "foo = np.random.randn(2,3)\n", 65 | "foo" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "source": [ 71 | "The array is transposed using T attributes." 72 | ], 73 | "metadata": { 74 | "id": "CcOZwA9mHwMu" 75 | } 76 | }, 77 | { 78 | "cell_type": "code", 79 | "source": [ 80 | "foo.T" 81 | ], 82 | "metadata": { 83 | "colab": { 84 | "base_uri": "https://localhost:8080/" 85 | }, 86 | "id": "MeVZ4F95Lngz", 87 | "outputId": "27f4a4c0-69e6-434f-8601-c188aa00aacd" 88 | }, 89 | "execution_count": 3, 90 | "outputs": [ 91 | { 92 | "output_type": "execute_result", 93 | "data": { 94 | "text/plain": [ 95 | "array([[ 0.36283246, -0.49774083],\n", 96 | " [ 2.61402225, -0.52976937],\n", 97 | " [-1.04827498, 0.75586423]])" 98 | ] 99 | }, 100 | "metadata": {}, 101 | "execution_count": 3 102 | } 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "source": [ 108 | "We can access some elements of the array, and if we want, new values also can be assigned to them.\n", 109 | "In this example, we shall first access element at (0,1) indices, and then we shall replace it by 5.\n", 110 | "Finally we will print the variable to check if the variable got modified." 111 | ], 112 | "metadata": { 113 | "id": "UOySjfZRLs2M" 114 | } 115 | }, 116 | { 117 | "cell_type": "code", 118 | "source": [ 119 | "print(foo[0,1])\n", 120 | "\n", 121 | "foo[0,1]=5\n", 122 | "print(foo)" 123 | ], 124 | "metadata": { 125 | "colab": { 126 | "base_uri": "https://localhost:8080/" 127 | }, 128 | "id": "xOuYks4LLy9F", 129 | "outputId": "87c63b88-251c-4400-a5ca-4caaf370635e" 130 | }, 131 | "execution_count": 4, 132 | "outputs": [ 133 | { 134 | "output_type": "stream", 135 | "name": "stdout", 136 | "text": [ 137 | "2.6140222464576492\n", 138 | "[[ 0.36283246 5. -1.04827498]\n", 139 | " [-0.49774083 -0.52976937 0.75586423]]\n" 140 | ] 141 | } 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "source": [ 147 | "The shape of any array is changed by using the reshape method. During reshape operation, the change in number of elements is not allowed. In the following example, first we shall create an\n", 148 | "array having size of (3×6), and the we shall change its shape to (2×9)." 149 | ], 150 | "metadata": { 151 | "id": "EOeJmhBqMZnz" 152 | } 153 | }, 154 | { 155 | "cell_type": "code", 156 | "source": [ 157 | "foo = np.random.randn(3,6)\n", 158 | "foo.reshape(2,9)" 159 | ], 160 | "metadata": { 161 | "colab": { 162 | "base_uri": "https://localhost:8080/" 163 | }, 164 | "id": "_VtmBWJqMehq", 165 | "outputId": "b939d7fc-7dfb-431b-a2ab-9c7407e262d5" 166 | }, 167 | "execution_count": 5, 168 | "outputs": [ 169 | { 170 | "output_type": "execute_result", 171 | "data": { 172 | "text/plain": [ 173 | "array([[-0.23974728, 2.15601789, -0.58866054, -0.94078212, 0.1031432 ,\n", 174 | " 1.13549013, -0.03180578, 0.70462518, -0.22228889],\n", 175 | " [ 0.88917479, -0.1367948 , -0.07020427, 1.02353006, -0.47758702,\n", 176 | " -0.59042834, 0.08574093, 0.181381 , 2.86724845]])" 177 | ] 178 | }, 179 | "metadata": {}, 180 | "execution_count": 5 181 | } 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "source": [ 187 | "Like we can access the any elements of the array and change it, in similar way we can access the any attributes, and modify them. However, the modification is only allowed if the attributes is writeable, and the new value makes some sense to the variable. We can use this behaviour, and change the\n", 188 | "shape of variable using the shape attributes." 189 | ], 190 | "metadata": { 191 | "id": "EiQjh6eiMk1h" 192 | } 193 | }, 194 | { 195 | "cell_type": "code", 196 | "source": [ 197 | "foo = np.random.randn(4,3)\n", 198 | "print(foo.shape)\n", 199 | "print(foo)\n", 200 | "\n", 201 | "foo.shape = 2,6\n", 202 | "print(foo.shape)\n", 203 | "print(foo)" 204 | ], 205 | "metadata": { 206 | "colab": { 207 | "base_uri": "https://localhost:8080/" 208 | }, 209 | "id": "MAY2H-rpMqiG", 210 | "outputId": "9e98a94a-6fce-423d-c503-4458540c8b72" 211 | }, 212 | "execution_count": 6, 213 | "outputs": [ 214 | { 215 | "output_type": "stream", 216 | "name": "stdout", 217 | "text": [ 218 | "(4, 3)\n", 219 | "[[ 0.4771758 -0.76936763 -2.34410875]\n", 220 | " [-0.4262899 -0.66992844 -2.27842671]\n", 221 | " [-0.92889648 1.19237224 -0.59784249]\n", 222 | " [-0.16383886 0.4506501 0.85307605]]\n", 223 | "(2, 6)\n", 224 | "[[ 0.4771758 -0.76936763 -2.34410875 -0.4262899 -0.66992844 -2.27842671]\n", 225 | " [-0.92889648 1.19237224 -0.59784249 -0.16383886 0.4506501 0.85307605]]\n" 226 | ] 227 | } 228 | ] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "source": [ 233 | "In the above example, first an array is defined with a size of (4×3) and then its shape is assigned a value of (2,6), which makes the array of size (2×6). As we can not change the number of elements, so if we define one dimension of the new variable, second dimension can be computed with ease.\n", 234 | "\n", 235 | "Numpy allow us to define -1 for the default dimension in this case. We can make the desired change in the shape of variable by using default dimension also." 236 | ], 237 | "metadata": { 238 | "id": "dvrHwdPbM5G5" 239 | } 240 | }, 241 | { 242 | "cell_type": "code", 243 | "source": [ 244 | "foo.shape = -1,6\n", 245 | "print(foo.shape)" 246 | ], 247 | "metadata": { 248 | "colab": { 249 | "base_uri": "https://localhost:8080/" 250 | }, 251 | "id": "vb374jNrM-cU", 252 | "outputId": "4887bb65-f0f1-4622-82ac-941f1f304235" 253 | }, 254 | "execution_count": 7, 255 | "outputs": [ 256 | { 257 | "output_type": "stream", 258 | "name": "stdout", 259 | "text": [ 260 | "(2, 6)\n" 261 | ] 262 | } 263 | ] 264 | }, 265 | { 266 | "cell_type": "markdown", 267 | "source": [ 268 | "We can flatten the array (make array one dimensional) by using the ravel method, which is explained in the following example:" 269 | ], 270 | "metadata": { 271 | "id": "JNN7o7w3NDCl" 272 | } 273 | }, 274 | { 275 | "cell_type": "code", 276 | "source": [ 277 | "foo = np.random.rand(2,3)\n", 278 | "print(foo)\n", 279 | "print(foo.shape)\n", 280 | "\n", 281 | "a = foo.ravel()\n", 282 | "print(a.shape)\n", 283 | "print(a)" 284 | ], 285 | "metadata": { 286 | "colab": { 287 | "base_uri": "https://localhost:8080/" 288 | }, 289 | "id": "BvkpnieENFzr", 290 | "outputId": "90b1f6b8-ecad-4aa0-f46c-25de38386ec9" 291 | }, 292 | "execution_count": 8, 293 | "outputs": [ 294 | { 295 | "output_type": "stream", 296 | "name": "stdout", 297 | "text": [ 298 | "[[0.04943299 0.76016869 0.59526285]\n", 299 | " [0.33552459 0.36886908 0.96542576]]\n", 300 | "(2, 3)\n", 301 | "(6,)\n", 302 | "[0.04943299 0.76016869 0.59526285 0.33552459 0.36886908 0.96542576]\n" 303 | ] 304 | } 305 | ] 306 | } 307 | ] 308 | } -------------------------------------------------------------------------------- /03_05.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyP3rJz2c+WKLCzGvTDl9/bs", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "### 3.5 DataFrame\n", 33 | "\n", 34 | "DataFrame is a kind of dictionary. In a dictionary, each key corresponds to a value, while in the DataFrame, each key corresponds to a series (column). *pandas* is a library to deal with DataFrame, we will be using the same. \n", 35 | "\n", 36 | "### 3.5.1 Creating a DataFrame\n", 37 | "Let us start taking the same sample data as we took in the *dictionary* section." 38 | ], 39 | "metadata": { 40 | "id": "gLbDBrFaFBQ2" 41 | } 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 2, 46 | "metadata": { 47 | "colab": { 48 | "base_uri": "https://localhost:8080/" 49 | }, 50 | "id": "ynxyJjEgD1sj", 51 | "outputId": "ce977485-a0d8-4b58-8b6f-4f454db7f095" 52 | }, 53 | "outputs": [ 54 | { 55 | "output_type": "stream", 56 | "name": "stdout", 57 | "text": [ 58 | " names rainfall\n", 59 | "0 Delhi 0\n", 60 | "1 Bangalore 5\n", 61 | "2 Kolkata 10\n" 62 | ] 63 | } 64 | ], 65 | "source": [ 66 | "import pandas as pd\n", 67 | "\n", 68 | "names = ['Delhi', 'Bangalore', 'Kolkata']\n", 69 | "rainfall = [0, 5, 10]\n", 70 | "\n", 71 | "data = {\n", 72 | " \"names\": names,\n", 73 | " \"rainfall\": rainfall\n", 74 | "}\n", 75 | "\n", 76 | "df = pd.DataFrame(data)\n", 77 | "print(df)" 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "source": [ 83 | "This example does not show any benefit of using DataFrame over the dictionary. However, as we go much deeper into the DataFrame, the benefits will be obvious, so keep some patience.\n", 84 | "\n", 85 | "Let get data over second and third row in our DataFrame:" 86 | ], 87 | "metadata": { 88 | "id": "hrdFba3EG6ox" 89 | } 90 | }, 91 | { 92 | "cell_type": "code", 93 | "source": [ 94 | "print(df.loc[[1,2]])" 95 | ], 96 | "metadata": { 97 | "colab": { 98 | "base_uri": "https://localhost:8080/" 99 | }, 100 | "id": "byK4N8h-HVtF", 101 | "outputId": "ea3ad5fc-8559-49ac-ab9b-10b3bb5730f9" 102 | }, 103 | "execution_count": 3, 104 | "outputs": [ 105 | { 106 | "output_type": "stream", 107 | "name": "stdout", 108 | "text": [ 109 | " names rainfall\n", 110 | "1 Bangalore 5\n", 111 | "2 Kolkata 10\n" 112 | ] 113 | } 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "source": [ 119 | "Note that when we would like to have more than one row, we are providing input as list ([1,2]).\n", 120 | "\n", 121 | "### 3.5.2 Indexing DataFrame\n", 122 | "\n", 123 | "We can provide the name of row (i.e. index) also such as:" 124 | ], 125 | "metadata": { 126 | "id": "oSUqpjtvHp2I" 127 | } 128 | }, 129 | { 130 | "cell_type": "code", 131 | "source": [ 132 | "df = pd.DataFrame(data, index = [\"first\", \"second\", \"third\"])\n", 133 | "\n", 134 | "print(df) " 135 | ], 136 | "metadata": { 137 | "colab": { 138 | "base_uri": "https://localhost:8080/" 139 | }, 140 | "id": "vYB_c7jyHpMw", 141 | "outputId": "75c8b142-d3dc-4a68-df0f-00c44528a059" 142 | }, 143 | "execution_count": 5, 144 | "outputs": [ 145 | { 146 | "output_type": "stream", 147 | "name": "stdout", 148 | "text": [ 149 | " names rainfall\n", 150 | "first Delhi 0\n", 151 | "second Bangalore 5\n", 152 | "third Kolkata 10\n" 153 | ] 154 | } 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "source": [ 160 | "Then, we can print value based on the index:" 161 | ], 162 | "metadata": { 163 | "id": "7LFShqLQIXYt" 164 | } 165 | }, 166 | { 167 | "cell_type": "code", 168 | "source": [ 169 | "print(df.loc[\"first\"])" 170 | ], 171 | "metadata": { 172 | "colab": { 173 | "base_uri": "https://localhost:8080/" 174 | }, 175 | "id": "gjds8858Ianu", 176 | "outputId": "37fb32b9-a8c6-40bb-e846-20bd933b0ace" 177 | }, 178 | "execution_count": 6, 179 | "outputs": [ 180 | { 181 | "output_type": "stream", 182 | "name": "stdout", 183 | "text": [ 184 | "names Delhi\n", 185 | "rainfall 0\n", 186 | "Name: first, dtype: object\n" 187 | ] 188 | } 189 | ] 190 | } 191 | ] 192 | } -------------------------------------------------------------------------------- /04_01.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyPIyEWdvAQMQdRXKuj4dWbu", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "# 4. Basic applications of Python\n", 33 | "\n", 34 | "## 4.1 Introdution\n", 35 | "This chapter will provide some of the applications of python in solving problems. I have taken most of the problems from the book titled “Applied Hydrology” by Chow et al., and for detailed\n", 36 | "description of them, you should refer to the book. Some of the problems are taken from other relevant issues in agriculture. \n", 37 | "\n", 38 | "In this chapter, we will be solving simple problem, usually involving some mathematical equations and then some plotting. In later chapters, we will go into much more complex (i.e. involving many steps) problems.\n" 39 | ], 40 | "metadata": { 41 | "id": "oZt9BZAjJ6So" 42 | } 43 | } 44 | ] 45 | } -------------------------------------------------------------------------------- /04_03.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyMkT6HWfPEl99soj9vEyyt5", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 4.3 Terminal velocity of precipitation\n", 33 | "\n", 34 | "The terminal velocity ($V_t$) of a falling raindrop is given by:\n", 35 | "\n", 36 | "$V_t =\u0014 \\left[ \\frac{4gD}{3C_d}\u0012 \\left(\\frac{\\rho_w}{\\rho_a}−1 \\right) \\right]^{1/2} $,\n", 37 | "\n", 38 | "where, $g$ is the acceleration due to gravity, $D$ is the diameter of the falling raindrop, $\\rho_w$ is the density\n", 39 | "of water, $\\rho_a$ is the density of air, and $C_d$ is the drag coefficient. The Stoke’s law can be used to calculate drag coefficient ($C_d = 24/Re$), which is valid for raindrop having diameter less than 0.1 mm. $Re$ is the Reynold number, which can be calculated as $\\rho_a V D/\\mu_a$. Let us assume, that the $Re$ is given as 5.0, and the raindrop has diameter of 0.05 mm, and we want to estimate the $V_t$. ($\\rho_w = 998$, $\\rho_a = 1.2$)." 40 | ], 41 | "metadata": { 42 | "id": "glHxUMd0tP4c" 43 | } 44 | }, 45 | { 46 | "cell_type": "code", 47 | "source": [ 48 | "import numpy as np\n", 49 | "\n", 50 | "Re = 5.0; rho_w = 998; rho_a = 1.2; g = 9.8; D = 0.05E-3\n", 51 | "Cd = 24/Re\n", 52 | "Vt = np.sqrt((4*g*D)/(3*Cd)*(rho_w/rho_a-1))\n", 53 | "Vt" 54 | ], 55 | "metadata": { 56 | "colab": { 57 | "base_uri": "https://localhost:8080/" 58 | }, 59 | "id": "HBkiL0KptCXp", 60 | "outputId": "0535cac9-bc8d-4a0e-c640-7e4a26c5b1c6" 61 | }, 62 | "execution_count": 2, 63 | "outputs": [ 64 | { 65 | "output_type": "execute_result", 66 | "data": { 67 | "text/plain": [ 68 | "0.3362483649967134" 69 | ] 70 | }, 71 | "metadata": {}, 72 | "execution_count": 2 73 | } 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "source": [ 79 | "In this example we see that \";\" allows us to define many expressions in one line." 80 | ], 81 | "metadata": { 82 | "id": "M_CcexdbvQwr" 83 | } 84 | } 85 | ] 86 | } -------------------------------------------------------------------------------- /04_07.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyOBWtgceyrM+KQseqkVs8qz", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 4.7 Surface water\n", 33 | "The flow depth in a rectangular channel is given by,\n", 34 | "\n", 35 | "$ Q = \\frac{1.49}{n} S_0^{1/2} \\frac{(By)^{5/3}}{(B+y)^{2/3}} $,\n", 36 | "\n", 37 | "where, $Q$ is the flow, $n$ is the Manning’s coefficient, $S_0$ is slope of water surface, $B$ is the width\n", 38 | "of channel, and $y$ is the flow depth. This is a nonlinear equation in $y$, and the explicit solution of\n", 39 | "this is not yet found. This can be solved iteratively like in the last section, or using methods like\n", 40 | "Newton-Raphson. In this, we will solve using the *fmin* function of the *Scipy.optimize* library.\n", 41 | "First we will import required libraries. Then we will define a function that takes the flow depth ($y$)\n", 42 | "as input and gives the error in the flow estimated based on this $y$ and the given $Q$. We are taking\n", 43 | "absolute value of error, other possible options are like square of error etc. After specifying the function, we can give this function as a input to *fmin* and some initial guess of the $y$." 44 | ], 45 | "metadata": { 46 | "id": "-Kvy8BUDWMfq" 47 | } 48 | }, 49 | { 50 | "cell_type": "code", 51 | "source": [ 52 | "# import required modules\n", 53 | "import numpy as np\n", 54 | "import matplotlib.pyplot as plt\n", 55 | "from scipy.optimize import fmin\n", 56 | "\n", 57 | "# define the variables\n", 58 | "n = 0.015\n", 59 | "S0 = 0.025\n", 60 | "Q = 9.26\n", 61 | "B = 2\n", 62 | "\n", 63 | "# define the flow function\n", 64 | "def flow(y):\n", 65 | " Q_estiamted = (1.49/n)*(S0**0.5)*((B*y)**(5/3))/((B+y)**(2/3))\n", 66 | " epsilon = np.abs(Q_estiamted - Q)\n", 67 | " return(epsilon)\n", 68 | "\n", 69 | "y_optimum = fmin(flow,0.5)\n", 70 | "print(y_optimum)" 71 | ], 72 | "metadata": { 73 | "colab": { 74 | "base_uri": "https://localhost:8080/" 75 | }, 76 | "id": "TKkA6d11XXxo", 77 | "outputId": "3f0f3ccb-3c75-475c-fc20-caf25b4f624e" 78 | }, 79 | "execution_count": 2, 80 | "outputs": [ 81 | { 82 | "output_type": "stream", 83 | "name": "stdout", 84 | "text": [ 85 | "Optimization terminated successfully.\n", 86 | " Current function value: 0.000078\n", 87 | " Iterations: 13\n", 88 | " Function evaluations: 26\n", 89 | "[0.52770386]\n" 90 | ] 91 | } 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "source": [ 97 | "fmin will give us the required y value. We can also get details of the iterations, and error value at\n", 98 | "final iterations. We use print function to see the details. The output is provided above.\n", 99 | "\n", 100 | "The optimization terminated successfully, i.e. the required accuracy was achieved within the default\n", 101 | "maximum number of iterations allowed. The output tells that it took 13 iterations to achieve the\n", 102 | "required accuracy, and that the function was evaluated 26 times in the process." 103 | ], 104 | "metadata": { 105 | "id": "4vBXdFsTX5-a" 106 | } 107 | } 108 | ] 109 | } -------------------------------------------------------------------------------- /04_09.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyODPVspuw1kTHTm3MNpgItT", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 4.9 Delta T\n", 33 | "\n", 34 | "In spraying, pesticides are mixed with more than 90% of the water and hence evaporation plays very important role. Delta T is an indication of the water evaporation. A lower value of Delta T means lower evaporation. \n", 35 | "\n", 36 | "Delta T is defined as the dry bulb temperature minus the wet bulb temperature. We need measurements of RH (relative humidity) and air (dry bulb) temperature to compute Delta T." 37 | ], 38 | "metadata": { 39 | "id": "JHrxcglfcjRn" 40 | } 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 3, 45 | "metadata": { 46 | "colab": { 47 | "base_uri": "https://localhost:8080/" 48 | }, 49 | "id": "wYVeGHIltXfh", 50 | "outputId": "e05912d2-bd79-4303-847b-ac82aac19390" 51 | }, 52 | "outputs": [ 53 | { 54 | "output_type": "stream", 55 | "name": "stdout", 56 | "text": [ 57 | "9.140479070827855\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "import numpy as np\n", 63 | "RH = 27 # relative humidity in %\n", 64 | "T = 18 # air temperature in degree Celcius\n", 65 | "Tw = T*np.arctan(0.151977*(RH+8.313659)**0.5) + np.arctan(T+RH) - np.arctan(RH - 1.676331) + 0.00391838*(RH)**(3/2)*np.arctan(0.023101*RH) -4.686035\n", 66 | "delta_t = T-Tw\n", 67 | "print(delta_t)" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "source": [ 73 | "Let us take all possible values of $RH$ and $T$ and plot the region for which spraying is preferred. Below example, takes the values of $T$ from 0 to 50, take the values of RH from 1 to 100, and compute Delta T. For a value of Delta T between 2 and 8, spraying is preferred and between 8 and 10, conditions are marginal, for any other value, spraying should be avoided. " 74 | ], 75 | "metadata": { 76 | "id": "4D_IWClxhUlT" 77 | } 78 | }, 79 | { 80 | "cell_type": "code", 81 | "source": [ 82 | "import matplotlib.pyplot as plt\n", 83 | "\n", 84 | "T,RH = np.meshgrid(np.linspace(0,50), np.linspace(1,100))\n", 85 | "\n", 86 | "def compute_delta_t(RH,T):\n", 87 | " Tw = T*np.arctan(0.151977*(RH+8.313659)**0.5) + np.arctan(T+RH) - np.arctan(RH - 1.676331) + 0.00391838*(RH)**(3/2)*np.arctan(0.023101*RH) -4.686035\n", 88 | " delta_t = T-Tw\n", 89 | " return(delta_t)\n", 90 | "\n", 91 | "delta_t = compute_delta_t(RH,T)\n", 92 | "\n", 93 | "plt.contourf(T,RH,delta_t, levels=[2,8,10])\n", 94 | "plt.colorbar()\n", 95 | "plt.xlabel('T', fontsize=20)\n", 96 | "plt.ylabel('RH', fontsize=20)\n", 97 | "plt.show()" 98 | ], 99 | "metadata": { 100 | "colab": { 101 | "base_uri": "https://localhost:8080/", 102 | "height": 292 103 | }, 104 | "id": "Uz-T-c2keT0e", 105 | "outputId": "09fa7c47-819a-4acf-99af-e90630c6cb4a" 106 | }, 107 | "execution_count": 14, 108 | "outputs": [ 109 | { 110 | "output_type": "display_data", 111 | "data": { 112 | "text/plain": [ 113 | "
" 114 | ], 115 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAETCAYAAAA4W80CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9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CWpcIZekgpn6CWpcIZekgr3/znzu1E9/XNNAAAAAElFTkSuQmCC\n" 116 | }, 117 | "metadata": { 118 | "needs_background": "light" 119 | } 120 | } 121 | ] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "source": [ 126 | "In the above figure, blue color indicates optimal condition and light green colour indicates marginal conditions for spraying. " 127 | ], 128 | "metadata": { 129 | "id": "p0SX2bYDh-tc" 130 | } 131 | } 132 | ] 133 | } -------------------------------------------------------------------------------- /05_03.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyOIX790A1ZKD+MUpTpvcDkF", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 5.3 The t-test\n", 33 | "In statistics, we make hypothesis like two different random variable have the same mean, or have\n", 34 | "equal variance, or they follow same distribution. To test these hypothesis, test statistic is derived,\n", 35 | "and based on the test statistic, the hypothesis is rejected or accepted. When the test statistic follow a Student’s t distribution, the t-test is used to test hypothesis. This test is available in the\n", 36 | "scipy.stats library. Let us first test if the mean of random variable is same as we expected or not.\n", 37 | "st.ttest_1samp function is used for this purpose. We will generate normally distributed random\n", 38 | "variable having mean equal to 5, and standard deviation equal to 10. And we will test if the mean\n", 39 | "of this generated random variable is 5 or not. Because we are talking 1000 number of sample, we\n", 40 | "expect that the mean will be approximately equal to 5 most of the time (but not always). The hypothetis is rejected or accepted based on the p-value. The p-value close to one means that hypothesis is\n", 41 | "true; a value closer to zero means that the hypothesis is rejected. The significance level ($\\alpha$) is used\n", 42 | "to define the threshold, which is often taken as 0.05 or 0.01. If the p-value is greater than this than\n", 43 | "we can accept the hypothesis." 44 | ], 45 | "metadata": { 46 | "id": "M_JiBOzRhPh9" 47 | } 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 5, 52 | "metadata": { 53 | "colab": { 54 | "base_uri": "https://localhost:8080/" 55 | }, 56 | "id": "bNTUdAl1hK0A", 57 | "outputId": "83bab908-6488-427e-d43e-c333c064b283" 58 | }, 59 | "outputs": [ 60 | { 61 | "output_type": "stream", 62 | "name": "stdout", 63 | "text": [ 64 | "0.2774121493487677\n" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "import scipy.stats as st\n", 70 | "\n", 71 | "rvs1 = st.norm.rvs(loc=5,scale=10,size=1000)\n", 72 | "# t-test\n", 73 | "t, p = st.ttest_1samp(rvs1,5)\n", 74 | "print(p)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "source": [ 80 | "We see in this example that p-value is not below our threshold, which means the mean of generated random variable\n", 81 | "is close to 5. \n", 82 | "\n", 83 | "\n", 84 | "The t-test is also used to test if the mean of two independent random number is equal or not. Let us generate two normally distributed random variable with same mean, say 5. We would like to see if the mean of these two independent random variable is same or not. We can use\n", 85 | "st.ttest_ind for this purpose. In this example the p-value is 0.96, which means means are same." 86 | ], 87 | "metadata": { 88 | "id": "9rUlRJttj9MQ" 89 | } 90 | }, 91 | { 92 | "cell_type": "code", 93 | "source": [ 94 | "rvs1 = st.norm.rvs(loc=5,scale=10,size=1000)\n", 95 | "rvs2 = st.norm.rvs(loc=5,scale=10,size=1000)\n", 96 | "# t-test\n", 97 | "t, p = st.ttest_ind(rvs1,rvs2)\n", 98 | "print(p)" 99 | ], 100 | "metadata": { 101 | "colab": { 102 | "base_uri": "https://localhost:8080/" 103 | }, 104 | "id": "tyGMu8bXkgQP", 105 | "outputId": "fa64ce66-ca1b-4112-f92d-a78a8e53f984" 106 | }, 107 | "execution_count": 6, 108 | "outputs": [ 109 | { 110 | "output_type": "stream", 111 | "name": "stdout", 112 | "text": [ 113 | "0.8209315558743397\n" 114 | ] 115 | } 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "source": [ 121 | "In the previous example, we tested two independent sample for the mean. We can also test if the\n", 122 | "mean is same or not, when the samples are related or come from same experiment. We can use\n", 123 | "st.ttest_rel for this purpose. \n", 124 | "\n", 125 | "We get p-value higher than the threshold, which means that the means are same." 126 | ], 127 | "metadata": { 128 | "id": "u2L6XZbHkpxW" 129 | } 130 | }, 131 | { 132 | "cell_type": "code", 133 | "source": [ 134 | "rvs1 = st.norm.rvs(loc=5,scale=10,size=1000)\n", 135 | "rvs2 = st.norm.rvs(loc=5,scale=10,size=1000)\n", 136 | "\n", 137 | "# t-test\n", 138 | "t, p = st.ttest_rel(rvs1,rvs2)\n", 139 | "print(p)" 140 | ], 141 | "metadata": { 142 | "colab": { 143 | "base_uri": "https://localhost:8080/" 144 | }, 145 | "id": "odPVlVlRksXO", 146 | "outputId": "e3f9ac25-ca99-44f3-e60e-c10989a1946e" 147 | }, 148 | "execution_count": 7, 149 | "outputs": [ 150 | { 151 | "output_type": "stream", 152 | "name": "stdout", 153 | "text": [ 154 | "0.6070549384440724\n" 155 | ] 156 | } 157 | ] 158 | } 159 | ] 160 | } -------------------------------------------------------------------------------- /05_04.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyOxMQJdCnRrRingMVOHtqkR", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 5.4 KS test\n", 33 | "KolmogorovSmirnov (KS) test is a non-parametric test to compare the equality of two continuous\n", 34 | "one dimensional probability distributions. In this test, we quantify the distance (absolute difference)\n", 35 | "between distributions. These two distributions could be two different sample, or one could be sample\n", 36 | "and another one a theoretical distribution. Let us test if our generated normal random variable follow\n", 37 | "normal distribution or not. st.kstest is the function to to perform KS test." 38 | ], 39 | "metadata": { 40 | "id": "HXdcA3uvlOju" 41 | } 42 | }, 43 | { 44 | "cell_type": "code", 45 | "source": [ 46 | "import numpy as np\n", 47 | "import scipy.stats as st\n", 48 | "x = np.random.randn(1000)\n", 49 | "# KS test\n", 50 | "D, p = st.kstest(x,'norm')\n", 51 | "print(p)" 52 | ], 53 | "metadata": { 54 | "colab": { 55 | "base_uri": "https://localhost:8080/" 56 | }, 57 | "id": "EMLarDwalUHs", 58 | "outputId": "954aac19-4b2c-48e9-b8cf-82f9bb553c35" 59 | }, 60 | "execution_count": 3, 61 | "outputs": [ 62 | { 63 | "output_type": "stream", 64 | "name": "stdout", 65 | "text": [ 66 | "0.4150586023618166\n" 67 | ] 68 | } 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "source": [ 74 | "We get a p-value higher than the threshold, which means that our generated normally distributed random variable\n", 75 | "is in fact normal. We can also test if the the generated uniformly distributed random variable are\n", 76 | "not normal by chance. In this we get a p-value less than the threshold, which means that our generated random\n", 77 | "numbers in this case are not normal." 78 | ], 79 | "metadata": { 80 | "id": "s0MYN1qTlUZI" 81 | } 82 | }, 83 | { 84 | "cell_type": "code", 85 | "source": [ 86 | "y = np.random.rand(1000)\n", 87 | "D, p = st.kstest(y,'norm')\n", 88 | "print(p)" 89 | ], 90 | "metadata": { 91 | "colab": { 92 | "base_uri": "https://localhost:8080/" 93 | }, 94 | "id": "tjPxilOQlUkZ", 95 | "outputId": "0a99f81b-4016-494a-e677-90921eedd9b5" 96 | }, 97 | "execution_count": 5, 98 | "outputs": [ 99 | { 100 | "output_type": "stream", 101 | "name": "stdout", 102 | "text": [ 103 | "3.684250271719294e-232\n" 104 | ] 105 | } 106 | ] 107 | } 108 | ] 109 | } -------------------------------------------------------------------------------- /05_05.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyOBys4AcYJ3P8bEkXQ5Mdey", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 5.5 The chi square test\n", 33 | "We can also compare distribution by comparing their PDFs. In this case we use the Chi square ($\\chi^2$) test. In this test, ($\\chi^2$) statistics is computed first, and based on this we say if distributions are same or not. We can compare sample with the theoretical distribution, or two samples. We will\n", 34 | "take two pdfs, in which one is assumed to be observed and another one is expected. We will use the chisquare function from scipy.stats.mstats library. This function gives $\\chi^2$\n", 35 | "statistics and p-value of the test. In the following example, we get a p-value close to zero, it means that these two\n", 36 | "frequency comes from different distributions." 37 | ], 38 | "metadata": { 39 | "id": "m3AWRhVGoobe" 40 | } 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 2, 45 | "metadata": { 46 | "colab": { 47 | "base_uri": "https://localhost:8080/" 48 | }, 49 | "id": "3Iplu59goYIm", 50 | "outputId": "425df912-12b8-4811-9753-c16c0bfa1da3" 51 | }, 52 | "outputs": [ 53 | { 54 | "output_type": "stream", 55 | "name": "stdout", 56 | "text": [ 57 | "10.666666666666666 0.01367189381253985\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "from scipy.stats.mstats import chisquare\n", 63 | "import numpy as np\n", 64 | "f_obs = np.array([10, 15, 20, 30]) # observed pdf (freq)\n", 65 | "f_exp = np.array([5, 25, 15, 30]) # expected pdf (freq)\n", 66 | "# chi square test\n", 67 | "c, p = chisquare(f_obs, f_exp)\n", 68 | "\n", 69 | "print(c,p)" 70 | ] 71 | } 72 | ] 73 | } -------------------------------------------------------------------------------- /06_01.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyOHhUrmyi9m9oKucBgIwVj/", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "# 6 Spatial Data\n", 33 | "\n", 34 | "## 6.1 Types of spatial data\n", 35 | "Raster and vector are the two basic data structures for storing and manipulating images and graphics\n", 36 | "data in GIS (Geographic Information Systems). Raster image comes in the form of individual\n", 37 | "pixels, and each spatial location or resolution element has a pixel associated where the pixel value\n", 38 | "indicates the attribute, such as color, elevation, or an ID number. Vector data comes in the form of\n", 39 | "points and lines, that are geometrically and mathematically associated. Points are stored using the\n", 40 | "coordinates, for example, a two-dimensional point is stored as ($x$, $y$). Lines are stored as a series of\n", 41 | "point pairs, where each pair represents a straight line segment, for example, ($x_1$, $y_1$) and ($x_2$, $y_2$)\n", 42 | "indicating a line from ($x_1$, $y_1$) to ($x_2$, $y_2$).\n", 43 | "We will create some raster data using some mathematical function, and then also add noise into\n", 44 | "it. We will keep both the data (with noise, and without noise) for future use. *np.mgrid* is used to create gridded points. The data is plotted using *plt.matshow* function, which is a simple function to visualize a two dimensional array. Below figures show the data without and with noise. The data without noise shows a systematic behaviour, while it is blurred\n", 45 | "in the data added with noise." 46 | ], 47 | "metadata": { 48 | "id": "TpmvwiEgMwjF" 49 | } 50 | }, 51 | { 52 | "cell_type": "code", 53 | "source": [ 54 | "import numpy as np\n", 55 | "# generate some synthetic data\n", 56 | "X, Y = np.mgrid[0:101, 0:101]\n", 57 | "data = np.sin((X**2 + Y**2)/25)\n", 58 | "data_noisy = data + np.random.random(X.shape)\n", 59 | "\n", 60 | "# plot the data\n", 61 | "plt.matshow(data)\n", 62 | "plt.colorbar()\n", 63 | "plt.title(\"Synthetic data without noise\")\n", 64 | "plt.show()\n", 65 | "\n", 66 | "plt.matshow(data_noisy)\n", 67 | "plt.colorbar(shrink=0.5)\n", 68 | "plt.title(\"Synthetic data perturbed with noise\")\n", 69 | "plt.show()" 70 | ], 71 | "metadata": { 72 | "id": "ksul51FJNUuf" 73 | }, 74 | "execution_count": null, 75 | "outputs": [] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "source": [ 80 | "We can also generate a vector data with using some points. Below figure shows the vector data." 81 | ], 82 | "metadata": { 83 | "id": "_xqxlCDwNrad" 84 | } 85 | }, 86 | { 87 | "cell_type": "code", 88 | "source": [ 89 | "# vector data\n", 90 | "vector_x = [10,7,24,16,15,10]\n", 91 | "vector_y = [10,23,20,14,7,10]\n", 92 | "\n", 93 | "#plot vector data\n", 94 | "plt.plot(vector_x, vector_y)\n", 95 | "plt.axis((5,25,5,25))\n", 96 | "plt.show()" 97 | ], 98 | "metadata": { 99 | "id": "7FqEscjxOIYb" 100 | }, 101 | "execution_count": null, 102 | "outputs": [] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "source": [ 107 | "The geospatial data can be classified into two major parts. In the first part we have information about some feature, like a two dimensional array showing the spatial variation in elevation etc. In the second part, we have information about the co-ordinates of the data. A typical processing chain\n", 108 | "for geo-spatial data is given in below flow chart. We have the geospatial data, and we extract the feature information and co-ordinate information separately, then we process them separately, and finally after processing we again combine them. The processing for feature information could be some\n", 109 | "mathematical operation, and for co-ordinate information, it could some co-ordinate transformation etc.\n", 110 | "\n", 111 | "![Flow chart](https://drive.google.com/uc?export=view&id=1OjEnt4XYIWcemtn7dvtu2TNVIQc_68ND)" 112 | ], 113 | "metadata": { 114 | "id": "cwNB8anQOQ6W" 115 | } 116 | } 117 | ] 118 | } -------------------------------------------------------------------------------- /06_02.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyNHtLpCaQ1Bp8/lAicBc/dn", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 6.2 Geoinformation\n", 33 | "The raster data can be geo-referenced either by specifying the GeoTransform variable, or by specifying the GCPs for the image. The GeoTransform (GT) is related to the geographical co-ordinates in the following way,\n", 34 | "\n", 35 | "$ X_{geo} = GT[0] + X_{image} ∗GT[1] + Y_{image} ∗GT[2]$, \n", 36 | "$ Y_{geo} = GT[3] + X_{image} ∗GT[4] + Y_{image} ∗GT[5]$, \n", 37 | "\n", 38 | "where, subscript $geo$ refers to the global co-ordinates, and $image$ refers to the image co-ordinates, i.e. pixel and line number of image. The number in the square bracket ([]) represents the indices of the GT variables. $X$ and $Y$ are co-ordinates. GT[2] and GT[4] represents the orientation of the image, with respect to the $X$ and $Y$ of geo, and becomes zero if the image is north up. GT[1] represents\n", 39 | "pixel width, and GT[5] represents the pixel height. GT[0] and GT[3] is the position of the top left\n", 40 | "corner of the top left pixel of the raster. It should be noted that the pixel/line coordinates in the above\n", 41 | "are from (0.0,0.0) at the top left corner of the top left pixel to at the bottom right corner of the bottom right pixel. The pixel/line location of the center of the top left pixel would therefore be (0.5,0.5).\n", 42 | "The information related to the geo-referencing can be specified by specifying the control points.\n", 43 | "Control points should contains minimally the $GCP_{Pixel}$ (pixel number of image), $GCP_{Line}$ (line number of image), $GCP_X$ (X co-ordinate), $GCP_Y$ (Y co-ordinate), and $GCP_Z$ (Z co-ordinate). The (Pixel, Line) position is the GCP location on the raster. The ($X$, $Y$, $Z$) position is the associated georeferenced location with the $Z$ often being zero. Usually polynomials are used to transform the image co-ordinate (Pixel,Line) into geo-referenced co-ordinates.\n", 44 | "\n", 45 | "Normally a dataset will contain either an affine geotransform or GCPs. In addition to the information about how co-ordinates are related to the geo-referenced co-ordinates, the name of co-ordinate system is also assigned to the image." 46 | ], 47 | "metadata": { 48 | "id": "B29RpI3OTNg5" 49 | } 50 | } 51 | ] 52 | } -------------------------------------------------------------------------------- /06_04.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyPVp11yldKCPewDqXZkKiVT", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 6.4 Writing Vector\n", 33 | "Shapefile (.shp) format is quiet commonly used type of vector data. Let us write one shapefile. To write shapefile, we will be using the package ogr. OGR is a part of the GDAL library. OGR deals with the vector formats, while GDAL main library is for raster formats. A list of format supported by OGR along with their code name to be used while creating driver, is given at http://www.gdal.org/ogr/ogr_formats.html. Let us say, we want to make a shapefile having location of the four cities and their name. The details of cities are as:\n", 34 | "\n", 35 | "\n", 36 | "Name Latitude Longitude\n", 37 | "Bijnor 29.4 78.1\n", 38 | "Delhi 28.6 77.2\n", 39 | "Bangalore 13.0 77.8\n", 40 | "Berambadi 11.8 76.6\n", 41 | "\n", 42 | "We begin with importing ogr library and defining the location and names of the cities." 43 | ], 44 | "metadata": { 45 | "id": "J5Rdo-5CeshK" 46 | } 47 | }, 48 | { 49 | "cell_type": "code", 50 | "source": [ 51 | "import ogr\n", 52 | "lat = [29.4,28.6,13.0,11.8]\n", 53 | "lon = [78.1,77.2,77.8,76.6]\n", 54 | "name = ['Bijnor', 'Delhi', 'Bangalore', 'Berambadi']" 55 | ], 56 | "metadata": { 57 | "id": "Lv_e4CA2hR_X" 58 | }, 59 | "execution_count": 2, 60 | "outputs": [] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "source": [ 65 | "Now, we define the name of driver (ESRI Shapefile), and create the data source.\n", 66 | "driver.CrateDataSource defines the name of the folder where data will be saved.\n", 67 | "ds.CreateLayer defines the name of the shapefile along with the geometry type (point in this\n", 68 | "case). Then we define, field name as ’Name’ and say that it is a string type having a maximum width of 16." 69 | ], 70 | "metadata": { 71 | "id": "siAdriyBfhor" 72 | } 73 | }, 74 | { 75 | "cell_type": "code", 76 | "source": [ 77 | "from google.colab import drive\n", 78 | "import ogr\n", 79 | "\n", 80 | "#drive.mount('/content/gdrive')\n", 81 | "\n", 82 | "driver = ogr.GetDriverByName(\"ESRI Shapefile\")\n", 83 | "data_dir = \"/content/gdrive/My Drive/Colab Notebooks/LearnPython/data/\"\n", 84 | "ds = driver.CreateDataSource(data_dir)\n", 85 | "layer = ds.CreateLayer('location', geom_type=ogr.wkbPoint)\n", 86 | "field_defn = ogr.FieldDefn('Name', ogr.OFTString )\n", 87 | "field_defn.SetWidth(16)\n", 88 | "print(layer)\n", 89 | "layer.CreateField(field_defn)" 90 | ], 91 | "metadata": { 92 | "colab": { 93 | "base_uri": "https://localhost:8080/", 94 | "height": 222 95 | }, 96 | "id": "T2ffsV03iAQG", 97 | "outputId": "ef5468b1-109d-448d-fb3b-8af41e0032c2" 98 | }, 99 | "execution_count": 5, 100 | "outputs": [ 101 | { 102 | "output_type": "stream", 103 | "name": "stdout", 104 | "text": [ 105 | "None\n" 106 | ] 107 | }, 108 | { 109 | "output_type": "error", 110 | "ename": "AttributeError", 111 | "evalue": "ignored", 112 | "traceback": [ 113 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 114 | "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", 115 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mfield_defn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSetWidth\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m16\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlayer\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m \u001b[0mlayer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCreateField\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfield_defn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 116 | "\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'CreateField'" 117 | ] 118 | } 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "source": [ 124 | "Now, we have the basic information ready, and we can start adding the information about cities\n", 125 | "(name and location). First we create a feature to store the information about city. Then, we add the\n", 126 | "name of the city in the ’Name’ field. After this, we say that it is point type, and we add its longitude\n", 127 | "and latitude. At last, we destroy the feature and data source, so that nothing else can be done with\n", 128 | "them, and our data is saved properly." 129 | ], 130 | "metadata": { 131 | "id": "Bgtg7haiiRmc" 132 | } 133 | }, 134 | { 135 | "cell_type": "code", 136 | "source": [ 137 | "i = 0\n", 138 | "for i in range(len(name)):\n", 139 | " feature = ogr.Feature(layer.GetLayerDefn())\n", 140 | " feature.SetField('Name', name[i])\n", 141 | " pt = ogr.Geometry(ogr.wkbPoint)\n", 142 | " pt.SetPoint_2D(0, lon[i], lat[i])\n", 143 | " feature.SetGeometry(pt)\n", 144 | " layer.CreateFeature(feature)\n", 145 | " feature.Destroy()\n", 146 | " ds.Destroy()" 147 | ], 148 | "metadata": { 149 | "colab": { 150 | "base_uri": "https://localhost:8080/", 151 | "height": 240 152 | }, 153 | "id": "v5oa0hTKiUJ-", 154 | "outputId": "1ba2bfab-60c7-41fb-ca1a-8c0b8a76a52c" 155 | }, 156 | "execution_count": 3, 157 | "outputs": [ 158 | { 159 | "output_type": "error", 160 | "ename": "NameError", 161 | "evalue": "ignored", 162 | "traceback": [ 163 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 164 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 165 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\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[0;32m----> 3\u001b[0;31m \u001b[0mfeature\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mogr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mFeature\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlayer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mGetLayerDefn\u001b[0m\u001b[0;34m(\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\u001b[1;32m 4\u001b[0m \u001b[0mfeature\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSetField\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Name'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\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[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfeature\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 166 | "\u001b[0;31mNameError\u001b[0m: name 'layer' is not defined" 167 | ] 168 | } 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "source": [ 174 | "Our vector file (shapefile) is saved and can be visualized in any GIS tool, or we can use in Python also which we wil learn later." 175 | ], 176 | "metadata": { 177 | "id": "3NLKlVkiL8-k" 178 | } 179 | } 180 | ] 181 | } -------------------------------------------------------------------------------- /06_06.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyOgt3CS3aVQICQmpIsQFMTR", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 6.6 Read the vector\n", 33 | "In this section, we will read the vector data, that we wrote earlier. First, we need to import *ogr* library. Then we open data source by specifying the directory of the shapefile. Then, we use *GetLayerByName* to read the shapefile by specifying the name of shpaefile without *.shp* extension.\n", 34 | "\n", 35 | "After this, we are printing *\\n* which means print a blank line. *\\n* represents a new line, and *\\t* represents the tab. Now, we are printing header information (SI., Name, Latitude, Longitude). We are using .format to format the output. The number inside '{}' after the colon (:) represents the\n", 36 | "length of the output. We read the feature in the shapefile one by one using the *for* loop. From each feature, we extract the name using GetFieldAsString, Longitude using GetX and Latitude\n", 37 | "using GetY. At the end to avoid corrupting the database, we close it safely by specifying data source as None." 38 | ], 39 | "metadata": { 40 | "id": "BhkkhMPOpU8g" 41 | } 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 1, 46 | "metadata": { 47 | "colab": { 48 | "base_uri": "https://localhost:8080/" 49 | }, 50 | "id": "pzwWEGeXpUf4", 51 | "outputId": "7f1a97ba-a9e8-45d2-d15a-66b06fd9a698" 52 | }, 53 | "outputs": [ 54 | { 55 | "output_type": "stream", 56 | "name": "stdout", 57 | "text": [ 58 | "Mounted at /content/gdrive\n", 59 | "\n", 60 | "\n", 61 | "SI \t Name \t Longitude \t Latitude\n", 62 | "0 \t Bijnor \t 78.100 \t \t 29.400\n" 63 | ] 64 | } 65 | ], 66 | "source": [ 67 | "from google.colab import drive\n", 68 | "import ogr\n", 69 | "\n", 70 | "drive.mount('/content/gdrive')\n", 71 | "\n", 72 | "data_dir = \"/content/gdrive/My Drive/Colab Notebooks/LearnPython/data/\"\n", 73 | "ds = ogr.Open(data_dir)\n", 74 | "lyr = ds.GetLayerByName('location')\n", 75 | "\n", 76 | "print(\"\\n\")\n", 77 | "print(\"{} \\t {:10s} \\t {} \\t {}\".format('SI', 'Name', 'Longitude', 'Latitude'))\n", 78 | "for feat in lyr:\n", 79 | " geom = feat.GetGeometryRef()\n", 80 | " name = feat.GetFieldAsString(0)\n", 81 | " lat = geom.GetX()\n", 82 | " lon = geom.GetY()\n", 83 | " print('{0} \\t {1:10s} \\t {2:.3f} \\t \\t {3:.3f}'.format(0, name, lat, lon ))\n", 84 | "\n", 85 | "ds = None" 86 | ] 87 | } 88 | ] 89 | } -------------------------------------------------------------------------------- /07_06.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyMbuCG98rBAU62SBBcmcBKa", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 7.6 Box-plot\n", 33 | "Box-plot is a way to graphically visualize the statistical properties of the data. It provides information about the minimum, first quartile (Q1), median (Q2), upper quartile (Q3), maximum, and outliers if present. *boxplot* is used to make boxplot. Below figure shows the box plot." 34 | ], 35 | "metadata": { 36 | "id": "Vr508TnflCVS" 37 | } 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 5, 42 | "metadata": { 43 | "colab": { 44 | "base_uri": "https://localhost:8080/", 45 | "height": 265 46 | }, 47 | "id": "HNCw1Ix3lBkf", 48 | "outputId": "aa27d3f9-aeb0-4e8b-fb8c-1970245243c2" 49 | }, 50 | "outputs": [ 51 | { 52 | "output_type": "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5/BdACAY2VS4Dsl7XT351s/rtLhQgcAZEGH18Dd/T/N7C0zG+bu/0/SlZK2RRcNCN+hQ4d099136+DBg5GN+aMf/SiScSZOnKhLLrkkkrEQj0yvQimVVNF6BcpfJX0r80hA97Fz507ddddduvHGGyMb8xOfyPz2jfXr1+vgwYPBFXg6ndZ9990X6ZizZ8/OeIycnBwtWrRI5557bgSJ2i+jAnf3TZKKI8rSYb/61a/04x//ONIxBwwYkPEYPXv2VDqdVr9+/SJIhFD17ds3sqPmqMZZtGiR9u3bF8lY2fSXv/xFe/bsiaR0Jenxxx/XuHHjMh7nJz/5iV577bWwCryreOONN/Stb31LpaWlkYx33nnnaePGjRmPM3LkSL3//vvBFfgLL7ygHTt2RDrmb37zm4zHGDRokL7whS9EkAYhKywsjKzAoxrnwQcfjGScU9UtClySzj777EiOmqXofjt6jx5h7t558+bpvPPOU+/evSMbc8WKFRl9/d/+9jft3r1bW7dujSgREL4wGwadyt3185//XMOHD487ylEvv/yypkyZEncMoEvhYVYAECgKHAACRYEDCEZeXp5SqZRWr16tpqamuOPo73//uyoqKvT6668rLy8v6/N3iwLPy8vT6tWrtWbNmi7xP7Wurk5LlizRO++8o9zc3LjjIEZmpvfee09/+tOf1NzcHHccSYev2nr++ecjuZ482+bMmaO5c+fqjjvu0Pnnn6/S0lI999xzkV140B6NjY36wx/+oBkzZmjgwIFavny57rnnHl166aVZy3CUu2ft7ZJLLvHOsHfvXr/77rv90ksv9fz8fL/uuuu8pqbGm5qaOmW+43n33Xf917/+tV911VXeu3dvnzZtmq9atSpr80dp8uTJPmjQIL/55ps9nU57S0tLLDlaWlo8nU77zTff7IMGDfLJkyfHkiMTLS0t/sADD/iIESN84MCBfuutt/orr7yS9Rzvv/++P/LII37FFVf4pz/9ab/++uv9rbfeynqOKL322mt+xx13+LBhw3zIkCF+++23e21tbafM1dLS4qlUyq+//nrPz8/3yy+/3B944AGvq6vrlPnakpT243RqtyjwY73++uv+s5/9zC+++GIfMGCAl5aWeiqV8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ezP6LAvoxX6/mQJBQACRYEDQKAo8ONbGneAbob9GR32ZbSC3p+sgQNAoDgCB4BAUeAAECgK/Bhm9rCZ1ZlZ1/y9SgExswvMrMbMtpnZy2a2IO5MITOzXmb2gpltbt2fP447U+jMLMfMXjSzVXFn6SgK/MMelXR13CG6iSZJN7n7hZIuk/SvZnZhzJlCdlDSl939nyVdLOlqM7ss5kyhWyCpNu4QmaDAj+Hu6yTtjTtHd+Dub7v7xtb3/0uH/6GcH2+qcPlhf2/9MLf1jSsQOsjMBkoaL+mhuLNkggJHpzOzAkkjJD0fb5Kwtf7Iv0lSnaS17s7+7Lh7Jf1A//iF7EGiwNGpzOxMSSsl3eju78edJ2Tu3uzuF0saKOkLZlYUd6YQmdkESXXuviHuLJmiwNFpzCxXh8u7wt1/G3ee7sLd90mqEedrOmqUpIlm9oakJyR92cwejzdSx1Dg6BRmZpLKJdW6+z1x5wmdmeWbWZ/W9/9J0lhJr8SbKkzufqu7D3T3AknTJFW7+6yYY3UIBX4MM0tKek7SMDPbaWYlcWcK2ChJs3X46GZT69vX4g4VsAGSasxsi6T/q8Nr4MFe/oZocCs9AASKI3AACBQFDgCBosABIFAUOAAEigIHgEBR4AAQKAocAAL1/wE64wmrwkDhtgAAAABJRU5ErkJggg==\n" 58 | }, 59 | "metadata": { 60 | "needs_background": "light" 61 | } 62 | } 63 | ], 64 | "source": [ 65 | "import numpy as np\n", 66 | "import matplotlib.pyplot as plt\n", 67 | "\n", 68 | "n = 4\n", 69 | "x = range(n)\n", 70 | "y = 5+np.random.randn(100,4)\n", 71 | "y[3,2] *= 2 # create some outlier\n", 72 | "\n", 73 | "plt.boxplot(y, 'gD')\n", 74 | "plt.show()" 75 | ] 76 | } 77 | ] 78 | } -------------------------------------------------------------------------------- /08_04.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyMTOLdyREkruWzXMLAhXSaG", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 8.4 Pickle\n", 33 | "Pickle format is very fast to read and write. But it is only useful when you want to keep data for yourself, e.g. write data from one program, and read the same data into another program. First we import cPicle and call it pickle." 34 | ], 35 | "metadata": { 36 | "id": "baLD7YoueXlK" 37 | } 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 4, 42 | "metadata": { 43 | "colab": { 44 | "base_uri": "https://localhost:8080/" 45 | }, 46 | "id": "el3vIi4CeTaE", 47 | "outputId": "7d088249-9c48-44c9-81b5-219939a1808e" 48 | }, 49 | "outputs": [ 50 | { 51 | "output_type": "stream", 52 | "name": "stdout", 53 | "text": [ 54 | "Mounted at /content/gdrive\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "import pickle as pickle\n", 60 | "from google.colab import drive\n", 61 | "\n", 62 | "drive.mount(\"/content/gdrive\")\n", 63 | "fname = \"/content/gdrive/My Drive/Colab Notebooks/LearnPython/data/tmp.pkl\"" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "source": [ 69 | "We define one variable e.g. a list and first save it and then read it. To save, we use *pickle.dump* to save the data." 70 | ], 71 | "metadata": { 72 | "id": "SVZ6CnbZegj1" 73 | } 74 | }, 75 | { 76 | "cell_type": "code", 77 | "source": [ 78 | "var = [2, 5, 8, 'foo']\n", 79 | "pickle.dump(var, open(fname, \"wb\"))" 80 | ], 81 | "metadata": { 82 | "id": "yovvcy0leioJ" 83 | }, 84 | "execution_count": 5, 85 | "outputs": [] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "source": [ 90 | "Now we read the same data and print:" 91 | ], 92 | "metadata": { 93 | "id": "dItXZB3melJm" 94 | } 95 | }, 96 | { 97 | "cell_type": "code", 98 | "source": [ 99 | "var1 = pickle.load(open(fname, \"rb\"))\n", 100 | "print(var1)" 101 | ], 102 | "metadata": { 103 | "colab": { 104 | "base_uri": "https://localhost:8080/" 105 | }, 106 | "id": "j7WZtlECfRGm", 107 | "outputId": "f406f9dd-931c-48e5-ec8a-25e517c29f40" 108 | }, 109 | "execution_count": 8, 110 | "outputs": [ 111 | { 112 | "output_type": "stream", 113 | "name": "stdout", 114 | "text": [ 115 | "[2, 5, 8, 'foo']\n" 116 | ] 117 | } 118 | ] 119 | } 120 | ] 121 | } -------------------------------------------------------------------------------- /09_01.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyNJizmmyNvTW8YO7aY3Nz9L", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "# 9 Numerical Modelling\n", 33 | "\n", 34 | "## 9.1 Integration\n", 35 | "Suppose we want to integrate some function and the function can not be integrated analytically, then we go for numerical integration. To check if our numerical integration shcems is working properly or not, we integrate the function for which analytical solution is available and then we compare our solution. So let us begin with function of one variable. Suppose, we have function $f (x) = x$ to integrate over limit 0 to 10. We know from mathematics that the answer is 50. Let us now try numerical methods about the solution. We will use integrate library of scipy. For simple function we an use lambad function instead of def to define the function. The function *integrate.quad* perform our task. It returns the integration and error in integration." 36 | ], 37 | "metadata": { 38 | "id": "tssPDz212YV0" 39 | } 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 5, 44 | "metadata": { 45 | "colab": { 46 | "base_uri": "https://localhost:8080/" 47 | }, 48 | "id": "UsPeDjdezTkL", 49 | "outputId": "7d64466c-4f2c-4f9b-a0e9-ceb392ff5f86" 50 | }, 51 | "outputs": [ 52 | { 53 | "output_type": "stream", 54 | "name": "stdout", 55 | "text": [ 56 | "50.0 5.551115123125783e-13\n" 57 | ] 58 | } 59 | ], 60 | "source": [ 61 | "from scipy import integrate\n", 62 | "import numpy as np\n", 63 | "y_fun = lambda x: x\n", 64 | "y,err = integrate.quad(y_fun,0,10)\n", 65 | "print(y, err)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "source": [ 71 | "We get the 50.0 as answer which is exactly the analytical solution, and also it says that error is the solution is very low. It could be by chance, that we get accurate solution from the numerical scheme. So let us try one more function, this time exponential. We will integrate f(x) = exp(−x) over 0 to ∞." 72 | ], 73 | "metadata": { 74 | "id": "YWZ-nVWg3JCp" 75 | } 76 | }, 77 | { 78 | "cell_type": "code", 79 | "source": [ 80 | "y_func = lambda x: np.exp(-x)\n", 81 | "y = integrate.quad(y_func, 0, np.inf)\n", 82 | "print(y)" 83 | ], 84 | "metadata": { 85 | "colab": { 86 | "base_uri": "https://localhost:8080/" 87 | }, 88 | "id": "9T8-S-TA3MhU", 89 | "outputId": "dc64b2ca-99c8-42a7-99ed-462026fdb371" 90 | }, 91 | "execution_count": 6, 92 | "outputs": [ 93 | { 94 | "output_type": "stream", 95 | "name": "stdout", 96 | "text": [ 97 | "(1.0000000000000002, 5.842606742906004e-11)\n" 98 | ] 99 | } 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "source": [ 105 | "We see that the solution is very near to the analytical solution (1.0). These function had only the vari- able as input, but we may want to have additional parameter in the function, e.g. f (x) = ex p(−ax, and assume a = 0.5 for this example. The integrate.quad provides options for providing additional argument also." 106 | ], 107 | "metadata": { 108 | "id": "Ev1ZxCAN3aFT" 109 | } 110 | }, 111 | { 112 | "cell_type": "code", 113 | "source": [ 114 | "f = lambda x,a : np.exp(-a*x)\n", 115 | "y, err = integrate.quad(f, 0, 1, args=(0.5,))\n", 116 | "print(y)" 117 | ], 118 | "metadata": { 119 | "colab": { 120 | "base_uri": "https://localhost:8080/" 121 | }, 122 | "id": "xP3usgBH3hKa", 123 | "outputId": "f592c7b2-a472-4589-ba8b-7e10f5cc652c" 124 | }, 125 | "execution_count": 7, 126 | "outputs": [ 127 | { 128 | "output_type": "stream", 129 | "name": "stdout", 130 | "text": [ 131 | "0.786938680574733\n" 132 | ] 133 | } 134 | ] 135 | } 136 | ] 137 | } -------------------------------------------------------------------------------- /10_02.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyNisSQD/tRespIk5J7OgOda", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 10.2 Multivariate distribution\n", 33 | "So far, we have generated random variables with only univariate distribution. In this section we will be generating multivariate nominally distributed random variable by specifying the mean and covariance matrix to np.random.multivariate_normal." 34 | ], 35 | "metadata": { 36 | "id": "B9F0YPxOHgaD" 37 | } 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "metadata": { 43 | "id": "nqfrnssVHAPk" 44 | }, 45 | "outputs": [], 46 | "source": [ 47 | "mean = [0,5]\n", 48 | "cov = [[1,0.4],[0.4,1]]\n", 49 | "data = np.random.multivariate_normal(mean,cov,5000)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "source": [ 55 | "We can check its mean and covariance." 56 | ], 57 | "metadata": { 58 | "id": "BtmoPSfNHn0Q" 59 | } 60 | }, 61 | { 62 | "cell_type": "code", 63 | "source": [ 64 | "print(data.mean(axis=0))\n", 65 | "print(np.corrcoef(data.T))" 66 | ], 67 | "metadata": { 68 | "id": "I0Dz8K6tHp0j" 69 | }, 70 | "execution_count": null, 71 | "outputs": [] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "source": [ 76 | "We see that generated random variable had mean and covariance close to the specified input." 77 | ], 78 | "metadata": { 79 | "id": "FbqAxBrAHtdu" 80 | } 81 | } 82 | ] 83 | } -------------------------------------------------------------------------------- /10_04.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyPPwrAlI/m368qc4NrvgU4Y", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "source": [ 32 | "## 10.4 Bias Correction\n", 33 | "CDF matching is one of the commonly used approach for bias correction in hydrological data. For a hydrological variable x the method is written as,\n", 34 | "\n", 35 | "$ x_{m-adjusted} = F^{−1}_{o-c} \\left(F_{m-c}\\left(x_{m-p}\\right) \\right) $\n", 36 | "\n", 37 | "where, subscript $m$ is for model, $o$ is for observation, $c$ is for current, $p$ is for prediction and $F$ is the CDF. \n", 38 | "\n", 39 | "We will make bias correction using the bias_correction function of the ambhas.stats library. We begin with importing the library. Then we generate some random numbers to apply the bias corrections. We deliberately modify the mean of the modeled variable, so that the bias correction method can be compared. As we can see, the mean of the modeled prediction was 2, while the mean of observed data is approximately 0 before applying bias correction. The mean of the bias adjusted modeled prediction has become approximately 0 after applying the CDF based bias correction." 40 | ], 41 | "metadata": { 42 | "id": "FE0etMJcrvnJ" 43 | } 44 | }, 45 | { 46 | "cell_type": "code", 47 | "source": [ 48 | "import numpy as np\n", 49 | "from ambhas.stats import bias_correction\n", 50 | "oc = np.random.randn(100)\n", 51 | "mc = 5+np.random.randn(100)\n", 52 | "mp = 2+np.random.randn(1000)\n", 53 | "\n", 54 | "print(\"mean of observed current is %f\"%oc.mean())\n", 55 | "print(\"mean of modeled current is %f\"%mc.mean())\n", 56 | "print(\"mean of modeled prediction is %f\"%mp.mean())\n", 57 | "\n", 58 | "mp_adjusted = bias_correction(oc, mc, mp)\n", 59 | "print(\"mean of adjusted modeled prediction is %f\"%mp_adjusted.mean())" 60 | ], 61 | "metadata": { 62 | "colab": { 63 | "base_uri": "https://localhost:8080/" 64 | }, 65 | "id": "0aS1cWC8w71N", 66 | "outputId": "6c8d3954-8ac6-4b57-cb6a-b89b1398e7d8" 67 | }, 68 | "execution_count": 4, 69 | "outputs": [ 70 | { 71 | "output_type": "stream", 72 | "name": "stdout", 73 | "text": [ 74 | "mean of observed current is -0.122826\n", 75 | "mean of modeled current is 5.105387\n", 76 | "mean of modeled prediction is 2.034841\n", 77 | "mean of adjusted modeled prediction is 0.000000\n" 78 | ] 79 | }, 80 | { 81 | "output_type": "stream", 82 | "name": "stderr", 83 | "text": [ 84 | "/usr/local/lib/python3.7/dist-packages/ambhas/stats.py:56: RuntimeWarning: invalid value encountered in double_scalars\n", 85 | " mp_adjusted[mp_adjusted<0])/(np.sum(mp_adjusted>0))\n" 86 | ] 87 | } 88 | ] 89 | } 90 | ] 91 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Learning Python for Modelling 2 | 3 | Version 1.0.0 4 | 5 | *Sat Kumar Tomer* 6 | 7 | History: 8 | * 0.0.0: First edition of Python in Hydrology in November, 2011. 9 | * 0.1.0: Added sections on variogram analysis, Kriging, bias correction, and calling external commands from 10 | Python in January, 2012. 11 | * 0.2.0: Fully edited the book in June, 2013. 12 | * 1.0.0: Updated and moved the book to Google Colab in September, 2022. 13 | 14 | The book was originally in the form of a LATEX source code. Current version of the book is written in Google Colab. The latest source code for the book is available from https://github.com/tomersk/learn-python 15 | 16 | # Table of Contents 17 | 18 | ## [Preface](https://github.com/tomersk/learn-python/blob/main/00_00_preface.ipynb) 19 | 20 | ## 1. Getting Started 21 | 22 | ### 1.1. [Why Python?](https://github.com/tomersk/learn-python/blob/main/01_GettingStarted.ipynb) [Watch on Youtube](https://www.youtube.com/watch?v=Yi7qUcmaIEM&t=161s) 23 | ### 1.2. [Python Installation](https://github.com/tomersk/learn-python/blob/main/01_02_python_installation.ipynb) 24 | ### 1.3. [Install additional packages](https://github.com/tomersk/learn-python/blob/main/01_03.ipynb) 25 | ### 1.4. [Interactive Development Environment](https://github.com/tomersk/learn-python/blob/main/01_04.ipynb) 26 | ### 1.5. [Execute the program](https://github.com/tomersk/learn-python/blob/main/01_05.ipynb) 27 | ### 1.6. [Type of errors](https://github.com/tomersk/learn-python/blob/main/01_06.ipynb) 28 | ### 1.7. [The first program](https://github.com/tomersk/learn-python/blob/main/01_07.ipynb) 29 | 30 | ## 2. [A bit of Python](https://github.com/tomersk/learn-python/blob/main/02_00.ipynb) 31 | ### 2.1. [Data type](https://github.com/tomersk/learn-python/blob/main/02_01.ipynb) 32 | ### 2.2. [Data structures](https://github.com/tomersk/learn-python/blob/main/02_02.ipynb) 33 | ### 2.3. [Choosing the name of variable](https://github.com/tomersk/learn-python/blob/main/02_03.ipynb) 34 | ### 2.4. [Operators and operands](https://github.com/tomersk/learn-python/blob/main/02_04.ipynb) 35 | ### 2.5. [Expressions](https://github.com/tomersk/learn-python/blob/main/02_05.ipynb) 36 | ### 2.6. [Control Flow](https://github.com/tomersk/learn-python/blob/main/02_06.ipynb) 37 | ### 2.7. [Function](https://github.com/tomersk/learn-python/blob/main/02_07.ipynb) 38 | ### 2.8. [Plotting](https://github.com/tomersk/learn-python/blob/main/02_08.ipynb) 39 | 40 | ## 3. Array and dataframe 41 | ### 3.1. [Generating sequential arrays](https://github.com/tomersk/learn-python/blob/main/03_01.ipynb) 42 | ### 3.2. [Useful attributes and methods](https://github.com/tomersk/learn-python/blob/main/03_02.ipynb) 43 | ### 3.3. [Indexing](https://github.com/tomersk/learn-python/blob/main/03_03.ipynb) 44 | ### 3.4. [Array Manipulation](https://github.com/tomersk/learn-python/blob/main/03_04.ipynb) 45 | ### 3.5. [Dataframe](https://github.com/tomersk/learn-python/blob/main/03_05.ipynb) 46 | 47 | ## 4. Basic applications of Python 48 | ### 4.1. [Introduction](https://github.com/tomersk/learn-python/blob/main/04_01.ipynb) 49 | ### 4.2. [Water Vapor](https://github.com/tomersk/learn-python/blob/main/04_02.ipynb) 50 | ### 4.3. [Terminal velocity of precipitation](https://github.com/tomersk/learn-python/blob/main/04_03.ipynb) 51 | ### 4.4. [Rainfall](https://github.com/tomersk/learn-python/blob/main/04_04.ipynb) 52 | ### 4.5. [Evaporation](https://github.com/tomersk/learn-python/blob/main/04_05.ipynb) 53 | ### 4.6. [Infiltration](https://github.com/tomersk/learn-python/blob/main/04_06.ipynb) 54 | ### 4.7. [Surface water](https://github.com/tomersk/learn-python/blob/main/04_07.ipynb) 55 | ### 4.8. [River Routing–Muskingum method](https://github.com/tomersk/learn-python/blob/main/04_08.ipynb) 56 | ### 4.9. [Delta T](https://github.com/tomersk/learn-python/blob/main/04_09.ipynb) 57 | ### 4.10 [Unit Hydrograph](https://github.com/tomersk/learn-python/blob/main/04_10.ipynb) 58 | 59 | ## 5. Statistics 60 | ### 5.1. [Empirical distributions](https://github.com/tomersk/learn-python/blob/main/05_01.ipynb) 61 | ### 5.2. [Theoretical distributions](https://github.com/tomersk/learn-python/blob/main/05_02.ipynb) 62 | ### 5.3. [The t-test](https://github.com/tomersk/learn-python/blob/main/05_03.ipynb) 63 | ### 5.4. [KS test](https://github.com/tomersk/learn-python/blob/main/05_04.ipynb) 64 | ### 5.5. [The chi square test](https://github.com/tomersk/learn-python/blob/main/05_05.ipynb) 65 | ### 5.6. [Measure of statistical dependence](https://github.com/tomersk/learn-python/blob/main/05_06.ipynb) 66 | ### 5.7. [Linear regression](https://github.com/tomersk/learn-python/blob/main/05_07.ipynb) 67 | ### 5.8. [Polynomial regression](https://github.com/tomersk/learn-python/blob/main/05_08.ipynb) 68 | ### 5.9. [Interpolation](https://github.com/tomersk/learn-python/blob/main/05_09.ipynb) 69 | ### 5.10. [Autocorrelation](https://github.com/tomersk/learn-python/blob/main/05_10.ipynb) 70 | ### 5.11. [Uncertainty Intervals](https://github.com/tomersk/learn-python/blob/main/05_11.ipynb) 71 | ### 5.12. [Non parameteric trend and slope](https://github.com/tomersk/learn-python/blob/main/05_12.ipynb) 72 | 73 | ## 6. Spatial Data 74 | ### 6.1. [Types of spatial data](https://github.com/tomersk/learn-python/blob/main/06_01.ipynb) 75 | ### 6.2. [Geoinformation](https://github.com/tomersk/learn-python/blob/main/06_02.ipynb) 76 | ### 6.3. [Writing Raster](https://github.com/tomersk/learn-python/blob/main/06_03.ipynb) 77 | ### 6.4. [Writing Vector](https://github.com/tomersk/learn-python/blob/main/06_04.ipynb) 78 | ### 6.5. [Reading the raster](https://github.com/tomersk/learn-python/blob/main/06_05.ipynb) 79 | ### 6.6. [Read the vector](https://github.com/tomersk/learn-python/blob/main/06_06.ipynb) 80 | ### 6.7. [Filtering](https://github.com/tomersk/learn-python/blob/main/06_07.ipynb) 81 | ### 6.8. [NDVI](https://github.com/tomersk/learn-python/blob/main/06_08.ipynb) 82 | ### 6.9. [Raster and Vector operations](https://github.com/tomersk/learn-python/blob/main/06_09.ipynb) 83 | ### 6.10. [Clustering](https://github.com/tomersk/learn-python/blob/main/06_10.ipynb) 84 | 85 | ## 7. Plotting 86 | ### 7.1. [Date axis](https://github.com/tomersk/learn-python/blob/main/07_01.ipynb) 87 | ### 7.2. [Bar charts](https://github.com/tomersk/learn-python/blob/main/07_02.ipynb) 88 | ### 7.3. [Pie charts](https://github.com/tomersk/learn-python/blob/main/07_03.ipynb) 89 | ### 7.4. [2D plots](https://github.com/tomersk/learn-python/blob/main/07_04.ipynb) 90 | ### 7.5. [3 D plots](https://github.com/tomersk/learn-python/blob/main/07_05.ipynb) 91 | ### 7.6. [Box-plot](https://github.com/tomersk/learn-python/blob/main/07_06.ipynb) 92 | ### 7.7. [Q-Q plot](https://github.com/tomersk/learn-python/blob/main/07_07.ipynb) 93 | ### 7.8. [plotyy](https://github.com/tomersk/learn-python/blob/main/07_08.ipynb) 94 | ### 7.9. [Annotation](https://github.com/tomersk/learn-python/blob/main/07_09.ipynb) 95 | ### 7.10. [Basemap] 96 | ### 7.11. [Shared axis](https://github.com/tomersk/learn-python/blob/main/07_11.ipynb) 97 | ### 7.12. [Subplot](https://github.com/tomersk/learn-python/blob/main/07_12.ipynb) 98 | 99 | ## 8. Input-Output 100 | ### 8.1. [xls](https://github.com/tomersk/learn-python/blob/main/08_01.ipynb) 101 | ### 8.2. [Text file](https://github.com/tomersk/learn-python/blob/main/08_02.ipynb) 102 | ### 8.3. [NetCDF](https://github.com/tomersk/learn-python/blob/main/08_03.ipynb) 103 | ### 8.4. [Pickle](https://github.com/tomersk/learn-python/blob/main/08_04.ipynb) 104 | ### 8.5. [CSV](https://github.com/tomersk/learn-python/blob/main/08_05.ipynb) 105 | 106 | ## 9. Numerical Modelling 107 | ### 9.1. [Integration](https://github.com/tomersk/learn-python/blob/main/09_01.ipynb) 108 | ### 9.2. [ODE](https://github.com/tomersk/learn-python/blob/main/09_02.ipynb) 109 | ### 9.3. [Differentiation](https://github.com/tomersk/learn-python/blob/main/09_03.ipynb) 110 | ### 9.4. [Parameter Estimation](https://github.com/tomersk/learn-python/blob/main/09_04.ipynb) 111 | 112 | ## 10. Advance statistics 113 | ### 10.1. [copula](https://github.com/tomersk/learn-python/blob/main/10_01.ipynb) 114 | ### 10.2. [Multivariate distribution](https://github.com/tomersk/learn-python/blob/main/10_02.ipynb) 115 | ### 10.3. [Geostatistics](https://github.com/tomersk/learn-python/blob/main/10_03.ipynb) 116 | ### 10.4. [Bias correction](https://github.com/tomersk/learn-python/blob/main/10_04.ipynb) 117 | ### 10.5. [PCA](https://github.com/tomersk/learn-python/blob/main/10_05.ipynb) 118 | ### 10.6. [Kalman filtering](https://github.com/tomersk/learn-python/blob/main/10_06.ipynb) 119 | -------------------------------------------------------------------------------- /data.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomersk/learn-python/e54ae1e15bf3790c8acbc558848e709eebef64d9/data.xls -------------------------------------------------------------------------------- /index.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "mount_file_id": "16_iC9WEqPsNm0DVlpxiWo8aJ3TrJ6ueg", 8 | "authorship_tag": "ABX9TyOAn+mIowIq4GQlF5EqIiEN", 9 | "include_colab_link": true 10 | }, 11 | "kernelspec": { 12 | "name": "python3", 13 | "display_name": "Python 3" 14 | }, 15 | "language_info": { 16 | "name": "python" 17 | } 18 | }, 19 | "cells": [ 20 | { 21 | "cell_type": "markdown", 22 | "metadata": { 23 | "id": "view-in-github", 24 | "colab_type": "text" 25 | }, 26 | "source": [ 27 | "\"Open" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "source": [ 33 | "# Python Data Science Handbook in Agtech\n", 34 | "\n", 35 | "Version 1.0.0\n", 36 | "\n", 37 | "*Sat Kumar Tomer*" 38 | ], 39 | "metadata": { 40 | "id": "91ygDi8mJGca" 41 | } 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "source": [ 46 | "History:\n", 47 | "* 0.0.0: First edition of Python in Hydrology in November, 2011.\n", 48 | "* 0.1.0: Added sections on variogram analysis, Kriging, bias correction, and calling external commands from\n", 49 | "Python in January, 2012.\n", 50 | "* 0.2.0: Fully edited the book in June, 2013.\n", 51 | "* 1.0.0: Updated and moved the book to Google Colab in September, 2022.\n", 52 | "\n", 53 | "The book was originally in the form of a LATEX source code. Current version of the book is written in Google Colab. The latest source code for the book is available from https://github.com/tomersk/learn-python" 54 | ], 55 | "metadata": { 56 | "id": "UbLsKmQtOrSR" 57 | } 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "source": [ 62 | "# Table of Contents\n", 63 | "\n", 64 | "## [Preface](https://github.com/tomersk/learn-python/blob/main/00_00_preface.ipynb)\n", 65 | "\n", 66 | "## [1. Getting Started](01.00-IPython-Beyond-Normal-Python.ipynb)\n", 67 | "\n", 68 | "### [1.1 Why Python?]()\n", 69 | "### 1.2.\tPython Installation \n", 70 | "### 1.3.\tInstall additional packages \n", 71 | "### 1.4.\tInteractive Development Environment \n", 72 | "### 1.5.\tExecute the program \n", 73 | "### 1.6.\tType of errors \n", 74 | "### 1.7.\tThe first program \n", 75 | "\n", 76 | "## 2.\tA bit of Python\n", 77 | "### 2.1.\tData type \n", 78 | "### 2.2.\tData structures \n", 79 | "### 2.3.\tChoosing the name of variable \n", 80 | "### 2.4.\tOperators and operands \n", 81 | "### 2.5.\tExpressions \n", 82 | "### 2.6.\tControl Flow \n", 83 | "### 2.7.\tFunction \n", 84 | "### 2.8.\tPlotting \n", 85 | "\n", 86 | "## 3.\tArray\n", 87 | "### 3.1.\tGenerating sequential arrays\n", 88 | "### 3.2.\tUseful attributes and methods \n", 89 | "### 3.3.\tIndexing \n", 90 | "### 3.4.\tArray Manipulation\n", 91 | "\n", 92 | "## 4.\tDataframe\n", 93 | "### 4.1.\tIntroduction\n", 94 | "### 4.2.\tIndexing in Dataframe\n", 95 | "### 4.3.\tTime series analysis using Dataframes\n", 96 | "\n", 97 | "## 5.\tBasic applications in Hydrology\n", 98 | "### 5.1.\tIntroduction \n", 99 | "### 5.2.\tWater Vapor\n", 100 | "### 5.3.\tPrecipitable Water in atmospheric column\n", 101 | "### 5.4.\tPrecipitation \n", 102 | "### 5.5.\tRainfall \n", 103 | "### 5.6.\tEvaporation \n", 104 | "### 5.7.\tInfiltration \n", 105 | "### 5.8.\tSurface water \n", 106 | "### 5.9.\tRiver Routing–Muskingum method \n", 107 | "\n", 108 | "## 6.\tAdvanced application in Hydrology (python-based modelling packages)\n", 109 | "### 6.1.\tAMBHAS\n", 110 | "### 6.2.\tPastas (Groundwater)\n", 111 | "\n", 112 | "## 7.\tStatistics\n", 113 | "### 7.1.\tEmpirical distributions \n", 114 | "### 7.2.\tTheoretical distributions \n", 115 | "### 7.3.\tThe t-test \n", 116 | "### 7.4.\tKS test \n", 117 | "### 7.5.\tThe chi square test \n", 118 | "### 7.6.\tMann-Kendall Test and Sen’s Slope\n", 119 | "### 7.7.\tMeasure of statistical dependence\n", 120 | "### 7.8.\tLinear regression \n", 121 | "### 7.9.\tPolynomial regression \n", 122 | "### 7.10.\tInterpolation \n", 123 | "### 7.11.\tAutocorrelation \n", 124 | "### 7.12.\tUncertainty Intervals\n", 125 | "\n", 126 | "## 8.\tSpatial Data (gdal and ogr incorporated into osgeo library)\n", 127 | "### 8.1.\tTypes of spatial data \n", 128 | "### 8.2.\tGeoinformation \n", 129 | "### 8.3.\tWriting Raster \n", 130 | "### 8.4.\tWriting Vector \n", 131 | "### 8.5.\tReading the raster \n", 132 | "### 8.6.\tRead the vector \n", 133 | "### 8.7.\tFiltering \n", 134 | "### 8.8.\tNDVI \n", 135 | "### 8.9.\tRaster and Vector operations: Clipping, extract values to point etc.\n", 136 | "\n", 137 | "## 9.\tPlotting\n", 138 | "### 9.1.\tDate axis \n", 139 | "### 9.2.\tBar charts \n", 140 | "### 9.3.\tPie charts \n", 141 | "### 9.4.\t2D plots \n", 142 | "### 9.5.\t3 D plots \n", 143 | "### 9.6.\tBox-plot \n", 144 | "### 9.7.\tQ-Q plot \n", 145 | "### 9.8.\tplotyy \n", 146 | "### 9.9.\tAnnotation \n", 147 | "### 9.10.\tBasemap \n", 148 | "### 9.11.\tShared axis \n", 149 | "### 9.12.\tSubplot \n", 150 | "\n", 151 | "## 10.\tInput-Output (Using pandas dataframe)\n", 152 | "### 10.1.\txls \n", 153 | "### 10.2.\tText file \n", 154 | "### 10.3.\tNetCDF \n", 155 | "### 10.4.\tPickle \n", 156 | "\n", 157 | "## 11.\tNumerical Modelling\n", 158 | "### 11.1.\tIntegration \n", 159 | "### 11.2.\tODE \n", 160 | "### 11.3.\tParameter Estimation\n", 161 | "\n", 162 | "## 12.\tAdvance statistics\n", 163 | "### 12.1.\tcopula \n", 164 | "### 12.2.\tMultivariate distribution\n", 165 | "\n", 166 | "## 13.\t Install library\n", 167 | "### 13.1.\tBasemap" 168 | ], 169 | "metadata": { 170 | "id": "LCgmDGPONTNW" 171 | } 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": null, 176 | "metadata": { 177 | "id": "2j-SEIs5JCVF" 178 | }, 179 | "outputs": [], 180 | "source": [] 181 | } 182 | ] 183 | } --------------------------------------------------------------------------------