├── README.md ├── numpy_x_pandas ├── numpy_indexing.ipynb ├── numpy_slicing.ipynb ├── pandas_slicing_and_indexing.ipynb └── vectorization.ipynb └── requirements.txt /README.md: -------------------------------------------------------------------------------- 1 | # Python 2 | 3 | A collection of blog articles and code related to Python that I've curated. 4 | 5 | I regularly write about Python, Machine Learning, Freelancing, Productivity, and Self-Branding in my 6 | [Medium Blog](https://kurtispykes.medium.com). With a $5 a month commitment, you can unlock an 7 | unlimited access to stories on Medium. If you use my 8 | [sign-up](https://kurtispykes.medium.com/membership) link, I'll recieve a small commision. 9 | If you're already a member, [subscribe](https://kurtispykes.medium.com/subscribe) to recieve 10 | my posts directly to your inbox whenever I publish. 11 | 12 | 13 | ## Downloading the code 14 | 15 | The simplest way to download the code is to clone the repository with `git clone`: 16 | ``` 17 | git clone https://github.com/kurtispykes/python.git 18 | ``` 19 | ### The steps 20 | 1. `git clone ` 21 | 2. `cd ` 22 | 3. `pip install virtualenv` (if you don't already have virtualenv installed) 23 | 4. `virtualenv venv` to create your new environment (called 'venv' here) 24 | 5. `venv/bin/activate.bat` to enter the virtual environment 25 | 6. `pip install -r requirements.txt` to install the requirements in the current environment 26 | 27 | ## Table of Contents 28 | 29 | ### Python Primer 30 | * [Getting Started with Object Oriented Programming in Python 3](https://medium.com/geekculture/getting-started-with-object-oriented-programming-in-python-3-e0a87d38acfc) 31 | * [An Introduction To Exception Handling in Python 3](https://medium.com/geekculture/an-introduction-to-exception-handling-in-python-8a5b9c98d47f) 32 | * [Inheritance: Getting to grips with OOP in Python](https://medium.com/geekculture/inheritance-getting-to-grips-with-oop-in-python-2ec35b52570) 33 | * [Python Iterators & Generators](https://medium.com/geekculture/python-iterators-generators-ea63c5821550) 34 | * [Python Decorators](https://medium.com/geekculture/python-decorators-9a1c42e61a35) 35 | * [Python Namespaces and Scopes](https://medium.com/geekculture/python-namespaces-and-scopes-acd1b2da9797) 36 | * [Python *args & **kwargs](https://medium.com/geekculture/python-args-kwargs-1c5c0ec9121) 37 | 38 | ### Data Structures & Algorithms 39 | 1. [Big O Notation Explained By An ML Engineer](https://medium.com/geekculture/big-o-notation-explained-by-an-ml-engineer-2dc991503474) 40 | 2. [The Array Data Structure Explained](https://medium.com/geekculture/the-array-data-structure-explained-b8eb4c5d1f7a) 41 | 3. [The Stack Data Structure Explained](https://medium.com/geekculture/the-stack-data-structure-explained-28fda4de4816) 42 | 4. [The Queue Data Structure Explained](https://medium.com/geekculture/the-queue-data-structure-explained-a6c1891232ba) 43 | 44 | ### NumPy x Pandas 45 | * [Vectorization in Python](https://towardsdatascience.com/vectorization-in-python-46486819d3a): [See Full Code](numpy_x_pandas/vectorization.ipynb) 46 | * [Working with NumPy Arrays: Indexing](https://towardsdatascience.com/working-with-numpy-arrays-indexing-e4c08595ed57): [See Full Code](numpy_x_pandas/numpy_indexing.ipynb) 47 | * [Working with NumPy Arrays: Slicing](https://towardsdatascience.com/working-with-numpy-arrays-slicing-4453ec757ff0): [See Full Code](numpy_x_pandas/numpy_slicing.ipynb) 48 | * [Slicing & Indexing with Pandas](https://towardsdatascience.com/slicing-and-indexing-with-pandas-2bff05ec361e): [See Full Code](numpy_x_pandas/pandas_slicing_and_indexing.ipynb) 49 | 50 | 51 | ### Testing 52 | * [The Rules of Unit Test: 5 Best Practices](https://medium.com/pykes-technical-notes/the-rules-of-unit-test-5-best-practices-e2427a53400b) -------------------------------------------------------------------------------- /numpy_x_pandas/numpy_indexing.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np " 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "### 1-D Indexing" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 2, 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "output_type": "stream", 26 | "name": "stdout", 27 | "text": "[ 1 2 3 4 5 6 7 8 9 10]\n" 28 | } 29 | ], 30 | "source": [ 31 | "numbers= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", 32 | "# creating a 1-D array\n", 33 | "a= np.array(numbers)\n", 34 | "print(a)" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 5, 40 | "metadata": {}, 41 | "outputs": [ 42 | { 43 | "output_type": "stream", 44 | "name": "stdout", 45 | "text": "Index 0: 1\nIndex 5: 6\nIndex 7: 8\n" 46 | } 47 | ], 48 | "source": [ 49 | "print(f\"Index 0: {a[0]}\\nIndex 5: {a[5]}\\nIndex 7: {a[7]}\")" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "### 2-D Indexing" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 8, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "output_type": "stream", 66 | "name": "stdout", 67 | "text": "[[ 1 2 3 4 5 6 7 8 9 10]\n [ 10 20 30 40 50 60 70 80 90 100]]\n" 68 | } 69 | ], 70 | "source": [ 71 | "big_numbers= [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]\n", 72 | "# creating a 2-D array (list of list) \n", 73 | "b= np.array([numbers, big_numbers])\n", 74 | "print(b)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 9, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "output_type": "stream", 84 | "name": "stdout", 85 | "text": "50\n" 86 | } 87 | ], 88 | "source": [ 89 | "print(b[1, 4])" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 10, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "output_type": "stream", 99 | "name": "stdout", 100 | "text": "[ 1 2 3 4 5 6 7 8 9 10]\n" 101 | } 102 | ], 103 | "source": [ 104 | "# print the first row\n", 105 | "print(b[0])" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 11, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "output_type": "stream", 115 | "name": "stdout", 116 | "text": "[ 9 90]\n" 117 | } 118 | ], 119 | "source": [ 120 | "# full slice, column 8\n", 121 | "print(b[:, 8])" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": {}, 127 | "source": [ 128 | "### 3-D Indexing" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 12, 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "output_type": "stream", 138 | "name": "stdout", 139 | "text": "[[[ 1 2 3]\n [ 4 5 6]\n [ 7 8 9]]\n\n [[10 11 12]\n [13 14 15]\n [16 17 18]]\n\n [[19 20 21]\n [22 23 24]\n [25 26 27]]]\n" 140 | } 141 | ], 142 | "source": [ 143 | "# instantiating a 3-d array \n", 144 | "c= np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]],\n", 145 | " [[10, 11, 12], [13, 14, 15], [16, 17, 18]],\n", 146 | " [[19, 20, 21], [22, 23, 24], [25, 26, 27]]])\n", 147 | "\n", 148 | "print(c)" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 15, 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "output_type": "stream", 158 | "name": "stdout", 159 | "text": "[[1 2 3]\n [4 5 6]\n [7 8 9]]\n" 160 | } 161 | ], 162 | "source": [ 163 | "# selecting a row\n", 164 | "print(c[0])" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 18, 170 | "metadata": {}, 171 | "outputs": [ 172 | { 173 | "output_type": "stream", 174 | "name": "stdout", 175 | "text": "23\n" 176 | } 177 | ], 178 | "source": [ 179 | "# accessing single elements\n", 180 | "print(c[2, 1, 1])" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 19, 186 | "metadata": {}, 187 | "outputs": [ 188 | { 189 | "output_type": "stream", 190 | "name": "stdout", 191 | "text": "[1 2 3]\n" 192 | } 193 | ], 194 | "source": [ 195 | "# accessing matrix at index 0 and row 0 from that matrix\n", 196 | "print(c[0, 0])" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 20, 202 | "metadata": {}, 203 | "outputs": [ 204 | { 205 | "output_type": "stream", 206 | "name": "stdout", 207 | "text": "[12 15 18]\n" 208 | } 209 | ], 210 | "source": [ 211 | "# selecting a column from a 3d matrix\n", 212 | "print(c[1, :, 2])" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 21, 218 | "metadata": {}, 219 | "outputs": [ 220 | { 221 | "output_type": "stream", 222 | "name": "stdout", 223 | "text": "[ 7 16 25]\n" 224 | } 225 | ], 226 | "source": [ 227 | "# selecting the same row and column for each matrix\n", 228 | "print(c[:, 2, 0])" 229 | ] 230 | } 231 | ], 232 | "metadata": { 233 | "language_info": { 234 | "codemirror_mode": { 235 | "name": "ipython", 236 | "version": 3 237 | }, 238 | "file_extension": ".py", 239 | "mimetype": "text/x-python", 240 | "name": "python", 241 | "nbconvert_exporter": "python", 242 | "pygments_lexer": "ipython3", 243 | "version": "3.8.2-final" 244 | }, 245 | "orig_nbformat": 2, 246 | "kernelspec": { 247 | "name": "python38264bitconda17b1cc6b0e6f4854b7f0da0cf005d246", 248 | "display_name": "Python 3.8.2 64-bit (conda)" 249 | } 250 | }, 251 | "nbformat": 4, 252 | "nbformat_minor": 2 253 | } -------------------------------------------------------------------------------- /numpy_x_pandas/numpy_slicing.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np " 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "## Slicing List Data Types" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 2, 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "output_type": "stream", 26 | "name": "stdout", 27 | "text": "[1, 2, 3, 4, 5]\n" 28 | } 29 | ], 30 | "source": [ 31 | "# Creating a list \n", 32 | "a = [1, 2, 3, 4, 5]\n", 33 | "print(a)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 3, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "output_type": "stream", 43 | "name": "stdout", 44 | "text": "[2, 3, 4]\n" 45 | } 46 | ], 47 | "source": [ 48 | "# Slicing a list\n", 49 | "b = a[1:4]\n", 50 | "print(b)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 4, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "output_type": "stream", 60 | "name": "stdout", 61 | "text": "List c: [1, 2, 3]\nList d: [3, 4, 5]\nList e: [1, 2, 3, 4, 5]\n" 62 | } 63 | ], 64 | "source": [ 65 | "# Demonstrating slice notation\n", 66 | "c = a[:3]\n", 67 | "d = a[2:]\n", 68 | "e = a[:]\n", 69 | "print(f\"List c: {c}\\nList d: {d}\\nList e: {e}\")" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 6, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "output_type": "stream", 79 | "name": "stdout", 80 | "text": "f: 3\ng: [3]\n" 81 | } 82 | ], 83 | "source": [ 84 | "# indexing list vs slicing list\n", 85 | "f = a[2]\n", 86 | "g = a[2:3]\n", 87 | "print(f\"f: {f}\\ng: {g}\")" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "## Slicing 1D Arrays" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 10, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "output_type": "stream", 104 | "name": "stdout", 105 | "text": "[2 3 4]\n" 106 | } 107 | ], 108 | "source": [ 109 | "# slicing numpy array\n", 110 | "np_a = np.array([1, 2, 3, 4, 5])\n", 111 | "np_b = np_a[1:4]\n", 112 | "print(np_b)" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 11, 118 | "metadata": {}, 119 | "outputs": [ 120 | { 121 | "output_type": "stream", 122 | "name": "stdout", 123 | "text": "Original Array: [1 2 3 4 5]\nnp_b: [2 6 4]\nOriginal Array: [1 2 6 4 5]\n" 124 | } 125 | ], 126 | "source": [ 127 | "# numpy returns a view of the original data\n", 128 | "print(f\"Original Array: {np_a}\")\n", 129 | "np_b[1] = 6\n", 130 | "print(f\"np_b: {np_b}\\nOriginal Array: {np_a}\")" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "## Slicing 2D Arrays" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 12, 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "output_type": "stream", 147 | "name": "stdout", 148 | "text": "[[ 7 8]\n [12 13]\n [17 18]]\n" 149 | } 150 | ], 151 | "source": [ 152 | "# creating and slicing 2D array in both axes\n", 153 | "a_2d = np.array([[1, 2, 3, 4, 5],\n", 154 | " [6, 7, 8, 9, 10],\n", 155 | " [11, 12, 13, 14, 15],\n", 156 | " [16, 17, 18, 19, 20]])\n", 157 | "print(a_2d[1:, 1:3])" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "## Slicing 3D Arrays" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 13, 170 | "metadata": {}, 171 | "outputs": [ 172 | { 173 | "output_type": "stream", 174 | "name": "stdout", 175 | "text": "[[[13 14 15]\n [16 17 18]]\n\n [[22 23 24]\n [25 26 27]]]\n" 176 | } 177 | ], 178 | "source": [ 179 | "# creating and slicing 3D array 3 axes (Plane, Row, Column)\n", 180 | "a_3d = np.array([[[1, 2, 3,], [4, 5, 6], [7, 8, 9]],\n", 181 | " [[10, 11, 12], [13, 14, 15], [16, 17, 18]],\n", 182 | " [[19, 20, 21], [22, 23, 24], [25, 26, 27]]])\n", 183 | "print(a_3d[1:, 1:, :])" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 21, 189 | "metadata": {}, 190 | "outputs": [ 191 | { 192 | "output_type": "stream", 193 | "name": "stdout", 194 | "text": "2D Array [[ True True True True True]\n [ True True True True True]]\n[[[ True True True]\n [ True True True]]\n\n [[ True True True]\n [ True True True]]]\n[[[ True True True]\n [ True True True]\n [ True True True]]]\n" 195 | } 196 | ], 197 | "source": [ 198 | "# 2D Arrays\n", 199 | "print(a_2d[1:3, :] == a_2d[1:3])\n", 200 | "\n", 201 | "# 3D Arrays\n", 202 | "print(a_3d[1:, :2, :] == a_3d[1:, :2])\n", 203 | "print(a_3d[2:, :, :] == a_3d[2:, :])" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "## Slicing and Indexing" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 22, 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "output_type": "stream", 220 | "name": "stdout", 221 | "text": "[8 9]\n" 222 | } 223 | ], 224 | "source": [ 225 | "print(a_2d[1, 2:4])" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 23, 231 | "metadata": {}, 232 | "outputs": [ 233 | { 234 | "output_type": "stream", 235 | "name": "stdout", 236 | "text": "[[8 9]]\n" 237 | } 238 | ], 239 | "source": [ 240 | "print(a_2d[1:2, 2:4])" 241 | ] 242 | } 243 | ], 244 | "metadata": { 245 | "language_info": { 246 | "codemirror_mode": { 247 | "name": "ipython", 248 | "version": 3 249 | }, 250 | "file_extension": ".py", 251 | "mimetype": "text/x-python", 252 | "name": "python", 253 | "nbconvert_exporter": "python", 254 | "pygments_lexer": "ipython3", 255 | "version": "3.8.2-final" 256 | }, 257 | "orig_nbformat": 2, 258 | "kernelspec": { 259 | "name": "python38264bitconda17b1cc6b0e6f4854b7f0da0cf005d246", 260 | "display_name": "Python 3.8.2 64-bit (conda)" 261 | } 262 | }, 263 | "nbformat": 4, 264 | "nbformat_minor": 2 265 | } -------------------------------------------------------------------------------- /numpy_x_pandas/pandas_slicing_and_indexing.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import pandas as pd \n", 10 | "import numpy as np" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 3, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "output_type": "execute_result", 20 | "data": { 21 | "text/plain": " col_1 col_2 col_3 col_4 col_5\n0 0.322292 0.770114 0.988133 0.485027 0.072073\n1 0.739063 0.975319 0.700919 0.278852 0.873513\n2 0.143394 0.821405 0.900299 0.321988 0.415693\n3 0.786752 0.563219 0.776299 0.651507 0.934423\n4 0.581952 0.441311 0.022910 0.424256 0.279161", 22 | "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
col_1col_2col_3col_4col_5
00.3222920.7701140.9881330.4850270.072073
10.7390630.9753190.7009190.2788520.873513
20.1433940.8214050.9002990.3219880.415693
30.7867520.5632190.7762990.6515070.934423
40.5819520.4413110.0229100.4242560.279161
\n
" 23 | }, 24 | "metadata": {}, 25 | "execution_count": 3 26 | } 27 | ], 28 | "source": [ 29 | "# creating a dataframe\n", 30 | "df = pd.DataFrame(np.random.rand(5, 5),\n", 31 | " columns=[\"col_1\", \"col_2\", \"col_3\", \"col_4\", \"col_5\"])\n", 32 | "\n", 33 | "df" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 11, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "output_type": "execute_result", 43 | "data": { 44 | "text/plain": "0 0.32\n1 0.74\n2 0.14\n3 0.79\n4 0.58\nName: col_1, dtype: float64" 45 | }, 46 | "metadata": {}, 47 | "execution_count": 11 48 | } 49 | ], 50 | "source": [ 51 | "# access all elements in \"col_1\"\n", 52 | "df[\"col_1\"]" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 6, 58 | "metadata": {}, 59 | "outputs": [ 60 | { 61 | "output_type": "execute_result", 62 | "data": { 63 | "text/plain": "0.7390631428753759" 64 | }, 65 | "metadata": {}, 66 | "execution_count": 6 67 | } 68 | ], 69 | "source": [ 70 | "a= df[\"col_1\"]\n", 71 | "\n", 72 | "a[1]" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 7, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "output_type": "execute_result", 82 | "data": { 83 | "text/plain": " col_2 col_3 col_4\n0 0.770114 0.988133 0.485027\n1 0.975319 0.700919 0.278852\n2 0.821405 0.900299 0.321988\n3 0.563219 0.776299 0.651507\n4 0.441311 0.022910 0.424256", 84 | "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
col_2col_3col_4
00.7701140.9881330.485027
10.9753190.7009190.278852
20.8214050.9002990.321988
30.5632190.7762990.651507
40.4413110.0229100.424256
\n
" 85 | }, 86 | "metadata": {}, 87 | "execution_count": 7 88 | } 89 | ], 90 | "source": [ 91 | "df[[\"col_2\", \"col_3\", \"col_4\"]]" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "## Selection by label" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 17, 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "output_type": "execute_result", 108 | "data": { 109 | "text/plain": " A B C D\na 0.952954 -1.310324 -1.376740 0.276258\nb 1.493595 0.539705 0.969513 -0.617174\nc 0.611367 0.488150 1.022522 0.229638\nd -0.650225 1.571667 -1.204090 0.637101\ne 0.560311 -0.649682 -0.768907 0.977468\nf -1.647243 -1.217747 0.783886 0.401624\ng 0.970983 -1.508581 -0.842256 -0.659852\nh 1.271112 2.321445 0.033766 0.984220\ni 0.126584 -0.203787 1.253717 0.947080\nj -1.679669 -0.479596 2.071345 1.182526", 110 | "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ABCD
a0.952954-1.310324-1.3767400.276258
b1.4935950.5397050.969513-0.617174
c0.6113670.4881501.0225220.229638
d-0.6502251.571667-1.2040900.637101
e0.560311-0.649682-0.7689070.977468
f-1.647243-1.2177470.7838860.401624
g0.970983-1.508581-0.842256-0.659852
h1.2711122.3214450.0337660.984220
i0.126584-0.2037871.2537170.947080
j-1.679669-0.4795962.0713451.182526
\n
" 111 | }, 112 | "metadata": {}, 113 | "execution_count": 17 114 | } 115 | ], 116 | "source": [ 117 | "new_df = pd.DataFrame(np.random.randn(10, 4), index=list(\"abcdefghij\"),\n", 118 | " columns=list(\"ABCD\"))\n", 119 | "new_df" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 19, 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "output_type": "execute_result", 129 | "data": { 130 | "text/plain": "A 0.952954\nB -1.310324\nC -1.376740\nD 0.276258\nName: a, dtype: float64" 131 | }, 132 | "metadata": {}, 133 | "execution_count": 19 134 | } 135 | ], 136 | "source": [ 137 | "# cross section using a label\n", 138 | "new_df.loc[\"a\"]" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 20, 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "output_type": "execute_result", 148 | "data": { 149 | "text/plain": " A B C D\na 0.952954 -1.310324 -1.376740 0.276258\nd -0.650225 1.571667 -1.204090 0.637101\ni 0.126584 -0.203787 1.253717 0.947080", 150 | "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ABCD
a0.952954-1.310324-1.3767400.276258
d-0.6502251.571667-1.2040900.637101
i0.126584-0.2037871.2537170.947080
\n
" 151 | }, 152 | "metadata": {}, 153 | "execution_count": 20 154 | } 155 | ], 156 | "source": [ 157 | "# a list or array of labels\n", 158 | "new_df.loc[[\"a\", \"d\", \"i\"]] # equivalent to new_df.loc[[\"a\", \"b\", \"c\"], :]" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 28, 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "output_type": "execute_result", 168 | "data": { 169 | "text/plain": " B C D\na -1.310324 -1.376740 0.276258\nb 0.539705 0.969513 -0.617174\nc 0.488150 1.022522 0.229638\nd 1.571667 -1.204090 0.637101", 170 | "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
BCD
a-1.310324-1.3767400.276258
b0.5397050.969513-0.617174
c0.4881501.0225220.229638
d1.571667-1.2040900.637101
\n
" 171 | }, 172 | "metadata": {}, 173 | "execution_count": 28 174 | } 175 | ], 176 | "source": [ 177 | "# slice with label\n", 178 | "new_df.loc[\"a\":\"d\", \"B\":]" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 23, 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "output_type": "execute_result", 188 | "data": { 189 | "text/plain": "A False\nB False\nC True\nD True\nName: j, dtype: bool" 190 | }, 191 | "metadata": {}, 192 | "execution_count": 23 193 | } 194 | ], 195 | "source": [ 196 | "# a boolean array\n", 197 | "new_df.loc[\"j\"] > 0.5" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": 26, 203 | "metadata": {}, 204 | "outputs": [ 205 | { 206 | "output_type": "execute_result", 207 | "data": { 208 | "text/plain": " C D\na -1.376740 0.276258\nb 0.969513 -0.617174\nc 1.022522 0.229638\nd -1.204090 0.637101\ne -0.768907 0.977468\nf 0.783886 0.401624\ng -0.842256 -0.659852\nh 0.033766 0.984220\ni 1.253717 0.947080\nj 2.071345 1.182526", 209 | "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
CD
a-1.3767400.276258
b0.969513-0.617174
c1.0225220.229638
d-1.2040900.637101
e-0.7689070.977468
f0.7838860.401624
g-0.842256-0.659852
h0.0337660.984220
i1.2537170.947080
j2.0713451.182526
\n
" 210 | }, 211 | "metadata": {}, 212 | "execution_count": 26 213 | } 214 | ], 215 | "source": [ 216 | "# a boolean array \n", 217 | "new_df.loc[:, new_df.loc[\"j\"] > 0.5]" 218 | ] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "## Selection by position" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": 30, 230 | "metadata": {}, 231 | "outputs": [ 232 | { 233 | "output_type": "execute_result", 234 | "data": { 235 | "text/plain": " A B C D\n0 0.952954 -1.310324 -1.376740 0.276258\n1 1.493595 0.539705 0.969513 -0.617174\n2 0.611367 0.488150 1.022522 0.229638\n3 -0.650225 1.571667 -1.204090 0.637101\n4 0.560311 -0.649682 -0.768907 0.977468\n5 -1.647243 -1.217747 0.783886 0.401624\n6 0.970983 -1.508581 -0.842256 -0.659852\n7 1.271112 2.321445 0.033766 0.984220\n8 0.126584 -0.203787 1.253717 0.947080\n9 -1.679669 -0.479596 2.071345 1.182526", 236 | "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ABCD
00.952954-1.310324-1.3767400.276258
11.4935950.5397050.969513-0.617174
20.6113670.4881501.0225220.229638
3-0.6502251.571667-1.2040900.637101
40.560311-0.649682-0.7689070.977468
5-1.647243-1.2177470.7838860.401624
60.970983-1.508581-0.842256-0.659852
71.2711122.3214450.0337660.984220
80.126584-0.2037871.2537170.947080
9-1.679669-0.4795962.0713451.182526
\n
" 237 | }, 238 | "metadata": {}, 239 | "execution_count": 30 240 | } 241 | ], 242 | "source": [ 243 | "# using same df but new index values \n", 244 | "new_df.index = list(range(0, 10))\n", 245 | "new_df" 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": 31, 251 | "metadata": {}, 252 | "outputs": [ 253 | { 254 | "output_type": "execute_result", 255 | "data": { 256 | "text/plain": "A -0.650225\nB 1.571667\nC -1.204090\nD 0.637101\nName: 3, dtype: float64" 257 | }, 258 | "metadata": {}, 259 | "execution_count": 31 260 | } 261 | ], 262 | "source": [ 263 | "# cross section using an integer position\n", 264 | "new_df.iloc[3]" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": 34, 270 | "metadata": {}, 271 | "outputs": [ 272 | { 273 | "output_type": "execute_result", 274 | "data": { 275 | "text/plain": " A B C D\n1 1.493595 0.539705 0.969513 -0.617174\n5 -1.647243 -1.217747 0.783886 0.401624\n2 0.611367 0.488150 1.022522 0.229638\n3 -0.650225 1.571667 -1.204090 0.637101", 276 | "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ABCD
11.4935950.5397050.969513-0.617174
5-1.647243-1.2177470.7838860.401624
20.6113670.4881501.0225220.229638
3-0.6502251.571667-1.2040900.637101
\n
" 277 | }, 278 | "metadata": {}, 279 | "execution_count": 34 280 | } 281 | ], 282 | "source": [ 283 | "# list or array of integers\n", 284 | "new_df.iloc[[1, 5, 2, 3]]" 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": 41, 290 | "metadata": {}, 291 | "outputs": [ 292 | { 293 | "output_type": "execute_result", 294 | "data": { 295 | "text/plain": " A B C\n1 1.493595 0.539705 0.969513\n2 0.611367 0.488150 1.022522\n3 -0.650225 1.571667 -1.204090\n4 0.560311 -0.649682 -0.768907", 296 | "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ABC
11.4935950.5397050.969513
20.6113670.4881501.022522
3-0.6502251.571667-1.204090
40.560311-0.649682-0.768907
\n
" 297 | }, 298 | "metadata": {}, 299 | "execution_count": 41 300 | } 301 | ], 302 | "source": [ 303 | "# list or array of integers \n", 304 | "new_df.iloc[[1, 2, 3, 4], [0, 1, 2]]" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": 42, 310 | "metadata": {}, 311 | "outputs": [ 312 | { 313 | "output_type": "execute_result", 314 | "data": { 315 | "text/plain": " A B C D\n0 0.952954 -1.310324 -1.376740 0.276258\n1 1.493595 0.539705 0.969513 -0.617174\n2 0.611367 0.488150 1.022522 0.229638", 316 | "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ABCD
00.952954-1.310324-1.3767400.276258
11.4935950.5397050.969513-0.617174
20.6113670.4881501.0225220.229638
\n
" 317 | }, 318 | "metadata": {}, 319 | "execution_count": 42 320 | } 321 | ], 322 | "source": [ 323 | "# integer slicing\n", 324 | "new_df.iloc[:3]" 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": 43, 330 | "metadata": {}, 331 | "outputs": [ 332 | { 333 | "output_type": "execute_result", 334 | "data": { 335 | "text/plain": " A B C\n0 0.952954 -1.310324 -1.376740\n1 1.493595 0.539705 0.969513\n2 0.611367 0.488150 1.022522\n3 -0.650225 1.571667 -1.204090\n4 0.560311 -0.649682 -0.768907\n5 -1.647243 -1.217747 0.783886\n6 0.970983 -1.508581 -0.842256\n7 1.271112 2.321445 0.033766\n8 0.126584 -0.203787 1.253717\n9 -1.679669 -0.479596 2.071345", 336 | "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ABC
00.952954-1.310324-1.376740
11.4935950.5397050.969513
20.6113670.4881501.022522
3-0.6502251.571667-1.204090
40.560311-0.649682-0.768907
5-1.647243-1.2177470.783886
60.970983-1.508581-0.842256
71.2711122.3214450.033766
80.126584-0.2037871.253717
9-1.679669-0.4795962.071345
\n
" 337 | }, 338 | "metadata": {}, 339 | "execution_count": 43 340 | } 341 | ], 342 | "source": [ 343 | "# slice obejct by column \n", 344 | "new_df.iloc[:, :3]" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": 44, 350 | "metadata": {}, 351 | "outputs": [ 352 | { 353 | "output_type": "execute_result", 354 | "data": { 355 | "text/plain": " A B C D\n1 True True True False\n2 True False True False\n3 False True False True\n4 True False False True\n5 False False True False\n6 True False False False\n7 True True False True\n8 False False True True\n9 False False True True", 356 | "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ABCD
1TrueTrueTrueFalse
2TrueFalseTrueFalse
3FalseTrueFalseTrue
4TrueFalseFalseTrue
5FalseFalseTrueFalse
6TrueFalseFalseFalse
7TrueTrueFalseTrue
8FalseFalseTrueTrue
9FalseFalseTrueTrue
\n
" 357 | }, 358 | "metadata": {}, 359 | "execution_count": 44 360 | } 361 | ], 362 | "source": [ 363 | "# a boolean array\n", 364 | "new_df.iloc[1:] > 0.5" 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": 48, 370 | "metadata": {}, 371 | "outputs": [ 372 | { 373 | "output_type": "execute_result", 374 | "data": { 375 | "text/plain": "Empty DataFrame\nColumns: [A, B, C, D]\nIndex: []", 376 | "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n
ABCD
\n
" 377 | }, 378 | "metadata": {}, 379 | "execution_count": 48 380 | } 381 | ], 382 | "source": [ 383 | "# out of bounds\n", 384 | "new_df.iloc[10:13]" 385 | ] 386 | }, 387 | { 388 | "cell_type": "markdown", 389 | "metadata": {}, 390 | "source": [ 391 | "## Selecting by a callable" 392 | ] 393 | }, 394 | { 395 | "cell_type": "code", 396 | "execution_count": 50, 397 | "metadata": {}, 398 | "outputs": [ 399 | { 400 | "output_type": "execute_result", 401 | "data": { 402 | "text/plain": " A B C D\n0 0.952954 -1.310324 -1.376740 0.276258\n1 1.493595 0.539705 0.969513 -0.617174\n2 0.611367 0.488150 1.022522 0.229638\n3 -0.650225 1.571667 -1.204090 0.637101\n4 0.560311 -0.649682 -0.768907 0.977468\n5 -1.647243 -1.217747 0.783886 0.401624\n6 0.970983 -1.508581 -0.842256 -0.659852\n7 1.271112 2.321445 0.033766 0.984220\n8 0.126584 -0.203787 1.253717 0.947080\n9 -1.679669 -0.479596 2.071345 1.182526", 403 | "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ABCD
00.952954-1.310324-1.3767400.276258
11.4935950.5397050.969513-0.617174
20.6113670.4881501.0225220.229638
3-0.6502251.571667-1.2040900.637101
40.560311-0.649682-0.7689070.977468
5-1.647243-1.2177470.7838860.401624
60.970983-1.508581-0.842256-0.659852
71.2711122.3214450.0337660.984220
80.126584-0.2037871.2537170.947080
9-1.679669-0.4795962.0713451.182526
\n
" 404 | }, 405 | "metadata": {}, 406 | "execution_count": 50 407 | } 408 | ], 409 | "source": [ 410 | "new_df" 411 | ] 412 | }, 413 | { 414 | "cell_type": "code", 415 | "execution_count": 51, 416 | "metadata": {}, 417 | "outputs": [ 418 | { 419 | "output_type": "execute_result", 420 | "data": { 421 | "text/plain": "0 0.276258\n1 -0.617174\n2 0.229638\n3 0.637101\n4 0.977468\n5 0.401624\n6 -0.659852\n7 0.984220\n8 0.947080\n9 1.182526\nName: D, dtype: float64" 422 | }, 423 | "metadata": {}, 424 | "execution_count": 51 425 | } 426 | ], 427 | "source": [ 428 | "# selection by a callable using []\n", 429 | "new_df[lambda df: df.columns[3]]" 430 | ] 431 | }, 432 | { 433 | "cell_type": "code", 434 | "execution_count": 185, 435 | "metadata": {}, 436 | "outputs": [ 437 | { 438 | "output_type": "execute_result", 439 | "data": { 440 | "text/plain": " A B\n0 0.952954 -1.310324\n1 1.493595 0.539705\n2 0.611367 0.488150\n3 -0.650225 1.571667\n4 0.560311 -0.649682\n5 -1.647243 -1.217747\n6 0.970983 -1.508581\n7 1.271112 2.321445\n8 0.126584 -0.203787\n9 -1.679669 -0.479596", 441 | "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
AB
00.952954-1.310324
11.4935950.539705
20.6113670.488150
3-0.6502251.571667
40.560311-0.649682
5-1.647243-1.217747
60.970983-1.508581
71.2711122.321445
80.126584-0.203787
9-1.679669-0.479596
\n
" 442 | }, 443 | "metadata": {}, 444 | "execution_count": 185 445 | } 446 | ], 447 | "source": [ 448 | "# selection by df using .loc\n", 449 | "new_df.loc[:, lambda df: [\"A\", \"B\"]]" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 54, 455 | "metadata": {}, 456 | "outputs": [ 457 | { 458 | "output_type": "execute_result", 459 | "data": { 460 | "text/plain": " A B\n0 0.952954 -1.310324\n1 1.493595 0.539705\n2 0.611367 0.488150\n3 -0.650225 1.571667\n4 0.560311 -0.649682\n5 -1.647243 -1.217747\n6 0.970983 -1.508581\n7 1.271112 2.321445\n8 0.126584 -0.203787\n9 -1.679669 -0.479596", 461 | "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
AB
00.952954-1.310324
11.4935950.539705
20.6113670.488150
3-0.6502251.571667
40.560311-0.649682
5-1.647243-1.217747
60.970983-1.508581
71.2711122.321445
80.126584-0.203787
9-1.679669-0.479596
\n
" 462 | }, 463 | "metadata": {}, 464 | "execution_count": 54 465 | } 466 | ], 467 | "source": [ 468 | "# selection by df using .iloc\n", 469 | "new_df.iloc[:, lambda df: [0, 1]]" 470 | ] 471 | } 472 | ], 473 | "metadata": { 474 | "language_info": { 475 | "codemirror_mode": { 476 | "name": "ipython", 477 | "version": 3 478 | }, 479 | "file_extension": ".py", 480 | "mimetype": "text/x-python", 481 | "name": "python", 482 | "nbconvert_exporter": "python", 483 | "pygments_lexer": "ipython3", 484 | "version": "3.8.2-final" 485 | }, 486 | "orig_nbformat": 2, 487 | "kernelspec": { 488 | "name": "python38264bitconda17b1cc6b0e6f4854b7f0da0cf005d246", 489 | "display_name": "Python 3.8.2 64-bit (conda)" 490 | } 491 | }, 492 | "nbformat": 4, 493 | "nbformat_minor": 2 494 | } -------------------------------------------------------------------------------- /numpy_x_pandas/vectorization.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 14, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "import time" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "## Outer Product" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 31, 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "output_type": "stream", 27 | "name": "stdout", 28 | "text": "python_outer_product = [[0.0000000e+00 0.0000000e+00 0.0000000e+00 ... 0.0000000e+00\n 0.0000000e+00 0.0000000e+00]\n [0.0000000e+00 1.0000000e+00 2.0000000e+00 ... 9.9970000e+03\n 9.9980000e+03 9.9990000e+03]\n [0.0000000e+00 2.0000000e+00 4.0000000e+00 ... 1.9994000e+04\n 1.9996000e+04 1.9998000e+04]\n ...\n [0.0000000e+00 9.9970000e+03 1.9994000e+04 ... 9.9940009e+07\n 9.9950006e+07 9.9960003e+07]\n [0.0000000e+00 9.9980000e+03 1.9996000e+04 ... 9.9950006e+07\n 9.9960004e+07 9.9970002e+07]\n [0.0000000e+00 9.9990000e+03 1.9998000e+04 ... 9.9960003e+07\n 9.9970002e+07 9.9980001e+07]]\nTime = 159921.875ms\n\nnumpy_outer_product = [[ 0 0 0 ... 0 0 0]\n [ 0 1 2 ... 9997 9998 9999]\n [ 0 2 4 ... 19994 19996 19998]\n ...\n [ 0 9997 19994 ... 99940009 99950006 99960003]\n [ 0 9998 19996 ... 99950006 99960004 99970002]\n [ 0 9999 19998 ... 99960003 99970002 99980001]]\nTime = 328.125ms\n" 29 | } 30 | ], 31 | "source": [ 32 | "a = np.arange(10000)\n", 33 | "b = np.arange(10000) \n", 34 | " \n", 35 | "# pure Python outer product implementation \n", 36 | "tic = time.process_time() \n", 37 | "outer_product = np.zeros((10000, 10000)) \n", 38 | " \n", 39 | "for i in range(len(a)): \n", 40 | " for j in range(len(b)): \n", 41 | " outer_product[i][j]= a[i] * b[j] \n", 42 | " \n", 43 | "toc = time.process_time() \n", 44 | " \n", 45 | "print(\"python_outer_product = \"+ str(outer_product)); \n", 46 | "print(\"Time = \"+str(1000*(toc - tic ))+\"ms\\n\") \n", 47 | "\n", 48 | "# Numpy implementation\n", 49 | "n_tic = time.process_time() \n", 50 | "outer_product = np.outer(a, b) \n", 51 | "n_toc = time.process_time() \n", 52 | " \n", 53 | "print(\"numpy_outer_product = \"+str(outer_product)); \n", 54 | "print(\"Time = \"+str(1000*(n_toc - n_tic ))+\"ms\")" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "## Dot Product" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 34, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "output_type": "stream", 71 | "name": "stdout", 72 | "text": "python_dot_product = -1039031360\nTime = 53125.0ms\n\nnumpy_dot_product = -1039031360\nTime = 15.625ms\n" 73 | } 74 | ], 75 | "source": [ 76 | "a = np.arange(10000000)\n", 77 | "b = np.arange(10000000)\n", 78 | "\n", 79 | "# pure Python outer product implementation \n", 80 | "tic = time.process_time() \n", 81 | "\n", 82 | "dot_product = 0\n", 83 | "for i in range(len(a)): \n", 84 | " dot_product += a[i] * b[i]\n", 85 | "\n", 86 | "toc = time.process_time() \n", 87 | " \n", 88 | "print(\"python_dot_product = \"+ str(dot_product)); \n", 89 | "print(\"Time = \"+str(1000*(toc - tic ))+\"ms\\n\") \n", 90 | "\n", 91 | "# Numpy implementation\n", 92 | "n_tic = time.process_time() \n", 93 | "dot_product = np.dot(a, b) \n", 94 | "n_toc = time.process_time() \n", 95 | " \n", 96 | "print(\"numpy_dot_product = \"+str(dot_product)); \n", 97 | "print(\"Time = \"+str(1000*(n_toc - n_tic ))+\"ms\")" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": null, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [] 106 | } 107 | ], 108 | "metadata": { 109 | "language_info": { 110 | "codemirror_mode": { 111 | "name": "ipython", 112 | "version": 3 113 | }, 114 | "file_extension": ".py", 115 | "mimetype": "text/x-python", 116 | "name": "python", 117 | "nbconvert_exporter": "python", 118 | "pygments_lexer": "ipython3", 119 | "version": "3.8.2-final" 120 | }, 121 | "orig_nbformat": 2, 122 | "kernelspec": { 123 | "name": "python38264bitconda17b1cc6b0e6f4854b7f0da0cf005d246", 124 | "display_name": "Python 3.8.2 64-bit (conda)" 125 | } 126 | }, 127 | "nbformat": 4, 128 | "nbformat_minor": 2 129 | } -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | jupyter>=1.0.0, <1.1.0 2 | numpy>=1.21.2, <1.22.0 3 | pandas>=1.3.3, <1.4.0 --------------------------------------------------------------------------------