You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2107 lines
290 KiB
2107 lines
290 KiB
12 months ago
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "92fb5068-0669-43e3-9799-9dca9ffe6638",
|
||
|
"metadata": {
|
||
|
"editable": true,
|
||
|
"jupyter": {
|
||
|
"source_hidden": true
|
||
|
},
|
||
|
"slideshow": {
|
||
|
"slide_type": ""
|
||
|
},
|
||
|
"tags": []
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"%%html\n",
|
||
|
"<style>\n",
|
||
|
"table {float:left}\n",
|
||
|
"</style>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "11174c4d-dcb5-49d8-96fc-b83ad35a6193",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<div>\n",
|
||
|
"<img src=\"https://cps.unileoben.ac.at/wp/CPS_Logo_Black.png\" width=\"260\"/>\n",
|
||
|
"</div>\n",
|
||
|
"Chair of Cyber-Physical-Systems, Austria"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "a04ead7b-4fce-4ea2-9501-d60a112aadcb",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<style>\n",
|
||
|
"td, th {\n",
|
||
|
" border: none!important;\n",
|
||
|
"}\n",
|
||
|
"</style>\n",
|
||
|
"\n",
|
||
|
"| Credentials | |\n",
|
||
|
"|----|---|\n",
|
||
|
"|Host | Montanuniversitaet Leoben |\n",
|
||
|
"|Web | https://cps.unileoben.ac.at |\n",
|
||
|
"|Mail | cps@unileoben.ac.at |\n",
|
||
|
"|Authors | Vedant Dave, Fotis Lygerakis, Linus Nwankwo, Melanie Neubauer, Nikolaus Feith and Elmar Rueckert|\n",
|
||
|
"|Corresponding Authors | melanie.neubauer@unileoben.ac.at, rueckert@unileoben.ac.at |\n",
|
||
|
"|Last edited | 02.10.2023 |\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "35a0d7a0",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Database Basics"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "4b05e025-9843-410e-80cd-f2db24148da8",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<div class=\"alert alert-block alert-success\">\n",
|
||
|
"This is a tutorial on the basics of interfacing databases for beginners. \n",
|
||
|
"</div>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "b9edce42-35e4-4dcd-886c-8d19a30bfdf0",
|
||
|
"metadata": {
|
||
|
"jp-MarkdownHeadingCollapsed": true
|
||
|
},
|
||
|
"source": [
|
||
|
"## Content\n",
|
||
|
"***\n",
|
||
|
"[1. Motivation for learning Python?](#sec:whypython) \n",
|
||
|
"\n",
|
||
|
"[2. How to run Python on your computer?](#sec:runpython)\n",
|
||
|
"\n",
|
||
|
"[3. Overview of Important Libraries](#sec:libraries)\n",
|
||
|
"\n",
|
||
|
"[4. Environments](#sec:environments)\n",
|
||
|
"***\n",
|
||
|
"[5. Let's Code Basic Commands](#sec:basiccommands)\n",
|
||
|
"\n",
|
||
|
"[6. Conditions & Loops](#sec:conditionsloops)\n",
|
||
|
"\n",
|
||
|
"[7. Lists & Dictionaries](#sec:lists)\n",
|
||
|
"***\n",
|
||
|
"[8. Numpy Arrays](#sec:arrays)\n",
|
||
|
"\n",
|
||
|
"[9. Functions and Efficient Implementations](#sec:functions)\n",
|
||
|
"\n",
|
||
|
"[10. Classes & Object Oriented Programming](#sec:classes)\n",
|
||
|
"\n",
|
||
|
"[11. Plotting & Scientific Figures](#sec:plotting)\n",
|
||
|
"***\n",
|
||
|
"[12. Advanced Features & Other Resource](#sec:resources)\n",
|
||
|
"\n",
|
||
|
"[13. Using Large Language Models like ChatGPT](#sec:llms)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "c4e0ceef-663a-46c3-9e52-6cd6c942422f",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# <a id=\"sec:whypython\">1. Motivation for Learning Python?</a>\n",
|
||
|
"- Easy to use.\n",
|
||
|
"- Lots of Libraries.\n",
|
||
|
"- Large Community (Stack Overflow).\n",
|
||
|
"- Easy to debug.\n",
|
||
|
"- Most popular programming language to date (see the image below taken from https://spectrum.ieee.org/the-top-programming-languages-2023 on Sept. 2023).\n",
|
||
|
"- Used in almost all research disciplines.\n",
|
||
|
" \n",
|
||
|
"<div>\n",
|
||
|
"<img src=\"https://cps.unileoben.ac.at/wp/statistics_2023_programming_language.png\" width=\"600\"/>\n",
|
||
|
"</div>\n",
|
||
|
"\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "ba97999c",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# <a id=\"sec:runpython\">2. How to run Python in your Computer?</a> \n",
|
||
|
"You have many IDE's. Here we are using **Jupyter Notebook**. The main reason is that that it is easy to write these passages and code in one single Notebook, then share it with others.\n",
|
||
|
"\n",
|
||
|
"If you are using Python for some personal project in your local PC, I recommend using **PyCharm** or **Visual Studio Code**. They are user friendly and very good for beginners. If you have any dobuts in them, you can contact me anytime."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"attachments": {
|
||
|
"ide1_ol5h4k.webp": {
|
||
|
"image/webp": "UklGRvQzAQBXRUJQVlA4TOgzAQAv24RtAP8HubbdRJItV/cwMzMGMJlMpL0dCIIZVs0MrpKl76EgINnWnrb5zQ4zlZn2gSvovOvtrJ0ycyUfi6X/h2PbbiJJtqtczcycddZnFkB7mEVOOEvhCSdjagajpPlXm7YBg1en3GEARmKGWCQGApFIJBaJAQmQAMmfF4n9b9KAgARIgARINCANCEiAJBLTgP7x++P/E/GR+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PeAb6axTVHvFWFJ6rSxZceGe1FKbWxtqMjYCUQvjNVNT07h23ZgfHWFOA4lGTXcSoyDMJB3sdwJWh6i12YUQVId3QtEVqpruOop77ACv85JkpdGDbpElCQI3DfzIxM0EsxwbaYbR7rO/Je8Vlrbv6dfuzMQA408SarWE4tPFIbzmBdc5+voGuUsDVpPHkhvLhxgOM+QkpY9usD+wiaT/thDBUk8qJjNybi+p7QxGtCGgwypxV1OYSHivHQWWJxf/wBnqdguNV23KiIvf8lnwM6vGvB8BwUlewlgOgrQVmQ2uC7F4YCC15MgOH8UnsWDC8aPDEsYVUdbgecVmC/IBD7pFltbHEv+nXqBxS7J5aszjvWd/Xxucg1+AgdcM3WGfoDscsKMCsqsDa1G3N0b6bmkOHkVVA195rRH7abEWs2irZQB5DJ5WSqIvr6CVnFMZESkycIRetD9B+xpbmOqcU5XEFwIN9xITEMSmkQHdCJxPzbEz45F834YCkaHXkSgljQIGqPFABTIutKREyq2EKhiLm3eIZahjSPWellYmrrZoDDHsFllL434qs4mC2ze8d8Ry5XiMfO/pScku5zty1j1ByFr
|
||
|
}
|
||
|
},
|
||
|
"cell_type": "markdown",
|
||
|
"id": "00b11a79",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"![ide1_ol5h4k.webp](attachment:ide1_ol5h4k.webp)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "cff5af33",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# <a id=\"sec:libraries\">3. Overview of Important Libraries</a> \n",
|
||
|
"### Arrays/ Matrix Compuation\n",
|
||
|
"- Numpy\n",
|
||
|
"- Numba\n",
|
||
|
"- Pandas (Tabular Data)\n",
|
||
|
"\n",
|
||
|
"### Plotting\n",
|
||
|
"- Matplotlib\n",
|
||
|
"- Plotly\n",
|
||
|
"- Seaborn (Advanced)\n",
|
||
|
"- Mayavi (Very Advanced)\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"### Machine Learning\n",
|
||
|
"- Scipy\n",
|
||
|
"- Scikit-learn\n",
|
||
|
"- Catboost\n",
|
||
|
"\n",
|
||
|
"### Neural Networks\n",
|
||
|
"- Pytorch\n",
|
||
|
"- Tensorflow"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "14b350a1",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# This command is only for Numpy installation\n",
|
||
|
"%pip install numpy"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "482cbfe2",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Import a Library in your Code\n",
|
||
|
"import numpy as np"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "f6217301",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# <a id=\"sec:environments\">4. Environments</a> \n",
|
||
|
"Why do you sometimes need local environments?\n",
|
||
|
"- Conflicts\n",
|
||
|
"- Dependencies\n",
|
||
|
"- Extra Libraries"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"attachments": {
|
||
|
"Screenshot%20from%202022-10-03%2017-41-19.png": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAABIsAAAKOCAIAAACp8A6wAAAAA3NCSVQICAjb4U/gAAAAGXRFWHRTb2Z0d2FyZQBnbm9tZS1zY3JlZW5zaG907wO/PgAAIABJREFUeJzs3XdYVGf6PvD7TKN3QYoVCyqIBQEL9hJjSewak6gxPXE35ReTTcymfjfJriYxxfQeE2MsUaMmVmyoFFEQpIgU6b0zwJTz+2MGZkBUVGAo9+fKtRcMZ848YY1wz/u8zyuIoggiIiIiIiJqBySmLoCIiIiIiIj0mNCIiIiIiIjaC5nhQ40GNUrTVUJERERERNQlmVlAKtV9aJTQLpxCciwEwTQ1EREREdFNcYJAh8Pfrqk5BAH3rIZcgQYJTdQCAv+zJyIiIiJqMfztmprD3AK11bqExn1oRERERERE7QUTGhERERERUXvBhEZERERERNReMKERERERERG1F0xoRERERERE7QUTGhERERERUXvBhEZERERERNReMKERERERERG1F0xoRERERERE7QUTGhERERERUXvBhEZERERERNReMKERERERERG1F0xoRERERERE7QUTGhERERERUXvBhEZERERERNReMKERERERERG1F0xoRERERERE7YXM1AUQERF1KHmFyCtAZg4KSpBXgJIy1Kr0/6tWGy4TRcjlUMhhZQErS/0/7i6wNIe9Hext0cMNVham+9cgIqJ2igmNiIjohjJzkJGDpDQkJiMtE1XK5j6xugYACouve4GFOVyc4N4dLt3QzR4ernCwg4drC9RMREQdFhMaERHRNYpLkZiCiIu4lIj8oiYukMng7Ah7W7h0g0IGezuYm0EiwLJuWay2FrVqVNegsgoqFYrLUFyKKiXyiwxLbcpqpGUiLbOJO7s4wcMVbs6MbUREXQ0TGhEREQBApUJkLJLScD4GV7Maf9XNBR6uGNAH7t3RtydcnG7/hSqVKClFRg4KipCVi7xC5BUakptajew8ZOchKs7wFF1s83CFixOcHGBvCxcnuDjBwe72yyAionaJCY2IiLq2zByEReFyKiKiG3/JzQUjvNG/N0b4tOSeMSsLWFk0XhZTqVBRhcwcww631IwmYlsjMhkszWFvCwc7WFrApRtsreFgCytL2NvqHyciog6FCY2IiLoe3XJZXBLOxzaOPU4O8PHCsEEY6HlHC2W3Si6Hgx0c7ODj1aDO4jL9YJLsfP2QkvwiKKv1F6jVKKtAWUUTi346MhkszGFtCRcn2NvB3QXdHOFgq2+eJCKi9ocJjYiIuozMHFy4hMQUhEc3mLsok8F7AIYMwPAh6NvTdPVdQy7XdzMaxzbU9UnmFaKiCgVFKC3Xb3IrLkVxGcorDFeq1SivQHlFE+tvFubo0wM9XNHLHR6u8PKEXN7q/0ZERHQzTGhERNSp1aeypNTGMz/cXDDQE8MGtXATYxtosk+ynq5hsqQMJWWorEJ+XYTLK0R+Icrq8puyGnFJiEvSfyqTY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1Z5rNm8GGRuqo5HIM7IuBffHofQ1OVK8fK/LNVv0EyOFDuDhMnZZYdSW3QitIXR1tpBBcAwJn/nxpx/nQXaUjV9vXRTTVlW0RpVpz7/sDHLzsJvZRJb12LMOi17B/zB6iAAAtAEB79vD5+9a8vD/QXgYA6se2bfDfnrH1ROL7Q4daQMw4tmNtZJmF1z0nX50xTKG7792Pbts4YXv02l8v3PPMSF0gFCQSQHP0WNrbbz+zro+u8Xj4JEXBwO8Sfz9x+f2hPp3vXRMmNCIiukXXNjTKZJgehNlT4OJk0sqIWo6HK+a76hsgw6IQHo2kVENUk8kwoA8mjWZUo06n9vKpnf+JUsFiyOLhNgIAa+8Vo6x3nkj8+WzxqpmOuoUtZdz5P4tE
|
||
|
}
|
||
|
},
|
||
|
"cell_type": "markdown",
|
||
|
"id": "7bb0ca61",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"![Screenshot%20from%202022-10-03%2017-41-19.png](attachment:Screenshot%20from%202022-10-03%2017-41-19.png)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "5f033898",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"What options do you have?\n",
|
||
|
"- [Virtual Environment](https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/)\n",
|
||
|
"- [Conda Environment](https://conda.io/projects/conda/en/latest/user-guide/getting-started.html)\n",
|
||
|
"\n",
|
||
|
"[Virtualenv vs Conda](https://www.reddit.com/r/learnpython/comments/xjzhxs/difference_between_virtualenv_venv_and_conda_env/): From what I understand, pip is a package manager for Python. venv is an environment manager for Python. conda is both a package and environment manager and is language agnostic. You can find even more explanation [here](https://towardsdatascience.com/a-guide-to-conda-environments-bc6180fc533#:~:text=venv%20is%20an%20environment%20manager,any%20language%20(in%20theory)."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "66f200c3",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# <a id=\"sec:basiccommands\">5. Let's Code Basic Commands</a> \n",
|
||
|
"## Print Statement"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "974c33a4",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"print(\"Hello World!\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "c8381ce9",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Value Assignments to Variables\n",
|
||
|
"Unlike C or C++, you don't need to predefine variables before assigning them some value. Things are simple in Python. Just assign the variable some value.\n",
|
||
|
"It must be noted that the standard interpreter in Python is written in C, thus the code is eventually represented as it is written in C. We will not go into th detail. If anyone is interested, they can refer to this [link](https://www.youtube.com/watch?v=54NWGAYhfbc)."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"id": "9464bc38",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Integer = 20\n",
|
||
|
"String = Hello\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"a = 20 # Integer\n",
|
||
|
"s = \"Hello\" # String\n",
|
||
|
"\n",
|
||
|
"print(\"Integer = \", a)\n",
|
||
|
"print(\"String = \", s)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "b4d96b2f",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Comments"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"id": "f304b4f5",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# This is a Single line Comment"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"id": "904aa124",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"'\\nThis is a \\nMulti line \\nComment\\n'"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 3,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"\"\"\"\n",
|
||
|
"This is a \n",
|
||
|
"Multi line \n",
|
||
|
"Comment\n",
|
||
|
"\"\"\""
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "94215fa0-27d8-489e-8286-3fb11240617a",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Exercises\n",
|
||
|
"\n",
|
||
|
"<div class=\"alert alert-block alert-warning\">\n",
|
||
|
"Create a variable that takes the concatenation of an integer value and of a constant string. \n",
|
||
|
"</div>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"id": "48502b54-997b-427b-83ba-1f4543c179e5",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Place your code here that concatenates an integer with a constant string.\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "56a96347",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# <a id=\"sec:conditionsloops\">6. Conditions & Loops</a> \n",
|
||
|
"Many might be familiar with this thing. This is just a simple conditional statement."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"id": "584af0eb",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"This variable is larger than 10\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"if a < 10:\n",
|
||
|
" print(\"This variable is smaller than 10\")\n",
|
||
|
"elif a > 10:\n",
|
||
|
" print(\"This variable is larger than 10\")\n",
|
||
|
"else:\n",
|
||
|
" pass # Do nothing, just pass the program to next line"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"id": "c09128a3",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"This variable is larger than 10\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# If-else statement in a single line\n",
|
||
|
"k = \"This variable is smaller than 10\" if a < 10 else \"This variable is larger than 10\"\n",
|
||
|
"print(k)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"id": "a8d79470",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"True\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Check if it is equal to some value\n",
|
||
|
"k = True if a == 20 else False\n",
|
||
|
"print(k)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "a5495504",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Other Operators\n",
|
||
|
"- != : Not equal to\n",
|
||
|
"- <= : Smaller than or equal to\n",
|
||
|
"- in : If a variable is in list or not\n",
|
||
|
"- is : Comparison of two variables in memory location terms"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "e6a7e562",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Loops"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "e490d6c9",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"for i in range(5):\n",
|
||
|
" print(i)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "91a5d09e",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Python indexing starts with 0 (not 1) and goes to n-1. Here, we print from 0 to 4 (0,...,4).\n",
|
||
|
"\n",
|
||
|
"The **range** function is the mostly used function. The purpose of range is to provide entries from 0 to n-1. For example **range(20)** will give you numbers (integers) from 0 to 19.\n",
|
||
|
"\n",
|
||
|
"You can do any computation with loops. The looping becomes powerful when used to extract out details or changing details from sequences."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "7e09994d",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# <a id=\"sec:lists\">7. Lists & Dictionaries</a> \n",
|
||
|
"The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number - its position or index. The first index is zero, the second index is one, and so forth.\n",
|
||
|
"\n",
|
||
|
"List contains a sequence of any data types. You can mix whatever data types you want.\n",
|
||
|
"\n",
|
||
|
"**Fun Fact:** You can also make a List of all the files inside certain folder in your computer. Then you can access them one by one and perform your desired operations (like extracting data from them)."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "93934611",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"list1 = [1, 24, \"My\", \"Name\", 58, 89.256, \"is\"]\n",
|
||
|
"list2 = [\"Slim\", 45, 0.567, \"Shady\"]\n",
|
||
|
"list_added = list1 + list2\n",
|
||
|
"\n",
|
||
|
"print(\"Added Lists: \", list_added)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "9dba34d0",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Append someting to List"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "9c143e9d",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"l = [1 , 2, 0.256]\n",
|
||
|
"\n",
|
||
|
"for i in range(len(l)):\n",
|
||
|
" list_added.append(l[i])\n",
|
||
|
" \n",
|
||
|
"print(\"New List : \", list_added)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "90503ee1",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Check if something is in your list"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "09d43b68",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"find_this = \"Slim\"\n",
|
||
|
"find_also_this = \"Shady\"\n",
|
||
|
"\n",
|
||
|
"# Everything in one line\n",
|
||
|
"finder = True if find_this and find_also_this in list_added else False\n",
|
||
|
"\n",
|
||
|
"# Print multiple variables in the print statement with Format\n",
|
||
|
"print(\"Are {} and {} in the given list?\".format(find_this, find_also_this))\n",
|
||
|
"print(finder)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "bd98fa5c",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Other List Operations\n",
|
||
|
"- Length\n",
|
||
|
"- Sort\n",
|
||
|
"- Reverse\n",
|
||
|
"\n",
|
||
|
"And many more. You can find them [here](https://docs.python.org/3/tutorial/datastructures.html)."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "7cf506f7",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Dictionaries\n",
|
||
|
"Also stores multipe data types into it. Each storage **value** has one **key** through which each value can be accessed.\n",
|
||
|
"\n",
|
||
|
"Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}. Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "cfed21c3",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"dict = {'Sensor': 'Joint1', 'Torque': 1, 'Joint_Config': [0.25, 0.55, 0.10]}\n",
|
||
|
"\n",
|
||
|
"print(\"Sensor : \", dict[\"Sensor\"])\n",
|
||
|
"print(\"Joint Position : \", dict[\"Joint_Config\"][0])\n",
|
||
|
"print(\"Joint Velocity : \", dict[\"Joint_Config\"][1])\n",
|
||
|
"print(\"Joint Acceleration : \", dict[\"Joint_Config\"][2])"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "b75fb1ae",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Get all the Keys and Values"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "41ecc0f4",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"print(\"All the Keys are: \", dict.keys(), \"\\n\")\n",
|
||
|
"\n",
|
||
|
"print(\"All the Values are: \", dict.values(), \"\\n\")\n",
|
||
|
"\n",
|
||
|
"print(\"All the Keys-Values pairs are: \", dict.items())"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "cc07bad4",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# To get them in form of list, just convert them to list\n",
|
||
|
"keys_in_list_form = list(dict.keys())\n",
|
||
|
"print(\"Keys in List Format:\", keys_in_list_form)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "a0683f0a",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Many Other methods for Dictionaries"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "fee69abc",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"- Update values\n",
|
||
|
"- Add new elements\n",
|
||
|
"\n",
|
||
|
"And Many more. You can find them [here](https://www.programiz.com/python-programming/methods/dictionary)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "d9deea1f",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# <a id=\"sec:arrays\">8. Numpy Arrays</a>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "0842f910",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<div class=\"alert alert-block alert-success\">\n",
|
||
|
"Numpy is a fundamental package for scientific computing in Python. It is a Python library that provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more.\n",
|
||
|
"</div>\n",
|
||
|
"\n",
|
||
|
"At the core of the NumPy package, is the <strong>ndarray object</strong>. This encapsulates n-dimensional arrays of homogeneous data types, with many operations being performed in compiled code for performance.\n",
|
||
|
"\n",
|
||
|
"NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and manipulate them very efficiently. NumPy is a Python library and is written partially in Python, but most of the parts that require fast computation are written in C or C++."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "9bddf001",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Some Examples of Arrays"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "8b5f237e",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Construct a simple 1D numpy array\n",
|
||
|
"arr_1D = np.array([1,2,3,4])\n",
|
||
|
"print(arr_1D)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "b3b6aa14",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Construct a 2D numpy array\n",
|
||
|
"arr_2D = np.array([\n",
|
||
|
" [1, 2, 3, 4],\n",
|
||
|
" [5, 6, 7, 8],\n",
|
||
|
" [9, 10, 11, 12]\n",
|
||
|
" ])\n",
|
||
|
"print(arr_2D)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "797e5acf",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Construct a 3D numpy array\n",
|
||
|
"arr_3D = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])\n",
|
||
|
"print(arr_3D)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "3e71acfa",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Check the dimensions of your array\n",
|
||
|
"print(arr_1D.ndim)\n",
|
||
|
"print(arr_2D.ndim)\n",
|
||
|
"print(arr_3D.ndim)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "9e463242",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Shape and Reshape"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "c90d6e51",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Find the shape of your array\n",
|
||
|
"print(\"Shape of arr_1D : \", np.shape(arr_1D))\n",
|
||
|
"print(\"Shape of arr_2D : \", np.shape(arr_2D))\n",
|
||
|
"print(\"Shape of arr_3D : \", np.shape(arr_3D))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "0589f0eb",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Reshape your arrays into any shape\n",
|
||
|
"arr_2D_into_1D = arr_2D.reshape(12,)\n",
|
||
|
"print(arr_2D_into_1D)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "56a8e21f",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# When you don't know the dimensions\n",
|
||
|
"arr_3D_into_1D = arr_3D.reshape(-1)\n",
|
||
|
"print(arr_3D_into_1D)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "1cc1a078",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# You want it to be some type (3,n) but don't know n\n",
|
||
|
"arr_3D_into_2D = arr_3D.reshape(3,-1)\n",
|
||
|
"print(arr_3D_into_2D)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "d8230d2e",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"**Note:** You cannot reshape the array of shape (3,20) into the shape of (59,1) because the reshaped array must have the same indices as the original one."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "a7a916c7",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Indexing Arrays"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "b507a091",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"How do you find one element in an array?"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "eb2fd658",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"arr = np.array([\n",
|
||
|
" [1, 2, 3, 4],\n",
|
||
|
" [5, 6, 7, 8],\n",
|
||
|
" [9, 10, 11, 12]\n",
|
||
|
" ])"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "8fe5e2d8",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Access 4th element in the 1st row\n",
|
||
|
"elem = 0"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "fcb84876",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Access all the elements from the final row\n",
|
||
|
"elems = arr[-1,:]\n",
|
||
|
"print(elems)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "787801f6",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Access all the elements from the final column\n",
|
||
|
"elems = arr[:,-1]\n",
|
||
|
"print(elems)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "e5502684",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Access two middle elements from the middle row\n",
|
||
|
"elems = 0"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "19bd4fcb",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Generate n samples between two numbers"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "fe8f8051",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"n = 20\n",
|
||
|
"l = np.linspace(0,1,n)\n",
|
||
|
"print(l)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "e515af41",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Generate Random arrays"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "92ef427d",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"r = np.random.rand(5,5)\n",
|
||
|
"print(r)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "00332ec8",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Concatenate two arrays"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "a84961b8",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"arr1 = np.array([[1, 2, 3]])\n",
|
||
|
"arr2 = np.array([[4, 5, 6]])\n",
|
||
|
"print(\"Shape of array 1: \", arr1.shape)\n",
|
||
|
"print(\"Shape of array 2: \", arr2.shape)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "4b092f35",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"arr = np.concatenate((arr1,arr2))\n",
|
||
|
"print(arr)\n",
|
||
|
"print(\"Shape of array concatenated along rows: \", arr.shape)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "a5ba1882",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"arr_col = np.concatenate((arr1,arr2), axis=1)\n",
|
||
|
"print(arr_col)\n",
|
||
|
"print(\"Shape of array concatenated along columns: \", arr_col.shape)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "78c5aa8d",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Converting Lists to Arrays"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "12328b05",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# List\n",
|
||
|
"l = [[1,2,3],[4,5,6]]\n",
|
||
|
"\n",
|
||
|
"# Print and Check type\n",
|
||
|
"print(l)\n",
|
||
|
"print(type(l))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "fed041ec",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# List converted to array\n",
|
||
|
"arr = np.asarray(l)\n",
|
||
|
"\n",
|
||
|
"# Print and Check type\n",
|
||
|
"print(arr)\n",
|
||
|
"print(type(arr))\n",
|
||
|
"\n",
|
||
|
"# Check Shape\n",
|
||
|
"print(\"Array Shape: \",arr.shape)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "f8137d0a",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"As lists can be of undefined shape, it sometime creates problem when converted to the list. The problem is not faced by the program for conversion, but more while accessing data from indexes. You might have to define it to store it as an object type data."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "866f6422",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# List\n",
|
||
|
"l = [[1,2,3],[4]]\n",
|
||
|
"\n",
|
||
|
"# Print and Check type\n",
|
||
|
"print(l)\n",
|
||
|
"print(type(l))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "969ced1c",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# List converted to array\n",
|
||
|
"arr = np.asarray(l)\n",
|
||
|
"\n",
|
||
|
"# Print and Check type\n",
|
||
|
"print(arr)\n",
|
||
|
"print(type(arr))\n",
|
||
|
"\n",
|
||
|
"# Check Shape\n",
|
||
|
"print(\"Array Shape: \",arr.shape)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "d953a0f3",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Search index of your desired variable"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "7c043f28",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"arr = np.array([\n",
|
||
|
" [1, 2, 3, 4],\n",
|
||
|
" [5, 6, 7, 8],\n",
|
||
|
" [9, 10, 11, 12]\n",
|
||
|
" ])\n",
|
||
|
"\n",
|
||
|
"# Find the index\n",
|
||
|
"idx= np.where(arr==11)\n",
|
||
|
"print(idx)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "9d20535b",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# <a id=\"sec:functions\">9. Functions</a>\n",
|
||
|
"Most important part of any language. It is a code block that is callable and reusable."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "638e9c10",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def function_for_multiply(m1,m2):\n",
|
||
|
" return m1.dot(m2)\n",
|
||
|
"\n",
|
||
|
"m1 = np.random.rand(50,30)\n",
|
||
|
"m2 = np.random.rand(30,500)\n",
|
||
|
"\n",
|
||
|
"m = function_for_multiply(m1,m2)\n",
|
||
|
"print(m.shape)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "6774c704",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Improving the Computational Times with Numba"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "c6e67f2d",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Numba is a just-in-time compiler for Python that works best on code that uses NumPy arrays and functions, and loops.\n",
|
||
|
"\n",
|
||
|
"It is an additional library that makes your code faster. It can make it very fast. But it depends on your code. \n",
|
||
|
"\n",
|
||
|
"Numba is suited for codes that have:\n",
|
||
|
"- Loops\n",
|
||
|
"- Numpy functions\n",
|
||
|
"\n",
|
||
|
"More information on Numba can be found [here](https://numba.pydata.org/numba-doc/latest/index.html)."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "22b0f024",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Install numba\n",
|
||
|
"%pip install numba"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "ea21f6d0",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import numba as nb"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "f250a3d4",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import math\n",
|
||
|
"\n",
|
||
|
"def std(xs):\n",
|
||
|
" # compute the mean\n",
|
||
|
" mean = 0\n",
|
||
|
" for x in xs: \n",
|
||
|
" mean += x\n",
|
||
|
" mean /= len(xs)\n",
|
||
|
" # compute the variance\n",
|
||
|
" ms = 0\n",
|
||
|
" for x in xs:\n",
|
||
|
" ms += (x-mean)**2\n",
|
||
|
" variance = ms / len(xs)\n",
|
||
|
" std = math.sqrt(variance)\n",
|
||
|
" return std"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "a8da7e65",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"a = np.random.normal(0, 1, 10000000)\n",
|
||
|
"std(a)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "bb4a64f7",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"c_std = nb.njit(std,fastmath=True)\n",
|
||
|
"c_std(a)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "37f4030f",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"%timeit std(a)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "40ff100a",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"%timeit c_std(a)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "c27f8c0d",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# <a id=\"sec:classes\">10. Classes & Object Oriented Programming</a>"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "718e6f20",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"<div class=\"alert alert-block alert-success\">\n",
|
||
|
"Python is an object oriented programming language. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by their class) for modifying their state.\n",
|
||
|
"</div>\n",
|
||
|
"\n",
|
||
|
"For more information on Object Oriented Programming, check out this [video](https://www.youtube.com/watch?v=-DP1i2ZU9gk)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "6baa3dc1",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Define a Class\n",
|
||
|
"class Student:\n",
|
||
|
" # Attributes\n",
|
||
|
" name = \"Vedant\"\n",
|
||
|
" matr = 1997\n",
|
||
|
" \n",
|
||
|
" # Method\n",
|
||
|
" def is_first_letter(self):\n",
|
||
|
" if self.name[0] == \"V\":\n",
|
||
|
" print(\"The name starts with the letter V\")\n",
|
||
|
" else:\n",
|
||
|
" print(\"The name does not starts with the letter V\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "71a58bcd",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Object Instantiation\n",
|
||
|
"student_1 = Student()\n",
|
||
|
"\n",
|
||
|
"# Access Class Attributes\n",
|
||
|
"print(\"The name of the Student is :\", student_1.name)\n",
|
||
|
"\n",
|
||
|
"# Call the function from the class\n",
|
||
|
"student_1.is_first_letter()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "6fcd77c7",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|