├── lecture_2 ├── .foo ├── dataframe.jpg ├── recent-grads.csv └── numpy.ipynb ├── lecture_3 ├── .foo ├── fastquad.f90 └── efficient_inventory_dynamics.ipynb ├── lecture_4 ├── .foo └── lininterp.py ├── apt.txt ├── lecture_1 ├── newfile.txt ├── foo.txt ├── lecture1.pdf ├── us_cities.txt ├── python_foundations.ipynb ├── .ipynb_checkpoints │ ├── python_foundations-checkpoint.ipynb │ └── python_essentials-checkpoint.ipynb └── python_essentials.ipynb ├── .gitignore ├── environment.yml └── README.md /lecture_2/.foo: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lecture_3/.foo: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lecture_4/.foo: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /apt.txt: -------------------------------------------------------------------------------- 1 | gfortran 2 | htop 3 | -------------------------------------------------------------------------------- /lecture_1/newfile.txt: -------------------------------------------------------------------------------- 1 | Testing 2 | Testing again -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | __pycache__/ 3 | */.ipynb_checkpoints/* 4 | 5 | -------------------------------------------------------------------------------- /lecture_1/foo.txt: -------------------------------------------------------------------------------- 1 | the rain 2 | in spain 3 | falls mainly 4 | on the plain -------------------------------------------------------------------------------- /lecture_1/lecture1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/QuantEcon/Copenhagen_workshop_2018/master/lecture_1/lecture1.pdf -------------------------------------------------------------------------------- /lecture_2/dataframe.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/QuantEcon/Copenhagen_workshop_2018/master/lecture_2/dataframe.jpg -------------------------------------------------------------------------------- /environment.yml: -------------------------------------------------------------------------------- 1 | channels: 2 | - conda-forge 3 | dependencies: 4 | - anaconda 5 | - numba=0.38 6 | - quantecon 7 | - scikit-learn 8 | - seaborn 9 | - bokeh -------------------------------------------------------------------------------- /lecture_1/us_cities.txt: -------------------------------------------------------------------------------- 1 | new york: 8244910 2 | los angeles: 3819702 3 | chicago: 2707120 4 | houston: 2145146 5 | philadelphia: 1536471 6 | phoenix: 1469471 7 | san antonio: 1359758 8 | san diego: 1326179 9 | dallas: 1223229 -------------------------------------------------------------------------------- /lecture_3/fastquad.f90: -------------------------------------------------------------------------------- 1 | 2 | PURE FUNCTION QUAD(X0, N) 3 | IMPLICIT NONE 4 | INTEGER, PARAMETER :: DP=KIND(0.d0) 5 | REAL(dp), INTENT(IN) :: X0 6 | REAL(dp) :: QUAD 7 | INTEGER :: I 8 | INTEGER, INTENT(IN) :: N 9 | QUAD = X0 10 | DO I = 1, N - 1 11 | QUAD = 4.0_dp * QUAD * real(1.0_dp - QUAD, dp) 12 | END DO 13 | RETURN 14 | END FUNCTION QUAD 15 | 16 | PROGRAM MAIN 17 | IMPLICIT NONE 18 | INTEGER, PARAMETER :: DP=KIND(0.d0) 19 | REAL(dp) :: START, FINISH, X, QUAD 20 | INTEGER :: N 21 | N = 10000000 22 | X = QUAD(0.2_dp, 10) 23 | CALL CPU_TIME(START) 24 | X = QUAD(0.2_dp, N) 25 | CALL CPU_TIME(FINISH) 26 | PRINT *,'last val = ', X 27 | PRINT *,'elapsed time = ', FINISH-START 28 | END PROGRAM MAIN -------------------------------------------------------------------------------- /lecture_4/lininterp.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from numba import jit 3 | 4 | @jit(nopython=True) 5 | def interp1d(grid, vals, x): 6 | """ 7 | Linearly interpolate (grid, vals) to evaluate at x. 8 | 9 | Parameters 10 | ---------- 11 | grid and vals are numpy arrays, x is a float 12 | 13 | Returns 14 | ------- 15 | a float, the interpolated value 16 | 17 | """ 18 | 19 | a, b, G = np.min(grid), np.max(grid), len(grid) 20 | 21 | s = (x - a) / (b - a) 22 | 23 | q_0 = max(min(int(s * (G - 1)), (G - 2)), 0) 24 | v_0 = vals[q_0] 25 | v_1 = vals[q_0 + 1] 26 | 27 | λ = s * (G - 1) - q_0 28 | 29 | return (1 - λ) * v_0 + λ * v_1 30 | 31 | 32 | @jit(nopython=True) 33 | def interp1d_vectorized(grid, vals, x_vec): 34 | """ 35 | Linearly interpolate (grid, vals) to evaluate at x_vec. 36 | 37 | All inputs are numpy arrays. 38 | 39 | Return value is a numpy array of length len(x_vec). 40 | """ 41 | 42 | out = np.empty_like(x_vec) 43 | 44 | for i, x in enumerate(x_vec): 45 | out[i] = interp1d(grid, vals, x) 46 | 47 | return out 48 | 49 | 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | Copenhagen 2018 Summer School QuantEcon Workshop 3 | -------------------------------------------------- 4 | 5 | Instructors: John Stachurski and Natasha Watkins 6 | 7 | Homepage: https://quantecon.org/copenhagen-summer-school-2018 8 | 9 | [![Binder](https://mybinder.org/badge.svg)](https://mybinder.org/v2/gh/QuantEcon/Copenhagen_workshop_2018/master) 10 | 11 | 12 | **Schedule** 13 | 14 | * 9:00 - 10:30: Introduction to QuantEcon, Jupyter notebooks, and Python 15 | * 10:45 - 12:15: Python for data handling, analysis, and visualisation, numerical techniques 16 | * 12:15 - 13:15: Lunch 17 | * 13:15 - 14:45: Intro to Numba 18 | * 15:00 - 16:30: Dynamic programming applications with Numba and parallelization 19 | * 16:45 - 18:15: Computer lab/office hours 20 | 21 | 22 | 23 | 24 | ### Links: 25 | 26 | * [Anaconda](https://www.anaconda.com/) 27 | * [AWS](https://aws.amazon.com/) 28 | * [Accessing AWS via SSH](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html) 29 | 30 | 31 | ### Notes on AWS 32 | 33 | 34 | #### To get an instance running 35 | 36 | 1. Login to Amazon AWS Console 37 | 2. Navigate to EC2 Service 38 | 3. Choose your region for setting up an instance 39 | 6. Create security key-pair for the region if you don't have one 40 | 4. Launch & Configure an instance and choose Ubuntu 64-bit 41 | 5. enable access through Port 8000 (in addition to Port 22 for ssh) 42 | 6. Choose security key you've set up 43 | 44 | #### Connecting and set up 45 | 46 | Use `ssh -i /path/to/pem-key ubuntu@hostname` 47 | 48 | Here `hostname` is your Public DNS, as shown in the instance information from AWS console 49 | 50 | Now run `sudo apt-get update` so you can install things you might need using `apt-get` 51 | 52 | 53 | #### Configure instance to run Jupyter 54 | 55 | 1. ssh into the running instance using IP from AWS Console 56 | 2. Install Anaconda using wget and the latest download link for python36 57 | 3. Run: jupyter notebook --generate-config 58 | 4. For Automatic Password Setup run: jupyter notebook password 59 | 5. Edit .jupyter/jupyter_notebook_config.py and set the following 60 | 61 | ``` 62 | # Set ip to '*' to bind on all interfaces (ips) for the public server 63 | c.NotebookApp.ip = '*' 64 | c.NotebookApp.open_browser = False 65 | c.NotebookApp.port = 8000 66 | ``` 67 | 68 | 69 | -------------------------------------------------------------------------------- /lecture_3/efficient_inventory_dynamics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Efficient Simulation in the Inventory Model\n", 8 | "\n", 9 | "\n", 10 | "#### John Stachurski" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "Simulation can be time consuming, especially if we need to simulate many paths\n", 18 | "\n", 19 | "And it's often true that large sample sizes are necessary to get an accurate picture\n", 20 | "\n", 21 | "Let's look at speeding up our simulations using [Numba](https://numba.pydata.org/)" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "import numpy as np\n", 31 | "from numba import jit, njit, prange\n", 32 | "from quantecon.util import tic, toc" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "Parameters:" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 2, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "s, S = 10, 100\n", 49 | "p = 0.4\n", 50 | "initial_x = 50.0" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "### Simulating One Path" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "Let's look at simulating one path with minimal optimization.\n", 65 | "\n", 66 | "(There's at least some efficiency gain over pure Python because we'll use a vectorized function to generate the random variables.)" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 3, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "def sim_one_inventory_path(sim_length=10_000_000):\n", 76 | " \n", 77 | " dvals = np.random.geometric(p, size=sim_length-1) - 1\n", 78 | " X = np.empty(sim_length, dtype=np.int64)\n", 79 | " X[0] = initial_x\n", 80 | " \n", 81 | " for t, d in enumerate(dvals):\n", 82 | " x = X[t]\n", 83 | " if x <= s:\n", 84 | " X[t+1] = max(S - d, 0)\n", 85 | " else:\n", 86 | " X[t+1] = max(x - d, 0)\n", 87 | " \n", 88 | " return X" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "How fast does this run?" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 4, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "name": "stdout", 105 | "output_type": "stream", 106 | "text": [ 107 | "TOC: Elapsed: 0:00:8.78\n" 108 | ] 109 | }, 110 | { 111 | "data": { 112 | "text/plain": [ 113 | "8.788817882537842" 114 | ] 115 | }, 116 | "execution_count": 4, 117 | "metadata": {}, 118 | "output_type": "execute_result" 119 | } 120 | ], 121 | "source": [ 122 | "tic()\n", 123 | "X = sim_one_inventory_path()\n", 124 | "toc()" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "Now let's build a jitted version." 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 5, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "@jit(nopython=True)\n", 141 | "def update(x): \n", 142 | " d = np.random.geometric(p) - 1\n", 143 | " if x <= s:\n", 144 | " y = np.maximum(S - d, 0)\n", 145 | " else:\n", 146 | " y = np.maximum(x - d, 0)\n", 147 | " return y" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 15, 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "data": { 157 | "text/plain": [ 158 | "12" 159 | ] 160 | }, 161 | "execution_count": 15, 162 | "metadata": {}, 163 | "output_type": "execute_result" 164 | } 165 | ], 166 | "source": [ 167 | "update(20)" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 16, 173 | "metadata": {}, 174 | "outputs": [], 175 | "source": [ 176 | "@jit(nopython=True)\n", 177 | "def sim_one_path_jitted(sim_length=10_000_000):\n", 178 | " X = np.empty(sim_length, dtype=np.int64)\n", 179 | " X[0] = initial_x\n", 180 | " for t in range(sim_length-1):\n", 181 | " X[t+1] = update(X[t])" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": {}, 187 | "source": [ 188 | "This is **much** faster:" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 18, 194 | "metadata": { 195 | "scrolled": true 196 | }, 197 | "outputs": [ 198 | { 199 | "name": "stdout", 200 | "output_type": "stream", 201 | "text": [ 202 | "TOC: Elapsed: 0:00:0.44\n" 203 | ] 204 | }, 205 | { 206 | "data": { 207 | "text/plain": [ 208 | "0.4414072036743164" 209 | ] 210 | }, 211 | "execution_count": 18, 212 | "metadata": {}, 213 | "output_type": "execute_result" 214 | } 215 | ], 216 | "source": [ 217 | "tic()\n", 218 | "X = sim_one_path_jitted()\n", 219 | "toc()" 220 | ] 221 | }, 222 | { 223 | "cell_type": "markdown", 224 | "metadata": {}, 225 | "source": [ 226 | "### Task: Calculate Stationary Order Rate" 227 | ] 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "metadata": {}, 232 | "source": [ 233 | "As we've seen, the inventory model has a stationary distribution.\n", 234 | "\n", 235 | "Let's call it $\\psi^*$, with the corresponding random variable denoted $X^*$.\n", 236 | "\n", 237 | "For a large population with independent demand shocks,\n", 238 | "\n", 239 | "$$ \\mathbb P\\{X^* = k\\} \\simeq \\text{fraction of firms with inventory } k $$\n", 240 | "\n", 241 | "We are interested in calculating\n", 242 | "\n", 243 | "$$ \\mathbb P\\{X^* \\leq s\\} \\simeq \\text{fraction of firms currently placing orders } $$\n", 244 | "\n", 245 | "The law of large numbers for stationary and ergodic sequences tells us that\n", 246 | "\n", 247 | "$$ \\mathbb P\\{X^* \\leq s\\} \\simeq \\frac{1}{n} \\sum_{t=1}^n \\mathbb 1\\{X_t \\leq s\\} $$\n", 248 | "\n", 249 | "In other words, we can calculate the stationary probability $\\mathbb P\\{X^* \\leq s\\}$ by generating a single time series and calculating the fraction of time that it spends below $s$.\n", 250 | "\n", 251 | "We can do this fast with the jitted code above, thanks to Numba.\n", 252 | "\n", 253 | "#### Exploiting parallelization\n", 254 | "\n", 255 | "However, the above approach is difficult to parallelize because it's inherently sequential\n", 256 | "\n", 257 | "An approach that's easier to parallelize is to \n", 258 | "\n", 259 | "* pick some large value $T$\n", 260 | "\n", 261 | "* generate many independent observations $X^i_T$ of $X_T$\n", 262 | "\n", 263 | "* calculate the fraction of observations that are less than $s$\n", 264 | "\n", 265 | "This works because \n", 266 | "\n", 267 | "$$ \\mathbb P\\{X^* \\leq s\\} \\simeq \\mathbb P \\{X_T \\leq s\\} $$\n", 268 | "\n", 269 | "when $T$ is large (since, as previously discussed, $\\psi_t \\to \\psi^*$).\n", 270 | "\n", 271 | "Thus, letting $m$ be the number of paths we'll generate, our goal is to fix large $T$ and compute\n", 272 | "\n", 273 | "$$ p(m, T) = \\frac{1}{m} \\sum_{i=1}^m \\mathbb 1\\{X_T^i \\leq s\\} $$" 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "### Simulating Many Paths" 281 | ] 282 | }, 283 | { 284 | "cell_type": "markdown", 285 | "metadata": {}, 286 | "source": [ 287 | "Here's a jitted (and hence fast) piece of code for simulating many paths." 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 27, 293 | "metadata": {}, 294 | "outputs": [], 295 | "source": [ 296 | "@njit\n", 297 | "def sim_prob(m=500_000, T=1000):\n", 298 | " \n", 299 | " is_below_s = 0\n", 300 | " for i in range(m):\n", 301 | " x = initial_x\n", 302 | " for t in range(T):\n", 303 | " x = update(x)\n", 304 | " if x <= s:\n", 305 | " is_below_s += 1\n", 306 | " p_m_T = is_below_s / m\n", 307 | " return p_m_T\n" 308 | ] 309 | }, 310 | { 311 | "cell_type": "markdown", 312 | "metadata": {}, 313 | "source": [ 314 | "Let's get a reading on the time." 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 28, 320 | "metadata": {}, 321 | "outputs": [ 322 | { 323 | "name": "stdout", 324 | "output_type": "stream", 325 | "text": [ 326 | "TOC: Elapsed: 0:00:20.78\n" 327 | ] 328 | }, 329 | { 330 | "data": { 331 | "text/plain": [ 332 | "20.7879695892334" 333 | ] 334 | }, 335 | "execution_count": 28, 336 | "metadata": {}, 337 | "output_type": "execute_result" 338 | } 339 | ], 340 | "source": [ 341 | "tic()\n", 342 | "p = sim_prob()\n", 343 | "toc()" 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": 29, 349 | "metadata": {}, 350 | "outputs": [ 351 | { 352 | "data": { 353 | "text/plain": [ 354 | "0.016094" 355 | ] 356 | }, 357 | "execution_count": 29, 358 | "metadata": {}, 359 | "output_type": "execute_result" 360 | } 361 | ], 362 | "source": [ 363 | "p" 364 | ] 365 | }, 366 | { 367 | "cell_type": "markdown", 368 | "metadata": {}, 369 | "source": [ 370 | "Now let's try a simple parallelization:" 371 | ] 372 | }, 373 | { 374 | "cell_type": "code", 375 | "execution_count": 30, 376 | "metadata": {}, 377 | "outputs": [], 378 | "source": [ 379 | "@njit(parallel=True)\n", 380 | "def sim_prob_parallel(m=500_000, T=1000):\n", 381 | " \n", 382 | " is_below_s = 0\n", 383 | " for i in prange(m):\n", 384 | " x = initial_x\n", 385 | " for t in range(T):\n", 386 | " x = update(x)\n", 387 | " if x <= s:\n", 388 | " is_below_s += 1\n", 389 | " p_m_T = is_below_s / m\n", 390 | " return p_m_T\n" 391 | ] 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": 31, 396 | "metadata": {}, 397 | "outputs": [ 398 | { 399 | "name": "stdout", 400 | "output_type": "stream", 401 | "text": [ 402 | "TOC: Elapsed: 0:00:4.26\n" 403 | ] 404 | }, 405 | { 406 | "data": { 407 | "text/plain": [ 408 | "4.2602362632751465" 409 | ] 410 | }, 411 | "execution_count": 31, 412 | "metadata": {}, 413 | "output_type": "execute_result" 414 | } 415 | ], 416 | "source": [ 417 | "tic()\n", 418 | "p = sim_prob_parallel()\n", 419 | "toc()" 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": 32, 425 | "metadata": {}, 426 | "outputs": [ 427 | { 428 | "data": { 429 | "text/plain": [ 430 | "0.016428" 431 | ] 432 | }, 433 | "execution_count": 32, 434 | "metadata": {}, 435 | "output_type": "execute_result" 436 | } 437 | ], 438 | "source": [ 439 | "p" 440 | ] 441 | }, 442 | { 443 | "cell_type": "markdown", 444 | "metadata": {}, 445 | "source": [ 446 | "Let's try for a more efficient parallelization, where the work is divided into a smaller number of chunks." 447 | ] 448 | }, 449 | { 450 | "cell_type": "code", 451 | "execution_count": 33, 452 | "metadata": {}, 453 | "outputs": [], 454 | "source": [ 455 | "@njit(parallel=True)\n", 456 | "def sim_prob_parallel(m=500_000, T=1000, chunks=500):\n", 457 | " \n", 458 | " chunk_size = m // chunks\n", 459 | " p_m_T_vals = np.empty(chunks)\n", 460 | " \n", 461 | " for i in prange(chunks):\n", 462 | " p_m_T_vals[i] = sim_prob(m=chunk_size)\n", 463 | "\n", 464 | " return p_m_T_vals.mean()" 465 | ] 466 | }, 467 | { 468 | "cell_type": "markdown", 469 | "metadata": {}, 470 | "source": [ 471 | "Now the speed up from parallelization is significant." 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": 34, 477 | "metadata": {}, 478 | "outputs": [ 479 | { 480 | "name": "stdout", 481 | "output_type": "stream", 482 | "text": [ 483 | "TOC: Elapsed: 0:00:4.35\n" 484 | ] 485 | }, 486 | { 487 | "data": { 488 | "text/plain": [ 489 | "4.351317644119263" 490 | ] 491 | }, 492 | "execution_count": 34, 493 | "metadata": {}, 494 | "output_type": "execute_result" 495 | } 496 | ], 497 | "source": [ 498 | "tic()\n", 499 | "X = sim_prob_parallel()\n", 500 | "toc()" 501 | ] 502 | }, 503 | { 504 | "cell_type": "markdown", 505 | "metadata": {}, 506 | "source": [ 507 | "There's essentially no difference --- so `prange` is already optimizing the load across CPUs for us." 508 | ] 509 | }, 510 | { 511 | "cell_type": "code", 512 | "execution_count": null, 513 | "metadata": {}, 514 | "outputs": [], 515 | "source": [] 516 | } 517 | ], 518 | "metadata": { 519 | "kernelspec": { 520 | "display_name": "Python 3", 521 | "language": "python", 522 | "name": "python3" 523 | }, 524 | "language_info": { 525 | "codemirror_mode": { 526 | "name": "ipython", 527 | "version": 3 528 | }, 529 | "file_extension": ".py", 530 | "mimetype": "text/x-python", 531 | "name": "python", 532 | "nbconvert_exporter": "python", 533 | "pygments_lexer": "ipython3", 534 | "version": "3.6.4" 535 | } 536 | }, 537 | "nbformat": 4, 538 | "nbformat_minor": 2 539 | } 540 | -------------------------------------------------------------------------------- /lecture_1/python_foundations.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Python and OOP" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Procedural code = functions act on data\n", 15 | "\n", 16 | "Object oriented code = functions (called methods) come bundled with data" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "Python is a pragmatic mix of procedural / OO / functional styles" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "### Everything's an object" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": {}, 36 | "source": [ 37 | "It's often said that in Python, everything's an object\n", 38 | "\n", 39 | "So what's an object?\n", 40 | "\n", 41 | "An object is an entity in memory with the following features\n", 42 | "\n", 43 | "* type\n", 44 | "* an identity number\n", 45 | "* data\n", 46 | "* attributes, including methods" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "#### Type" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 1, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "data": { 63 | "text/plain": [ 64 | "str" 65 | ] 66 | }, 67 | "execution_count": 1, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "s = 'This is a string'\n", 74 | "type(s)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 2, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "data": { 84 | "text/plain": [ 85 | "int" 86 | ] 87 | }, 88 | "execution_count": 2, 89 | "metadata": {}, 90 | "output_type": "execute_result" 91 | } 92 | ], 93 | "source": [ 94 | "x = 42 # Now let's create an integer\n", 95 | "type(x)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "The Python interpreter queries type before it applies certain operators" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 3, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "data": { 112 | "text/plain": [ 113 | "'300cc'" 114 | ] 115 | }, 116 | "execution_count": 3, 117 | "metadata": {}, 118 | "output_type": "execute_result" 119 | } 120 | ], 121 | "source": [ 122 | "'300' + 'cc'" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "Here Python has used addition appropriate to type (in this case, concatenation)\n", 130 | "\n", 131 | "With ints we get regular integer addition" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 4, 137 | "metadata": {}, 138 | "outputs": [ 139 | { 140 | "data": { 141 | "text/plain": [ 142 | "700" 143 | ] 144 | }, 145 | "execution_count": 4, 146 | "metadata": {}, 147 | "output_type": "execute_result" 148 | } 149 | ], 150 | "source": [ 151 | "300 + 400" 152 | ] 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "metadata": {}, 157 | "source": [ 158 | "What happens here?" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 5, 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "ename": "TypeError", 168 | "evalue": "must be str, not int", 169 | "output_type": "error", 170 | "traceback": [ 171 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 172 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 173 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;34m'300'\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m400\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", 174 | "\u001b[0;31mTypeError\u001b[0m: must be str, not int" 175 | ] 176 | } 177 | ], 178 | "source": [ 179 | "\n", 180 | "'300' + 400\n", 181 | "\n" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 6, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "data": { 191 | "text/plain": [ 192 | "700" 193 | ] 194 | }, 195 | "execution_count": 6, 196 | "metadata": {}, 197 | "output_type": "execute_result" 198 | } 199 | ], 200 | "source": [ 201 | "int('300') + 400 " 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": {}, 207 | "source": [ 208 | "#### Identity" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 17, 214 | "metadata": {}, 215 | "outputs": [ 216 | { 217 | "data": { 218 | "text/plain": [ 219 | "140270281056928" 220 | ] 221 | }, 222 | "execution_count": 17, 223 | "metadata": {}, 224 | "output_type": "execute_result" 225 | } 226 | ], 227 | "source": [ 228 | "y = 2.5\n", 229 | "z = 2.5\n", 230 | "id(y)" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": 18, 236 | "metadata": {}, 237 | "outputs": [ 238 | { 239 | "data": { 240 | "text/plain": [ 241 | "140270281056688" 242 | ] 243 | }, 244 | "execution_count": 18, 245 | "metadata": {}, 246 | "output_type": "execute_result" 247 | } 248 | ], 249 | "source": [ 250 | "id(z)" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 19, 256 | "metadata": {}, 257 | "outputs": [], 258 | "source": [ 259 | "x, y = 10, 10" 260 | ] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "metadata": {}, 265 | "source": [ 266 | "#### Methods\n", 267 | "\n" 268 | ] 269 | }, 270 | { 271 | "cell_type": "markdown", 272 | "metadata": {}, 273 | "source": [ 274 | "Methods tied to objects, intending to be useful for the data they contain" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": 20, 280 | "metadata": {}, 281 | "outputs": [ 282 | { 283 | "data": { 284 | "text/plain": [ 285 | "['a', 'b', 'c']" 286 | ] 287 | }, 288 | "execution_count": 20, 289 | "metadata": {}, 290 | "output_type": "execute_result" 291 | } 292 | ], 293 | "source": [ 294 | "x = ['a', 'b']\n", 295 | "x.append('c')\n", 296 | "x" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 11, 302 | "metadata": {}, 303 | "outputs": [ 304 | { 305 | "data": { 306 | "text/plain": [ 307 | "'THIS IS A STRING'" 308 | ] 309 | }, 310 | "execution_count": 11, 311 | "metadata": {}, 312 | "output_type": "execute_result" 313 | } 314 | ], 315 | "source": [ 316 | "s = 'This is a string'\n", 317 | "s.upper()" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": 12, 323 | "metadata": {}, 324 | "outputs": [ 325 | { 326 | "data": { 327 | "text/plain": [ 328 | "'this is a string'" 329 | ] 330 | }, 331 | "execution_count": 12, 332 | "metadata": {}, 333 | "output_type": "execute_result" 334 | } 335 | ], 336 | "source": [ 337 | "s.lower()" 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": 13, 343 | "metadata": {}, 344 | "outputs": [ 345 | { 346 | "data": { 347 | "text/plain": [ 348 | "'That is a string'" 349 | ] 350 | }, 351 | "execution_count": 13, 352 | "metadata": {}, 353 | "output_type": "execute_result" 354 | } 355 | ], 356 | "source": [ 357 | "s.replace('This', 'That')" 358 | ] 359 | }, 360 | { 361 | "cell_type": "markdown", 362 | "metadata": {}, 363 | "source": [ 364 | "Even item assignment is really just a method:" 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": 14, 370 | "metadata": {}, 371 | "outputs": [ 372 | { 373 | "data": { 374 | "text/plain": [ 375 | "['aa', 'b']" 376 | ] 377 | }, 378 | "execution_count": 14, 379 | "metadata": {}, 380 | "output_type": "execute_result" 381 | } 382 | ], 383 | "source": [ 384 | "x = ['a', 'b']\n", 385 | "x[0] = 'aa' \n", 386 | "x" 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": 15, 392 | "metadata": {}, 393 | "outputs": [ 394 | { 395 | "data": { 396 | "text/plain": [ 397 | "['aa', 'b']" 398 | ] 399 | }, 400 | "execution_count": 15, 401 | "metadata": {}, 402 | "output_type": "execute_result" 403 | } 404 | ], 405 | "source": [ 406 | "x = ['a', 'b']\n", 407 | "x.__setitem__(0, 'aa') \n", 408 | "x" 409 | ] 410 | }, 411 | { 412 | "cell_type": "markdown", 413 | "metadata": {}, 414 | "source": [ 415 | "### Are you sure everything's an object?" 416 | ] 417 | }, 418 | { 419 | "cell_type": "markdown", 420 | "metadata": {}, 421 | "source": [ 422 | "Literally everything in memory is an object, from integers..." 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": 21, 428 | "metadata": {}, 429 | "outputs": [ 430 | { 431 | "data": { 432 | "text/plain": [ 433 | "42" 434 | ] 435 | }, 436 | "execution_count": 21, 437 | "metadata": {}, 438 | "output_type": "execute_result" 439 | } 440 | ], 441 | "source": [ 442 | "x = 42\n", 443 | "x" 444 | ] 445 | }, 446 | { 447 | "cell_type": "code", 448 | "execution_count": 22, 449 | "metadata": {}, 450 | "outputs": [ 451 | { 452 | "data": { 453 | "text/plain": [ 454 | "0" 455 | ] 456 | }, 457 | "execution_count": 22, 458 | "metadata": {}, 459 | "output_type": "execute_result" 460 | } 461 | ], 462 | "source": [ 463 | "x.imag" 464 | ] 465 | }, 466 | { 467 | "cell_type": "code", 468 | "execution_count": 23, 469 | "metadata": {}, 470 | "outputs": [ 471 | { 472 | "data": { 473 | "text/plain": [ 474 | "int" 475 | ] 476 | }, 477 | "execution_count": 23, 478 | "metadata": {}, 479 | "output_type": "execute_result" 480 | } 481 | ], 482 | "source": [ 483 | "x.__class__" 484 | ] 485 | }, 486 | { 487 | "cell_type": "markdown", 488 | "metadata": {}, 489 | "source": [ 490 | "...to functions..." 491 | ] 492 | }, 493 | { 494 | "cell_type": "code", 495 | "execution_count": 24, 496 | "metadata": {}, 497 | "outputs": [ 498 | { 499 | "data": { 500 | "text/plain": [ 501 | "" 502 | ] 503 | }, 504 | "execution_count": 24, 505 | "metadata": {}, 506 | "output_type": "execute_result" 507 | } 508 | ], 509 | "source": [ 510 | "def f(x): return x**2\n", 511 | "f" 512 | ] 513 | }, 514 | { 515 | "cell_type": "code", 516 | "execution_count": 25, 517 | "metadata": {}, 518 | "outputs": [ 519 | { 520 | "data": { 521 | "text/plain": [ 522 | "function" 523 | ] 524 | }, 525 | "execution_count": 25, 526 | "metadata": {}, 527 | "output_type": "execute_result" 528 | } 529 | ], 530 | "source": [ 531 | "type(f)" 532 | ] 533 | }, 534 | { 535 | "cell_type": "code", 536 | "execution_count": 26, 537 | "metadata": {}, 538 | "outputs": [ 539 | { 540 | "data": { 541 | "text/plain": [ 542 | "140270280313712" 543 | ] 544 | }, 545 | "execution_count": 26, 546 | "metadata": {}, 547 | "output_type": "execute_result" 548 | } 549 | ], 550 | "source": [ 551 | "id(f)" 552 | ] 553 | }, 554 | { 555 | "cell_type": "code", 556 | "execution_count": 27, 557 | "metadata": {}, 558 | "outputs": [ 559 | { 560 | "data": { 561 | "text/plain": [ 562 | "'f'" 563 | ] 564 | }, 565 | "execution_count": 27, 566 | "metadata": {}, 567 | "output_type": "execute_result" 568 | } 569 | ], 570 | "source": [ 571 | "f.__name__" 572 | ] 573 | }, 574 | { 575 | "cell_type": "code", 576 | "execution_count": 28, 577 | "metadata": {}, 578 | "outputs": [ 579 | { 580 | "data": { 581 | "text/plain": [ 582 | "9" 583 | ] 584 | }, 585 | "execution_count": 28, 586 | "metadata": {}, 587 | "output_type": "execute_result" 588 | } 589 | ], 590 | "source": [ 591 | "f.__call__(3)" 592 | ] 593 | }, 594 | { 595 | "cell_type": "markdown", 596 | "metadata": {}, 597 | "source": [ 598 | "...to modules." 599 | ] 600 | }, 601 | { 602 | "cell_type": "code", 603 | "execution_count": 29, 604 | "metadata": {}, 605 | "outputs": [ 606 | { 607 | "data": { 608 | "text/plain": [ 609 | "140270472209576" 610 | ] 611 | }, 612 | "execution_count": 29, 613 | "metadata": {}, 614 | "output_type": "execute_result" 615 | } 616 | ], 617 | "source": [ 618 | "import math\n", 619 | "\n", 620 | "id(math)" 621 | ] 622 | }, 623 | { 624 | "cell_type": "code", 625 | "execution_count": 30, 626 | "metadata": {}, 627 | "outputs": [ 628 | { 629 | "data": { 630 | "text/plain": [ 631 | "module" 632 | ] 633 | }, 634 | "execution_count": 30, 635 | "metadata": {}, 636 | "output_type": "execute_result" 637 | } 638 | ], 639 | "source": [ 640 | "type(math)" 641 | ] 642 | }, 643 | { 644 | "cell_type": "markdown", 645 | "metadata": {}, 646 | "source": [ 647 | "### Classes: Building our own objects" 648 | ] 649 | }, 650 | { 651 | "cell_type": "markdown", 652 | "metadata": {}, 653 | "source": [ 654 | "Classes are how we build our own custom objects.\n", 655 | "\n", 656 | "Classes are the blueprint and objects are then generated (instantiated) from this blueprint.\n", 657 | "\n", 658 | "Here's an example:" 659 | ] 660 | }, 661 | { 662 | "cell_type": "code", 663 | "execution_count": 52, 664 | "metadata": {}, 665 | "outputs": [], 666 | "source": [ 667 | "class Firm:\n", 668 | " \"\"\"\n", 669 | " Stores the parameters of the production function f(k) = Ak^α,\n", 670 | " implements the function.\n", 671 | " \"\"\"\n", 672 | " \n", 673 | " def __init__(self, α=0.5, A=2.0):\n", 674 | " self.α = α\n", 675 | " self.A = A\n", 676 | " \n", 677 | " def f(self, k):\n", 678 | " return self.A * k**self.α\n", 679 | " \n", 680 | " " 681 | ] 682 | }, 683 | { 684 | "cell_type": "markdown", 685 | "metadata": {}, 686 | "source": [ 687 | "You can see there's some boilerplate, which you'll get used to over time\n", 688 | "\n", 689 | "* the `__init__` method is used to help build an instance\n", 690 | "* think of `self` as referring to data specific to an instance" 691 | ] 692 | }, 693 | { 694 | "cell_type": "markdown", 695 | "metadata": {}, 696 | "source": [ 697 | "Let's generate an instance:" 698 | ] 699 | }, 700 | { 701 | "cell_type": "code", 702 | "execution_count": 53, 703 | "metadata": {}, 704 | "outputs": [], 705 | "source": [ 706 | "firm = Firm()" 707 | ] 708 | }, 709 | { 710 | "cell_type": "code", 711 | "execution_count": 54, 712 | "metadata": {}, 713 | "outputs": [ 714 | { 715 | "data": { 716 | "text/plain": [ 717 | "0.5" 718 | ] 719 | }, 720 | "execution_count": 54, 721 | "metadata": {}, 722 | "output_type": "execute_result" 723 | } 724 | ], 725 | "source": [ 726 | "firm.α" 727 | ] 728 | }, 729 | { 730 | "cell_type": "code", 731 | "execution_count": 55, 732 | "metadata": {}, 733 | "outputs": [ 734 | { 735 | "data": { 736 | "text/plain": [ 737 | "6.324555320336759" 738 | ] 739 | }, 740 | "execution_count": 55, 741 | "metadata": {}, 742 | "output_type": "execute_result" 743 | } 744 | ], 745 | "source": [ 746 | "k = 10.0\n", 747 | "firm.f(k)" 748 | ] 749 | }, 750 | { 751 | "cell_type": "code", 752 | "execution_count": 56, 753 | "metadata": {}, 754 | "outputs": [], 755 | "source": [ 756 | "firm.A = 10.0" 757 | ] 758 | }, 759 | { 760 | "cell_type": "code", 761 | "execution_count": 57, 762 | "metadata": {}, 763 | "outputs": [ 764 | { 765 | "data": { 766 | "text/plain": [ 767 | "31.622776601683796" 768 | ] 769 | }, 770 | "execution_count": 57, 771 | "metadata": {}, 772 | "output_type": "execute_result" 773 | } 774 | ], 775 | "source": [ 776 | "firm.f(k)" 777 | ] 778 | }, 779 | { 780 | "cell_type": "markdown", 781 | "metadata": {}, 782 | "source": [ 783 | "#### Summary" 784 | ] 785 | }, 786 | { 787 | "cell_type": "markdown", 788 | "metadata": {}, 789 | "source": [ 790 | "When it first came out, the concept of OOP was overhyped and oversold.\n", 791 | "\n", 792 | "Some claimed the OOP methodology was ideal for solving every coding problem known to humankind.\n", 793 | "\n", 794 | "The hype lead to a backlash amongst developers, against OOP\n", 795 | "\n", 796 | "The backlash was a good thing **but** you should ignore extremists on both sides.\n", 797 | "\n", 798 | "Python's object model is elegant and widely admired.\n", 799 | "\n", 800 | "Also, classes are definitely useful --- I use one lightweight class in almost every script I write, to\n", 801 | "\n", 802 | "* store and organize parameters\n", 803 | "* test restrictions on parameters\n", 804 | "* implement other small tasks like generating and storing grids based on information contained in the parameters\n", 805 | "\n", 806 | "Just don't overuse them --- large classes and class hierarchies are a pain in the `#!@$`" 807 | ] 808 | }, 809 | { 810 | "cell_type": "code", 811 | "execution_count": null, 812 | "metadata": {}, 813 | "outputs": [], 814 | "source": [] 815 | } 816 | ], 817 | "metadata": { 818 | "kernelspec": { 819 | "display_name": "Python 3", 820 | "language": "python", 821 | "name": "python3" 822 | }, 823 | "language_info": { 824 | "codemirror_mode": { 825 | "name": "ipython", 826 | "version": 3 827 | }, 828 | "file_extension": ".py", 829 | "mimetype": "text/x-python", 830 | "name": "python", 831 | "nbconvert_exporter": "python", 832 | "pygments_lexer": "ipython3", 833 | "version": "3.6.4" 834 | } 835 | }, 836 | "nbformat": 4, 837 | "nbformat_minor": 2 838 | } 839 | -------------------------------------------------------------------------------- /lecture_1/.ipynb_checkpoints/python_foundations-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Python and OOP" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Procedural code = functions act on data\n", 15 | "\n", 16 | "Object oriented code = functions (called methods) come bundled with data" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "Python is a pragmatic mix of procedural / OO / functional styles" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "### Everything's an object" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": {}, 36 | "source": [ 37 | "It's often said that in Python, everything's an object\n", 38 | "\n", 39 | "So what's an object?\n", 40 | "\n", 41 | "An object is an entity in memory with the following features\n", 42 | "\n", 43 | "* type\n", 44 | "* an identity number\n", 45 | "* data\n", 46 | "* attributes, including methods" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "#### Type" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 1, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "data": { 63 | "text/plain": [ 64 | "str" 65 | ] 66 | }, 67 | "execution_count": 1, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "s = 'This is a string'\n", 74 | "type(s)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 2, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "data": { 84 | "text/plain": [ 85 | "int" 86 | ] 87 | }, 88 | "execution_count": 2, 89 | "metadata": {}, 90 | "output_type": "execute_result" 91 | } 92 | ], 93 | "source": [ 94 | "x = 42 # Now let's create an integer\n", 95 | "type(x)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "The Python interpreter queries type before it applies certain operators" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 3, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "data": { 112 | "text/plain": [ 113 | "'300cc'" 114 | ] 115 | }, 116 | "execution_count": 3, 117 | "metadata": {}, 118 | "output_type": "execute_result" 119 | } 120 | ], 121 | "source": [ 122 | "'300' + 'cc'" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "Here Python has used addition appropriate to type (in this case, concatenation)\n", 130 | "\n", 131 | "With ints we get regular integer addition" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 4, 137 | "metadata": {}, 138 | "outputs": [ 139 | { 140 | "data": { 141 | "text/plain": [ 142 | "700" 143 | ] 144 | }, 145 | "execution_count": 4, 146 | "metadata": {}, 147 | "output_type": "execute_result" 148 | } 149 | ], 150 | "source": [ 151 | "300 + 400" 152 | ] 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "metadata": {}, 157 | "source": [ 158 | "What happens here?" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 5, 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "ename": "TypeError", 168 | "evalue": "must be str, not int", 169 | "output_type": "error", 170 | "traceback": [ 171 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 172 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 173 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;34m'300'\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m400\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", 174 | "\u001b[0;31mTypeError\u001b[0m: must be str, not int" 175 | ] 176 | } 177 | ], 178 | "source": [ 179 | "\n", 180 | "'300' + 400\n", 181 | "\n" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 6, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "data": { 191 | "text/plain": [ 192 | "700" 193 | ] 194 | }, 195 | "execution_count": 6, 196 | "metadata": {}, 197 | "output_type": "execute_result" 198 | } 199 | ], 200 | "source": [ 201 | "int('300') + 400 " 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": {}, 207 | "source": [ 208 | "#### Identity" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 17, 214 | "metadata": {}, 215 | "outputs": [ 216 | { 217 | "data": { 218 | "text/plain": [ 219 | "140270281056928" 220 | ] 221 | }, 222 | "execution_count": 17, 223 | "metadata": {}, 224 | "output_type": "execute_result" 225 | } 226 | ], 227 | "source": [ 228 | "y = 2.5\n", 229 | "z = 2.5\n", 230 | "id(y)" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": 18, 236 | "metadata": {}, 237 | "outputs": [ 238 | { 239 | "data": { 240 | "text/plain": [ 241 | "140270281056688" 242 | ] 243 | }, 244 | "execution_count": 18, 245 | "metadata": {}, 246 | "output_type": "execute_result" 247 | } 248 | ], 249 | "source": [ 250 | "id(z)" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 19, 256 | "metadata": {}, 257 | "outputs": [], 258 | "source": [ 259 | "x, y = 10, 10" 260 | ] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "metadata": {}, 265 | "source": [ 266 | "#### Methods\n", 267 | "\n" 268 | ] 269 | }, 270 | { 271 | "cell_type": "markdown", 272 | "metadata": {}, 273 | "source": [ 274 | "Methods tied to objects, intending to be useful for the data they contain" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": 20, 280 | "metadata": {}, 281 | "outputs": [ 282 | { 283 | "data": { 284 | "text/plain": [ 285 | "['a', 'b', 'c']" 286 | ] 287 | }, 288 | "execution_count": 20, 289 | "metadata": {}, 290 | "output_type": "execute_result" 291 | } 292 | ], 293 | "source": [ 294 | "x = ['a', 'b']\n", 295 | "x.append('c')\n", 296 | "x" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 11, 302 | "metadata": {}, 303 | "outputs": [ 304 | { 305 | "data": { 306 | "text/plain": [ 307 | "'THIS IS A STRING'" 308 | ] 309 | }, 310 | "execution_count": 11, 311 | "metadata": {}, 312 | "output_type": "execute_result" 313 | } 314 | ], 315 | "source": [ 316 | "s = 'This is a string'\n", 317 | "s.upper()" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": 12, 323 | "metadata": {}, 324 | "outputs": [ 325 | { 326 | "data": { 327 | "text/plain": [ 328 | "'this is a string'" 329 | ] 330 | }, 331 | "execution_count": 12, 332 | "metadata": {}, 333 | "output_type": "execute_result" 334 | } 335 | ], 336 | "source": [ 337 | "s.lower()" 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": 13, 343 | "metadata": {}, 344 | "outputs": [ 345 | { 346 | "data": { 347 | "text/plain": [ 348 | "'That is a string'" 349 | ] 350 | }, 351 | "execution_count": 13, 352 | "metadata": {}, 353 | "output_type": "execute_result" 354 | } 355 | ], 356 | "source": [ 357 | "s.replace('This', 'That')" 358 | ] 359 | }, 360 | { 361 | "cell_type": "markdown", 362 | "metadata": {}, 363 | "source": [ 364 | "Even item assignment is really just a method:" 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": 14, 370 | "metadata": {}, 371 | "outputs": [ 372 | { 373 | "data": { 374 | "text/plain": [ 375 | "['aa', 'b']" 376 | ] 377 | }, 378 | "execution_count": 14, 379 | "metadata": {}, 380 | "output_type": "execute_result" 381 | } 382 | ], 383 | "source": [ 384 | "x = ['a', 'b']\n", 385 | "x[0] = 'aa' \n", 386 | "x" 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": 15, 392 | "metadata": {}, 393 | "outputs": [ 394 | { 395 | "data": { 396 | "text/plain": [ 397 | "['aa', 'b']" 398 | ] 399 | }, 400 | "execution_count": 15, 401 | "metadata": {}, 402 | "output_type": "execute_result" 403 | } 404 | ], 405 | "source": [ 406 | "x = ['a', 'b']\n", 407 | "x.__setitem__(0, 'aa') \n", 408 | "x" 409 | ] 410 | }, 411 | { 412 | "cell_type": "markdown", 413 | "metadata": {}, 414 | "source": [ 415 | "### Are you sure everything's an object?" 416 | ] 417 | }, 418 | { 419 | "cell_type": "markdown", 420 | "metadata": {}, 421 | "source": [ 422 | "Literally everything in memory is an object, from integers..." 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": 21, 428 | "metadata": {}, 429 | "outputs": [ 430 | { 431 | "data": { 432 | "text/plain": [ 433 | "42" 434 | ] 435 | }, 436 | "execution_count": 21, 437 | "metadata": {}, 438 | "output_type": "execute_result" 439 | } 440 | ], 441 | "source": [ 442 | "x = 42\n", 443 | "x" 444 | ] 445 | }, 446 | { 447 | "cell_type": "code", 448 | "execution_count": 22, 449 | "metadata": {}, 450 | "outputs": [ 451 | { 452 | "data": { 453 | "text/plain": [ 454 | "0" 455 | ] 456 | }, 457 | "execution_count": 22, 458 | "metadata": {}, 459 | "output_type": "execute_result" 460 | } 461 | ], 462 | "source": [ 463 | "x.imag" 464 | ] 465 | }, 466 | { 467 | "cell_type": "code", 468 | "execution_count": 23, 469 | "metadata": {}, 470 | "outputs": [ 471 | { 472 | "data": { 473 | "text/plain": [ 474 | "int" 475 | ] 476 | }, 477 | "execution_count": 23, 478 | "metadata": {}, 479 | "output_type": "execute_result" 480 | } 481 | ], 482 | "source": [ 483 | "x.__class__" 484 | ] 485 | }, 486 | { 487 | "cell_type": "markdown", 488 | "metadata": {}, 489 | "source": [ 490 | "...to functions..." 491 | ] 492 | }, 493 | { 494 | "cell_type": "code", 495 | "execution_count": 24, 496 | "metadata": {}, 497 | "outputs": [ 498 | { 499 | "data": { 500 | "text/plain": [ 501 | "" 502 | ] 503 | }, 504 | "execution_count": 24, 505 | "metadata": {}, 506 | "output_type": "execute_result" 507 | } 508 | ], 509 | "source": [ 510 | "def f(x): return x**2\n", 511 | "f" 512 | ] 513 | }, 514 | { 515 | "cell_type": "code", 516 | "execution_count": 25, 517 | "metadata": {}, 518 | "outputs": [ 519 | { 520 | "data": { 521 | "text/plain": [ 522 | "function" 523 | ] 524 | }, 525 | "execution_count": 25, 526 | "metadata": {}, 527 | "output_type": "execute_result" 528 | } 529 | ], 530 | "source": [ 531 | "type(f)" 532 | ] 533 | }, 534 | { 535 | "cell_type": "code", 536 | "execution_count": 26, 537 | "metadata": {}, 538 | "outputs": [ 539 | { 540 | "data": { 541 | "text/plain": [ 542 | "140270280313712" 543 | ] 544 | }, 545 | "execution_count": 26, 546 | "metadata": {}, 547 | "output_type": "execute_result" 548 | } 549 | ], 550 | "source": [ 551 | "id(f)" 552 | ] 553 | }, 554 | { 555 | "cell_type": "code", 556 | "execution_count": 27, 557 | "metadata": {}, 558 | "outputs": [ 559 | { 560 | "data": { 561 | "text/plain": [ 562 | "'f'" 563 | ] 564 | }, 565 | "execution_count": 27, 566 | "metadata": {}, 567 | "output_type": "execute_result" 568 | } 569 | ], 570 | "source": [ 571 | "f.__name__" 572 | ] 573 | }, 574 | { 575 | "cell_type": "code", 576 | "execution_count": 28, 577 | "metadata": {}, 578 | "outputs": [ 579 | { 580 | "data": { 581 | "text/plain": [ 582 | "9" 583 | ] 584 | }, 585 | "execution_count": 28, 586 | "metadata": {}, 587 | "output_type": "execute_result" 588 | } 589 | ], 590 | "source": [ 591 | "f.__call__(3)" 592 | ] 593 | }, 594 | { 595 | "cell_type": "markdown", 596 | "metadata": {}, 597 | "source": [ 598 | "...to modules." 599 | ] 600 | }, 601 | { 602 | "cell_type": "code", 603 | "execution_count": 29, 604 | "metadata": {}, 605 | "outputs": [ 606 | { 607 | "data": { 608 | "text/plain": [ 609 | "140270472209576" 610 | ] 611 | }, 612 | "execution_count": 29, 613 | "metadata": {}, 614 | "output_type": "execute_result" 615 | } 616 | ], 617 | "source": [ 618 | "import math\n", 619 | "\n", 620 | "id(math)" 621 | ] 622 | }, 623 | { 624 | "cell_type": "code", 625 | "execution_count": 30, 626 | "metadata": {}, 627 | "outputs": [ 628 | { 629 | "data": { 630 | "text/plain": [ 631 | "module" 632 | ] 633 | }, 634 | "execution_count": 30, 635 | "metadata": {}, 636 | "output_type": "execute_result" 637 | } 638 | ], 639 | "source": [ 640 | "type(math)" 641 | ] 642 | }, 643 | { 644 | "cell_type": "markdown", 645 | "metadata": {}, 646 | "source": [ 647 | "### Classes: Building our own objects" 648 | ] 649 | }, 650 | { 651 | "cell_type": "markdown", 652 | "metadata": {}, 653 | "source": [ 654 | "Classes are how we build our own custom objects.\n", 655 | "\n", 656 | "Classes are the blueprint and objects are then generated (instantiated) from this blueprint.\n", 657 | "\n", 658 | "Here's an example:" 659 | ] 660 | }, 661 | { 662 | "cell_type": "code", 663 | "execution_count": 52, 664 | "metadata": {}, 665 | "outputs": [], 666 | "source": [ 667 | "class Firm:\n", 668 | " \"\"\"\n", 669 | " Stores the parameters of the production function f(k) = Ak^α,\n", 670 | " implements the function.\n", 671 | " \"\"\"\n", 672 | " \n", 673 | " def __init__(self, α=0.5, A=2.0):\n", 674 | " self.α = α\n", 675 | " self.A = A\n", 676 | " \n", 677 | " def f(self, k):\n", 678 | " return self.A * k**self.α\n", 679 | " \n", 680 | " " 681 | ] 682 | }, 683 | { 684 | "cell_type": "markdown", 685 | "metadata": {}, 686 | "source": [ 687 | "You can see there's some boilerplate, which you'll get used to over time\n", 688 | "\n", 689 | "* the `__init__` method is used to help build an instance\n", 690 | "* think of `self` as referring to data specific to an instance" 691 | ] 692 | }, 693 | { 694 | "cell_type": "markdown", 695 | "metadata": {}, 696 | "source": [ 697 | "Let's generate an instance:" 698 | ] 699 | }, 700 | { 701 | "cell_type": "code", 702 | "execution_count": 53, 703 | "metadata": {}, 704 | "outputs": [], 705 | "source": [ 706 | "firm = Firm()" 707 | ] 708 | }, 709 | { 710 | "cell_type": "code", 711 | "execution_count": 54, 712 | "metadata": {}, 713 | "outputs": [ 714 | { 715 | "data": { 716 | "text/plain": [ 717 | "0.5" 718 | ] 719 | }, 720 | "execution_count": 54, 721 | "metadata": {}, 722 | "output_type": "execute_result" 723 | } 724 | ], 725 | "source": [ 726 | "firm.α" 727 | ] 728 | }, 729 | { 730 | "cell_type": "code", 731 | "execution_count": 55, 732 | "metadata": {}, 733 | "outputs": [ 734 | { 735 | "data": { 736 | "text/plain": [ 737 | "6.324555320336759" 738 | ] 739 | }, 740 | "execution_count": 55, 741 | "metadata": {}, 742 | "output_type": "execute_result" 743 | } 744 | ], 745 | "source": [ 746 | "k = 10.0\n", 747 | "firm.f(k)" 748 | ] 749 | }, 750 | { 751 | "cell_type": "code", 752 | "execution_count": 56, 753 | "metadata": {}, 754 | "outputs": [], 755 | "source": [ 756 | "firm.A = 10.0" 757 | ] 758 | }, 759 | { 760 | "cell_type": "code", 761 | "execution_count": 57, 762 | "metadata": {}, 763 | "outputs": [ 764 | { 765 | "data": { 766 | "text/plain": [ 767 | "31.622776601683796" 768 | ] 769 | }, 770 | "execution_count": 57, 771 | "metadata": {}, 772 | "output_type": "execute_result" 773 | } 774 | ], 775 | "source": [ 776 | "firm.f(k)" 777 | ] 778 | }, 779 | { 780 | "cell_type": "markdown", 781 | "metadata": {}, 782 | "source": [ 783 | "#### Summary" 784 | ] 785 | }, 786 | { 787 | "cell_type": "markdown", 788 | "metadata": {}, 789 | "source": [ 790 | "When it first came out, the concept of OOP was overhyped and oversold.\n", 791 | "\n", 792 | "Some claimed the OOP methodology was ideal for solving every coding problem known to humankind.\n", 793 | "\n", 794 | "The hype lead to a backlash amongst developers, against OOP\n", 795 | "\n", 796 | "The backlash was a good thing **but** you should ignore extremists on both sides.\n", 797 | "\n", 798 | "Python's object model is elegant and widely admired.\n", 799 | "\n", 800 | "Also, classes are definitely useful --- I use one lightweight class in almost every script I write, to\n", 801 | "\n", 802 | "* store and organize parameters\n", 803 | "* test restrictions on parameters\n", 804 | "* implement other small tasks like generating and storing grids based on information contained in the parameters\n", 805 | "\n", 806 | "Just don't overuse them --- large classes and class hierarchies are a pain in the `#!@$`" 807 | ] 808 | }, 809 | { 810 | "cell_type": "code", 811 | "execution_count": null, 812 | "metadata": {}, 813 | "outputs": [], 814 | "source": [] 815 | } 816 | ], 817 | "metadata": { 818 | "kernelspec": { 819 | "display_name": "Python 3", 820 | "language": "python", 821 | "name": "python3" 822 | }, 823 | "language_info": { 824 | "codemirror_mode": { 825 | "name": "ipython", 826 | "version": 3 827 | }, 828 | "file_extension": ".py", 829 | "mimetype": "text/x-python", 830 | "name": "python", 831 | "nbconvert_exporter": "python", 832 | "pygments_lexer": "ipython3", 833 | "version": "3.6.4" 834 | } 835 | }, 836 | "nbformat": 4, 837 | "nbformat_minor": 2 838 | } 839 | -------------------------------------------------------------------------------- /lecture_2/recent-grads.csv: -------------------------------------------------------------------------------- 1 | Rank,Major_code,Major,Total,Men,Women,Major_category,ShareWomen,Sample_size,Employed,Full_time,Part_time,Full_time_year_round,Unemployed,Unemployment_rate,Median,P25th,P75th,College_jobs,Non_college_jobs,Low_wage_jobs 2 | 1,2419,PETROLEUM ENGINEERING,2339,2057,282,Engineering,0.120564344,36,1976,1849,270,1207,37,0.018380527,110000,95000,125000,1534,364,193 3 | 2,2416,MINING AND MINERAL ENGINEERING,756,679,77,Engineering,0.101851852,7,640,556,170,388,85,0.117241379,75000,55000,90000,350,257,50 4 | 3,2415,METALLURGICAL ENGINEERING,856,725,131,Engineering,0.153037383,3,648,558,133,340,16,0.024096386,73000,50000,105000,456,176,0 5 | 4,2417,NAVAL ARCHITECTURE AND MARINE ENGINEERING,1258,1123,135,Engineering,0.107313196,16,758,1069,150,692,40,0.050125313,70000,43000,80000,529,102,0 6 | 5,2405,CHEMICAL ENGINEERING,32260,21239,11021,Engineering,0.341630502,289,25694,23170,5180,16697,1672,0.061097712,65000,50000,75000,18314,4440,972 7 | 6,2418,NUCLEAR ENGINEERING,2573,2200,373,Engineering,0.144966965,17,1857,2038,264,1449,400,0.177226407,65000,50000,102000,1142,657,244 8 | 7,6202,ACTUARIAL SCIENCE,3777,2110,1667,Business,0.441355573,51,2912,2924,296,2482,308,0.095652174,62000,53000,72000,1768,314,259 9 | 8,5001,ASTRONOMY AND ASTROPHYSICS,1792,832,960,Physical Sciences,0.535714286,10,1526,1085,553,827,33,0.021167415,62000,31500,109000,972,500,220 10 | 9,2414,MECHANICAL ENGINEERING,91227,80320,10907,Engineering,0.119558903,1029,76442,71298,13101,54639,4650,0.057342278,60000,48000,70000,52844,16384,3253 11 | 10,2408,ELECTRICAL ENGINEERING,81527,65511,16016,Engineering,0.196450256,631,61928,55450,12695,41413,3895,0.059173845,60000,45000,72000,45829,10874,3170 12 | 11,2407,COMPUTER ENGINEERING,41542,33258,8284,Engineering,0.199412643,399,32506,30315,5146,23621,2275,0.065409275,60000,45000,75000,23694,5721,980 13 | 12,2401,AEROSPACE ENGINEERING,15058,12953,2105,Engineering,0.139792801,147,11391,11106,2724,8790,794,0.065162085,60000,42000,70000,8184,2425,372 14 | 13,2404,BIOMEDICAL ENGINEERING,14955,8407,6548,Engineering,0.437846874,79,10047,9017,2694,5986,1019,0.09208386,60000,36000,70000,6439,2471,789 15 | 14,5008,MATERIALS SCIENCE,4279,2949,1330,Engineering,0.310820285,22,3307,2751,878,1967,78,0.023042836,60000,39000,65000,2626,391,81 16 | 15,2409,ENGINEERING MECHANICS PHYSICS AND SCIENCE,4321,3526,795,Engineering,0.183985189,30,3608,2999,811,2004,23,0.006334343,58000,25000,74000,2439,947,263 17 | 16,2402,BIOLOGICAL ENGINEERING,8925,6062,2863,Engineering,0.320784314,55,6170,5455,1983,3413,589,0.087143069,57100,40000,76000,3603,1595,524 18 | 17,2412,INDUSTRIAL AND MANUFACTURING ENGINEERING,18968,12453,6515,Engineering,0.343473218,183,15604,14879,2243,11326,699,0.042875544,57000,37900,67000,8306,3235,640 19 | 18,2400,GENERAL ENGINEERING,61152,45683,15469,Engineering,0.252959838,425,44931,41235,7199,33540,2859,0.059824231,56000,36000,69000,26898,11734,3192 20 | 19,2403,ARCHITECTURAL ENGINEERING,2825,1835,990,Engineering,0.350442478,26,2575,2277,343,1848,170,0.061930783,54000,38000,65000,1665,649,137 21 | 20,3201,COURT REPORTING,1148,877,271,Law & Public Policy,0.236062718,14,930,808,223,808,11,0.011689692,54000,50000,54000,402,528,144 22 | 21,2102,COMPUTER SCIENCE,128319,99743,28576,Computers & Mathematics,0.222695002,1196,102087,91485,18726,70932,6884,0.063172771,53000,39000,70000,68622,25667,5144 23 | 22,1104,FOOD SCIENCE,,,,Agriculture & Natural Resources,,36,3149,2558,1121,1735,338,0.09693146,53000,32000,70000,1183,1274,485 24 | 23,2502,ELECTRICAL ENGINEERING TECHNOLOGY,11565,8181,3384,Engineering,0.292607004,97,8587,7530,1873,5681,824,0.087557114,52000,35000,60000,5126,2686,696 25 | 24,2413,MATERIALS ENGINEERING AND MATERIALS SCIENCE,2993,2020,973,Engineering,0.325091881,22,2449,1658,1040,1151,70,0.027788805,52000,35000,62000,1911,305,70 26 | 25,6212,MANAGEMENT INFORMATION SYSTEMS AND STATISTICS,18713,13496,5217,Business,0.278790146,278,16413,15141,2420,13017,1015,0.058239614,51000,38000,60000,6342,5741,708 27 | 26,2406,CIVIL ENGINEERING,53153,41081,12072,Engineering,0.227117943,565,43041,38302,10080,29196,3270,0.070609574,50000,40000,60000,28526,9356,2899 28 | 27,5601,CONSTRUCTION SERVICES,18498,16820,1678,Industrial Arts & Consumer Services,0.090712509,295,16318,15690,1751,12313,1042,0.060023041,50000,36000,60000,3275,5351,703 29 | 28,6204,OPERATIONS LOGISTICS AND E-COMMERCE,11732,7921,3811,Business,0.32483805,156,10027,9639,1183,7724,504,0.047858703,50000,40000,60000,1466,3629,285 30 | 29,2499,MISCELLANEOUS ENGINEERING,9133,7398,1735,Engineering,0.189970437,118,7428,6811,1662,5476,597,0.074392523,50000,39000,65000,3445,2426,365 31 | 30,5402,PUBLIC POLICY,5978,2639,3339,Law & Public Policy,0.558548009,55,4547,4163,1306,2776,670,0.128426299,50000,35000,70000,1550,1871,340 32 | 31,2410,ENVIRONMENTAL ENGINEERING,4047,2662,1385,Engineering,0.342228811,26,2983,2384,930,1951,308,0.093588575,50000,42000,56000,2028,830,260 33 | 32,2500,ENGINEERING TECHNOLOGIES,3600,2695,905,Engineering,0.251388889,39,2799,2257,689,1723,163,0.055030385,50000,43000,60000,1017,1269,142 34 | 33,6099,MISCELLANEOUS FINE ARTS,3340,1970,1370,Arts,0.410179641,30,2914,2049,1067,1200,286,0.089375,50000,25000,66000,693,1714,755 35 | 34,2411,GEOLOGICAL AND GEOPHYSICAL ENGINEERING,720,488,232,Engineering,0.322222222,5,604,524,126,396,49,0.075038285,50000,42800,57000,501,50,49 36 | 35,6107,NURSING,209394,21773,187621,Health,0.896018988,2554,180903,151191,40818,122817,8497,0.044862724,48000,39000,58000,151643,26146,6193 37 | 36,6207,FINANCE,174506,115030,59476,Business,0.340824957,2189,145696,137921,21463,108595,9413,0.060686356,47000,35000,64000,24243,48447,9910 38 | 37,5501,ECONOMICS,139247,89749,49498,Social Science,0.355469059,1322,104117,96567,25325,70740,11452,0.099092317,47000,35000,65000,25582,37057,10653 39 | 38,6205,BUSINESS ECONOMICS,13302,7575,5727,Business,0.430536761,199,10914,10048,1937,8000,1165,0.096448381,46000,33000,58000,1578,4612,1284 40 | 39,2503,INDUSTRIAL PRODUCTION TECHNOLOGIES,4631,3477,1154,Engineering,0.24919024,73,4428,3988,597,3242,129,0.028308097,46000,35000,65000,1394,2454,480 41 | 40,5102,"NUCLEAR, INDUSTRIAL RADIOLOGY, AND BIOLOGICAL TECHNOLOGIES",2116,528,1588,Physical Sciences,0.75047259,31,1778,1392,579,1115,137,0.07154047,46000,38000,53000,162,1475,124 42 | 41,6201,ACCOUNTING,198633,94519,104114,Business,0.524152583,2042,165527,151967,27693,123169,12411,0.069749014,45000,34000,56000,11417,39323,10886 43 | 42,3700,MATHEMATICS,72397,39956,32441,Computers & Mathematics,0.448098678,541,58118,46399,18079,33738,2884,0.047277138,45000,33000,60000,34800,14829,4569 44 | 43,2100,COMPUTER AND INFORMATION SYSTEMS,36698,27392,9306,Computers & Mathematics,0.253583302,425,28459,26348,4332,21130,2934,0.093460326,45000,30000,60000,13344,11783,1672 45 | 44,5007,PHYSICS,32142,23080,9062,Physical Sciences,0.281936407,142,25302,19428,8721,14389,1282,0.048224496,45000,30000,68000,18674,4576,1823 46 | 45,6105,MEDICAL TECHNOLOGIES TECHNICIANS,15914,3916,11998,Health,0.75392736,190,13150,11510,2665,9005,505,0.03698279,45000,36000,50000,5546,7176,1002 47 | 46,2105,INFORMATION SCIENCES,11913,9005,2908,Computers & Mathematics,0.244103081,158,9881,9105,1468,7378,639,0.060741445,45000,32500,58000,4390,4102,608 48 | 47,3702,STATISTICS AND DECISION SCIENCE,6251,2960,3291,Computers & Mathematics,0.526475764,37,4247,3190,1840,2151,401,0.086273666,45000,26700,60000,2298,1200,343 49 | 48,3701,APPLIED MATHEMATICS,4939,2794,2145,Computers & Mathematics,0.434298441,45,3854,3465,1176,2593,385,0.090823307,45000,34000,63000,2437,803,357 50 | 49,3607,PHARMACOLOGY,1762,515,1247,Biology & Life Science,0.707718502,3,1144,657,532,565,107,0.085531575,45000,40000,45000,603,478,93 51 | 50,5006,OCEANOGRAPHY,2418,752,1666,Physical Sciences,0.688999173,36,1638,1931,379,1595,99,0.056994819,44700,23000,50000,459,996,186 52 | 51,2501,ENGINEERING AND INDUSTRIAL MANAGEMENT,2906,2400,506,Engineering,0.174122505,29,2125,1992,462,1358,74,0.03365166,44000,30000,50000,482,844,245 53 | 52,6104,MEDICAL ASSISTING SERVICES,11123,803,10320,Health,0.927807246,67,9168,5643,4107,4290,407,0.042506527,42000,30000,65000,2091,6948,1270 54 | 53,4005,MATHEMATICS AND COMPUTER SCIENCE,609,500,109,Computers & Mathematics,0.178981938,7,559,584,0,391,0,0,42000,30000,78000,452,67,25 55 | 54,2101,COMPUTER PROGRAMMING AND DATA PROCESSING,4168,3046,1122,Computers & Mathematics,0.269193858,43,3257,3204,482,2453,419,0.11398259,41300,20000,46000,2024,1033,263 56 | 55,4006,COGNITIVE SCIENCE AND BIOPSYCHOLOGY,3831,1667,2164,Biology & Life Science,0.56486557,25,2741,2470,711,1584,223,0.075236167,41000,20000,60000,1369,921,135 57 | 56,2303,SCHOOL STUDENT COUNSELING,818,119,699,Education,0.854523227,4,730,595,135,545,88,0.107579462,41000,41000,43000,509,221,0 58 | 57,5505,INTERNATIONAL RELATIONS,28187,10345,17842,Social Science,0.632986838,219,21190,18681,5563,13583,2271,0.096798943,40100,31200,53000,6774,9570,2499 59 | 58,6200,GENERAL BUSINESS,234590,132238,102352,Business,0.436301633,2380,190183,171385,36241,138299,14946,0.072861468,40000,30000,55000,29334,100831,27320 60 | 59,1401,ARCHITECTURE,46420,25463,20957,Engineering,0.451464886,362,34158,29223,10206,20026,4366,0.113331949,40000,31000,50000,16178,13724,4221 61 | 60,6210,INTERNATIONAL BUSINESS,25894,10624,15270,Business,0.589711902,260,19660,17563,4890,12823,2092,0.096175064,40000,30000,50000,3383,9482,3046 62 | 61,6108,PHARMACY PHARMACEUTICAL SCIENCES AND ADMINISTRATION,23551,8697,14854,Health,0.630716318,38,16620,12537,5346,9131,977,0.055520827,40000,20000,90000,11573,4493,1121 63 | 62,3603,MOLECULAR BIOLOGY,18300,7426,10874,Biology & Life Science,0.59420765,90,11581,9441,4590,6183,1067,0.084361164,40000,29000,47000,7225,3145,1168 64 | 63,6299,MISCELLANEOUS BUSINESS & MEDICAL ADMINISTRATION,17947,10285,7662,Business,0.42692372,244,14826,13364,3366,10637,1150,0.071982974,40000,30000,51000,2236,8937,1758 65 | 64,1101,AGRICULTURE PRODUCTION AND MANAGEMENT,14240,9658,4582,Agriculture & Natural Resources,0.321769663,273,12323,11119,2196,9093,649,0.050030836,40000,25000,50000,1925,6221,1362 66 | 65,1100,GENERAL AGRICULTURE,10399,6053,4346,Agriculture & Natural Resources,0.4179248,158,8884,7589,2031,5888,178,0.019642463,40000,30000,50000,2418,4717,839 67 | 66,2599,MISCELLANEOUS ENGINEERING TECHNOLOGIES,8804,7043,1761,Engineering,0.200022717,125,7502,7001,1240,5825,416,0.05253852,40000,30400,56000,2446,3896,386 68 | 67,2504,MECHANICAL ENGINEERING RELATED TECHNOLOGIES,4790,4419,371,Engineering,0.077453027,71,4186,4175,247,3607,250,0.056357078,40000,27000,52000,1861,2121,406 69 | 68,3605,GENETICS,3635,1761,1874,Biology & Life Science,0.515543329,11,2463,1787,847,1487,87,0.034117647,40000,34000,45000,1675,678,201 70 | 69,5599,MISCELLANEOUS SOCIAL SCIENCES,3283,1499,1784,Social Science,0.543405422,28,2727,2183,907,1530,215,0.073079538,40000,30000,54000,744,1654,573 71 | 70,6403,UNITED STATES HISTORY,3079,1756,1323,Humanities & Liberal Arts,0.429684963,22,2787,2103,839,1274,138,0.047179487,40000,30000,42000,801,1591,302 72 | 71,5205,INDUSTRIAL AND ORGANIZATIONAL PSYCHOLOGY,3014,1075,1939,Psychology & Social Work,0.643331121,24,2343,1644,1095,1409,286,0.108786611,40000,32000,53000,559,1224,272 73 | 72,1102,AGRICULTURAL ECONOMICS,2439,1749,690,Agriculture & Natural Resources,0.282902829,44,2174,1819,620,1528,182,0.077249576,40000,27000,54000,535,893,94 74 | 73,5000,PHYSICAL SCIENCES,1436,894,542,Physical Sciences,0.377437326,10,1146,768,437,653,42,0.035353535,40000,30000,55000,530,465,269 75 | 74,3801,MILITARY TECHNOLOGIES,124,124,0,Industrial Arts & Consumer Services,0,4,0,111,0,111,0,0,40000,40000,40000,0,0,0 76 | 75,5003,CHEMISTRY,66530,32923,33607,Physical Sciences,0.505140538,353,48535,39509,15066,29910,2769,0.0539724,39000,30000,49900,30382,14718,4288 77 | 76,5701,"ELECTRICAL, MECHANICAL, AND PRECISION TECHNOLOGIES AND PRODUCTION",2435,1869,566,Industrial Arts & Consumer Services,0.232443532,37,2107,2057,287,1752,64,0.029479503,38400,22500,45000,221,1659,81 78 | 77,6203,BUSINESS MANAGEMENT AND ADMINISTRATION,329927,173809,156118,Business,0.473189524,4212,276234,251540,50357,199897,21502,0.072218341,38000,29000,50000,36720,148395,32395 79 | 78,6206,MARKETING AND MARKETING RESEARCH,205211,78857,126354,Business,0.615727227,2684,178862,156668,35829,127230,11663,0.061215064,38000,30000,50000,25320,93889,27968 80 | 79,5506,POLITICAL SCIENCE AND GOVERNMENT,182621,93880,88741,Social Science,0.485929877,1387,133454,117709,43711,83236,15022,0.101174601,38000,28000,50000,36854,66947,19803 81 | 80,5504,GEOGRAPHY,18480,11404,7076,Social Science,0.382900433,179,14057,11367,5651,8628,1799,0.113458628,38000,30000,50000,5350,6830,1905 82 | 81,3606,MICROBIOLOGY,15232,6383,8849,Biology & Life Science,0.580948004,62,9685,7453,3379,5080,693,0.066775872,38000,29600,50000,5577,3174,1246 83 | 82,2106,COMPUTER ADMINISTRATION MANAGEMENT AND SECURITY,8066,6607,1459,Computers & Mathematics,0.180882718,103,6509,6289,1030,4936,721,0.099723375,37500,25000,50000,2354,3244,308 84 | 83,3601,BIOCHEMICAL SCIENCES,39107,18951,20156,Biology & Life Science,0.515406449,174,25678,20643,9948,13785,2249,0.080531385,37400,29000,50000,15654,8394,3012 85 | 84,3602,BOTANY,1329,626,703,Biology & Life Science,0.52896915,9,1010,946,169,740,0,0,37000,26000,40000,677,184,56 86 | 85,2107,COMPUTER NETWORKING AND TELECOMMUNICATIONS,7613,5291,2322,Computers & Mathematics,0.305004597,97,6144,5495,1447,4369,1100,0.151849807,36400,27000,49000,2593,2941,352 87 | 86,5004,GEOLOGY AND EARTH SCIENCE,10972,5813,5159,Physical Sciences,0.470196865,78,8296,6966,2913,5008,677,0.075448568,36200,28000,47000,4858,2792,959 88 | 87,6209,HUMAN RESOURCES AND PERSONNEL MANAGEMENT,24497,6184,18313,Business,0.747560926,264,20760,18550,3767,15446,1315,0.059569649,36000,28000,45000,2406,9629,1906 89 | 88,3202,PRE-LAW AND LEGAL STUDIES,13528,4435,9093,Law & Public Policy,0.672161443,92,9762,7851,3595,5370,757,0.071965016,36000,29200,46000,2002,6454,1336 90 | 89,6199,MISCELLANEOUS HEALTH MEDICAL PROFESSIONS,13386,1589,11797,Health,0.881293889,81,10076,7514,4145,5868,893,0.08141125,36000,23000,42000,5652,3835,1422 91 | 90,5401,PUBLIC ADMINISTRATION,5629,2947,2682,Law & Public Policy,0.476461183,46,4158,4148,847,2952,789,0.1594906,36000,23000,60000,919,2313,496 92 | 91,5005,GEOSCIENCES,1978,809,1169,Physical Sciences,0.591001011,18,1441,1264,354,1011,36,0.024373731,36000,21000,41000,784,591,221 93 | 92,5206,SOCIAL PSYCHOLOGY,1386,413,973,Psychology & Social Work,0.702020202,8,1080,828,433,529,33,0.029649596,36000,34000,45000,434,593,37 94 | 93,1301,ENVIRONMENTAL SCIENCE,25965,10787,15178,Biology & Life Science,0.584556133,225,20859,16987,7071,10916,1779,0.078584681,35600,25000,40200,8149,10076,3175 95 | 94,1901,COMMUNICATIONS,213996,70619,143377,Communications & Journalism,0.669998505,2394,179633,147335,49889,116251,14602,0.075176976,35000,27000,45000,40763,97964,27440 96 | 95,5301,CRIMINAL JUSTICE AND FIRE PROTECTION,152824,80231,72593,Law & Public Policy,0.47501047,1728,125393,109970,32242,88548,11268,0.082452199,35000,26000,45000,24348,88858,18404 97 | 96,6004,COMMERCIAL ART AND GRAPHIC DESIGN,103480,32041,71439,Arts,0.690365288,1186,83483,67448,24387,52243,8947,0.096797577,35000,25000,45000,37389,38119,14839 98 | 97,1902,JOURNALISM,72619,23736,48883,Communications & Journalism,0.673143392,843,61022,51411,15902,39524,4535,0.069176442,35000,26000,42900,23279,26672,8512 99 | 98,5098,MULTI-DISCIPLINARY OR GENERAL SCIENCE,62052,27015,35037,Physical Sciences,0.564639335,427,46138,37850,13133,28966,2727,0.055806815,35000,24000,50000,17923,22039,5751 100 | 99,1904,ADVERTISING AND PUBLIC RELATIONS,53162,12862,40300,Communications & Journalism,0.758060269,681,45326,38815,10948,30932,3305,0.067960766,35000,27000,47000,9659,23059,7214 101 | 100,1501,AREA ETHNIC AND CIVILIZATION STUDIES,31195,8739,22456,Humanities & Liberal Arts,0.719858952,249,24629,18755,9541,13109,1668,0.063429289,35000,24500,44000,8465,11818,3677 102 | 101,2310,SPECIAL NEEDS EDUCATION,28739,2682,26057,Education,0.906677337,246,24639,21584,5153,16642,1067,0.041507819,35000,32000,42000,20185,3797,1179 103 | 102,3608,PHYSIOLOGY,22060,8422,13638,Biology & Life Science,0.618223028,99,14643,10732,6541,7588,1088,0.0691628,35000,20000,50000,6587,6894,2237 104 | 103,5503,CRIMINOLOGY,19879,10031,9848,Social Science,0.495397153,214,16181,13616,4543,10548,1743,0.097243919,35000,25000,45000,3373,10605,1895 105 | 104,4002,NUTRITION SCIENCES,18909,2563,16346,Health,0.864456079,118,13217,9601,6648,6625,975,0.068700676,35000,26000,45000,6535,5473,2449 106 | 105,6103,HEALTH AND MEDICAL ADMINISTRATIVE SERVICES,18109,4266,13843,Health,0.764426528,184,15419,13534,3299,10982,1518,0.089626262,35000,27000,42000,2589,8592,1391 107 | 106,2001,COMMUNICATION TECHNOLOGIES,18035,11431,6604,Computers & Mathematics,0.366176878,208,14779,11981,4690,9085,2006,0.119511469,35000,25000,45000,4545,8794,2495 108 | 107,5901,TRANSPORTATION SCIENCES AND TECHNOLOGIES,15150,13257,1893,Industrial Arts & Consumer Services,0.124950495,180,12266,11688,2633,9170,962,0.072724524,35000,22000,52000,4575,6147,557 109 | 108,1303,NATURAL RESOURCES MANAGEMENT,13773,8617,5156,Agriculture & Natural Resources,0.374355623,152,11797,10722,2613,6954,842,0.066619195,35000,25000,42000,4333,5808,1405 110 | 109,3611,NEUROSCIENCE,13663,4944,8719,Biology & Life Science,0.63814682,53,9087,8027,3078,5482,463,0.048481675,35000,30000,44000,5605,2301,902 111 | 110,4000,MULTI/INTERDISCIPLINARY STUDIES,12296,2817,9479,Interdisciplinary,0.770901106,128,9821,8032,3173,6234,749,0.070860927,35000,25000,44000,5176,3903,1061 112 | 111,5002,ATMOSPHERIC SCIENCES AND METEOROLOGY,4043,2744,1299,Physical Sciences,0.321296067,32,3431,2659,1309,2161,78,0.022228555,35000,28000,50000,1808,1317,237 113 | 112,1302,FORESTRY,3607,3156,451,Agriculture & Natural Resources,0.125034655,48,3007,2473,891,1763,322,0.096725743,35000,28600,48000,1096,1692,327 114 | 113,1106,SOIL SCIENCE,685,476,209,Agriculture & Natural Resources,0.305109489,4,613,488,185,383,0,0,35000,18500,44000,355,144,0 115 | 114,2300,GENERAL EDUCATION,143718,26893,116825,Education,0.812876606,919,118241,98408,29558,73531,7195,0.057359929,34000,26000,41000,82007,31112,11443 116 | 115,6402,HISTORY,141951,78253,63698,Humanities & Liberal Arts,0.448732309,1058,105646,84681,40657,59218,11176,0.095666912,34000,25000,47000,35336,54569,16839 117 | 116,2602,FRENCH GERMAN LATIN AND OTHER COMMON FOREIGN LANGUAGE STUDIES,48246,12835,35411,Humanities & Liberal Arts,0.733967583,342,38315,29340,14569,20056,3132,0.075566386,34000,25000,45000,15051,18193,5267 118 | 117,4001,INTERCULTURAL AND INTERNATIONAL STUDIES,24650,8575,16075,Humanities & Liberal Arts,0.652129817,184,18824,14354,7978,8801,1718,0.083633531,34000,24000,45000,4956,10343,3168 119 | 118,2311,SOCIAL SCIENCE OR HISTORY TEACHER EDUCATION,20198,9950,10248,Education,0.507376968,157,17700,14002,5168,8871,1012,0.054082941,34000,23050,42000,10928,5561,1806 120 | 119,6110,COMMUNITY AND PUBLIC HEALTH,19735,4103,15632,Health,0.792095262,130,14512,10099,6377,7460,1833,0.112144387,34000,21000,45000,5225,7385,1854 121 | 120,2305,MATHEMATICS TEACHER EDUCATION,14237,3872,10365,Education,0.728032591,123,13115,11259,2273,8073,216,0.016202835,34000,30000,40000,10699,1977,786 122 | 121,2301,EDUCATIONAL ADMINISTRATION AND SUPERVISION,804,280,524,Education,0.651741294,5,703,733,0,504,0,0,34000,29000,35000,346,206,111 123 | 122,6106,HEALTH AND MEDICAL PREPARATORY PROGRAMS,12740,5521,7219,Health,0.566640502,31,7052,5029,3891,3236,529,0.069779712,33500,23000,40000,3051,3539,1159 124 | 123,3699,MISCELLANEOUS BIOLOGY,10706,4747,5959,Biology & Life Science,0.556603774,63,7767,6076,2568,4542,483,0.058545455,33500,23000,48000,4253,2722,459 125 | 124,3600,BIOLOGY,280709,111762,168947,Biology & Life Science,0.601858152,1370,182295,144512,72371,100336,13874,0.070724732,33400,24000,45000,88232,81109,28339 126 | 125,5507,SOCIOLOGY,115433,32510,82923,Social Science,0.718364766,1024,92721,73475,29639,56561,8608,0.084951001,33000,25000,44000,29051,48899,13748 127 | 126,1903,MASS MEDIA,52824,24704,28120,Communications & Journalism,0.532333788,590,44679,35769,13078,27521,4410,0.089836827,33000,25000,45000,12855,25297,6429 128 | 127,6109,TREATMENT THERAPY PROFESSIONS,48491,13487,35004,Health,0.721865913,224,37861,30020,12346,21735,2409,0.059821207,33000,24000,41000,22215,14616,4468 129 | 128,6211,HOSPITALITY MANAGEMENT,43647,15204,28443,Business,0.651659908,546,36728,32160,7494,23106,2393,0.061169193,33000,25000,42000,2325,23341,9063 130 | 129,2313,LANGUAGE AND DRAMA EDUCATION,30471,3741,26730,Education,0.877227528,235,26033,21419,7239,15266,1379,0.050306435,33000,24000,40000,17985,6824,2819 131 | 130,2601,LINGUISTICS AND COMPARATIVE LANGUAGE AND LITERATURE,16601,4416,12185,Humanities & Liberal Arts,0.733991928,88,11165,8462,4831,5821,1302,0.10443571,33000,25000,40000,4122,5695,2085 132 | 131,2399,MISCELLANEOUS EDUCATION,10150,3654,6496,Education,0.64,126,8691,7264,2202,5816,547,0.059211951,33000,30000,45000,5284,2438,657 133 | 132,4007,INTERDISCIPLINARY SOCIAL SCIENCES,9916,2337,7579,Social Science,0.76432029,95,7444,5843,2834,4714,757,0.092305816,33000,24000,40000,2630,3906,1470 134 | 133,3604,ECOLOGY,9154,3878,5276,Biology & Life Science,0.576360061,86,7585,5603,2741,3912,437,0.054475193,33000,23000,42000,2856,4159,976 135 | 134,2309,SECONDARY TEACHER EDUCATION,17125,6820,10305,Education,0.601751825,156,15116,12520,3782,9193,833,0.05222898,32500,25000,38000,10304,3967,1385 136 | 135,6100,GENERAL MEDICAL AND HEALTH SERVICES,33599,7574,26025,Health,0.774576624,202,24406,18166,11088,12809,2183,0.082101621,32400,25000,45000,9364,12889,3816 137 | 136,4801,PHILOSOPHY AND RELIGIOUS STUDIES,54814,31967,22847,Humanities & Liberal Arts,0.416809574,375,40157,31086,16659,21816,4267,0.096051684,32200,23000,47100,14444,20313,8051 138 | 137,2314,ART AND MUSIC EDUCATION,34181,10732,23449,Education,0.6860244,338,30007,23018,9209,16537,1206,0.038637747,32100,25000,40000,20821,8260,2767 139 | 138,3301,ENGLISH LANGUAGE AND LITERATURE,194673,58227,136446,Humanities & Liberal Arts,0.70089843,1436,149180,114386,57825,81180,14345,0.08772359,32000,23000,41000,57690,71827,26503 140 | 139,2304,ELEMENTARY EDUCATION,170862,13029,157833,Education,0.923745479,1629,149339,123177,37965,86540,7297,0.046585715,32000,23400,38000,108085,36972,11502 141 | 140,4101,PHYSICAL FITNESS PARKS RECREATION AND LEISURE,125074,62181,62893,Industrial Arts & Consumer Services,0.502846315,1014,103078,77428,38515,57978,5593,0.051467273,32000,24000,43000,27581,63946,16838 142 | 141,3401,LIBERAL ARTS,71369,22339,49030,Humanities & Liberal Arts,0.686992952,569,54844,43401,19187,33438,4657,0.078267592,32000,25000,42000,18565,28558,9030 143 | 142,6005,FILM VIDEO AND PHOTOGRAPHIC ARTS,38761,22357,16404,Arts,0.423208896,331,31433,22457,12818,15740,3718,0.10577224,32000,22000,42000,7368,20721,5862 144 | 143,5500,GENERAL SOCIAL SCIENCES,12920,5079,7841,Social Science,0.606888545,113,9602,7700,3396,5679,1108,0.103454715,32000,27000,50000,3602,4778,1634 145 | 144,1105,PLANT SCIENCE AND AGRONOMY,7416,4897,2519,Agriculture & Natural Resources,0.339670982,110,6594,5798,1246,4522,314,0.045454545,32000,22900,40000,2089,3545,1231 146 | 145,2308,SCIENCE AND COMPUTER TEACHER EDUCATION,6483,2049,4434,Education,0.683942619,59,5362,4764,1227,3247,266,0.047263682,32000,28000,39000,4214,1106,591 147 | 146,5200,PSYCHOLOGY,393735,86648,307087,Psychology & Social Work,0.779933204,2584,307933,233205,115172,174438,28169,0.083810867,31500,24000,41000,125148,141860,48207 148 | 147,6002,MUSIC,60633,29909,30724,Arts,0.506720763,419,47662,29010,24943,21425,3918,0.075959674,31000,22300,42000,13752,28786,9286 149 | 148,2306,PHYSICAL AND HEALTH EDUCATION TEACHING,28213,15670,12543,Education,0.444582285,259,23794,19420,7230,13651,1920,0.074667496,31000,24000,40000,12777,9328,2042 150 | 149,6006,ART HISTORY AND CRITICISM,21030,3240,17790,Humanities & Liberal Arts,0.845934379,204,17579,13262,6140,9965,1128,0.060298284,31000,23000,40000,5139,9738,3426 151 | 150,6000,FINE ARTS,74440,24786,49654,Arts,0.667033853,623,59679,42764,23656,31877,5486,0.084186296,30500,21000,41000,20792,32725,11880 152 | 151,2901,FAMILY AND CONSUMER SCIENCES,58001,5166,52835,Industrial Arts & Consumer Services,0.91093257,518,46624,36747,15872,26906,3355,0.067128194,30000,22900,40000,20985,20133,5248 153 | 152,5404,SOCIAL WORK,53552,5137,48415,Psychology & Social Work,0.904074544,374,45038,34941,13481,27588,3329,0.06882792,30000,25000,35000,27449,14416,4344 154 | 153,1103,ANIMAL SCIENCES,21573,5347,16226,Agriculture & Natural Resources,0.752143884,255,17112,14479,5353,10824,917,0.050862499,30000,22000,40000,5443,9571,2125 155 | 154,6003,VISUAL AND PERFORMING ARTS,16250,4133,12117,Arts,0.745661538,132,12870,8447,6253,6322,1465,0.102197419,30000,22000,40000,3849,7635,2840 156 | 155,2312,TEACHER EDUCATION: MULTIPLE LEVELS,14443,2734,11709,Education,0.810704147,142,13076,11734,2214,8457,496,0.03654583,30000,24000,37000,10766,1949,722 157 | 156,5299,MISCELLANEOUS PSYCHOLOGY,9628,1936,7692,Psychology & Social Work,0.798919817,60,7653,5201,3221,3838,419,0.05190783,30000,20800,40000,2960,3948,1650 158 | 157,5403,HUMAN SERVICES AND COMMUNITY ORGANIZATION,9374,885,8489,Psychology & Social Work,0.90558993,89,8294,6455,2405,5061,326,0.037819026,30000,24000,35000,2878,4595,724 159 | 158,3402,HUMANITIES,6652,2013,4639,Humanities & Liberal Arts,0.697384245,49,5052,3565,2225,2661,372,0.068584071,30000,20000,49000,1168,3354,1141 160 | 159,4901,THEOLOGY AND RELIGIOUS VOCATIONS,30207,18616,11591,Humanities & Liberal Arts,0.383719006,310,24202,18079,8767,13944,1617,0.062628297,29000,22000,38000,9927,12037,3304 161 | 160,6007,STUDIO ARTS,16977,4754,12223,Arts,0.719974083,182,13908,10451,5673,7413,1368,0.089552239,29000,19200,38300,3948,8707,3586 162 | 161,2201,COSMETOLOGY SERVICES AND CULINARY ARTS,10510,4364,6146,Industrial Arts & Consumer Services,0.584776403,117,8650,7662,2064,5949,510,0.055676856,29000,20000,36000,563,7384,3163 163 | 162,1199,MISCELLANEOUS AGRICULTURE,1488,404,1084,Agriculture & Natural Resources,0.728494624,24,1290,1098,335,936,82,0.059766764,29000,23000,42100,483,626,31 164 | 163,5502,ANTHROPOLOGY AND ARCHEOLOGY,38844,11376,27468,Humanities & Liberal Arts,0.707136237,247,29633,20147,14515,13232,3395,0.102791571,28000,20000,38000,9805,16693,6866 165 | 164,6102,COMMUNICATION DISORDERS SCIENCES AND SERVICES,38279,1225,37054,Health,0.967998119,95,29763,19975,13862,14460,1487,0.047584,28000,20000,40000,19957,9404,5125 166 | 165,2307,EARLY CHILDHOOD EDUCATION,37589,1167,36422,Education,0.968953683,342,32551,27569,7001,20748,1360,0.040104981,28000,21000,35000,23515,7705,2868 167 | 166,2603,OTHER FOREIGN LANGUAGES,11204,3472,7732,Humanities & Liberal Arts,0.690110675,56,7052,5197,3685,3214,846,0.107115726,27500,22900,38000,2326,3703,1115 168 | 167,6001,DRAMA AND THEATER ARTS,43249,14440,28809,Arts,0.666119448,357,36165,25147,15994,16891,3040,0.07754113,27000,19200,35000,6994,25313,11068 169 | 168,3302,COMPOSITION AND RHETORIC,18953,7022,11931,Humanities & Liberal Arts,0.629504564,151,15053,10121,6612,7832,1340,0.081742207,27000,20000,35000,4855,8100,3466 170 | 169,3609,ZOOLOGY,8409,3050,5359,Biology & Life Science,0.637293376,47,6259,5043,2190,3602,304,0.04632028,26000,20000,39000,2771,2947,743 171 | 170,5201,EDUCATIONAL PSYCHOLOGY,2854,522,2332,Psychology & Social Work,0.817098809,7,2125,1848,572,1211,148,0.065112187,25000,24000,34000,1488,615,82 172 | 171,5202,CLINICAL PSYCHOLOGY,2838,568,2270,Psychology & Social Work,0.799859056,13,2101,1724,648,1293,368,0.149048198,25000,25000,40000,986,870,622 173 | 172,5203,COUNSELING PSYCHOLOGY,4626,931,3695,Psychology & Social Work,0.798746217,21,3777,3154,965,2738,214,0.053620646,23400,19200,26000,2403,1245,308 174 | 173,3501,LIBRARY SCIENCE,1098,134,964,Education,0.877959927,2,742,593,237,410,87,0.104945718,22000,20000,22000,288,338,192 175 | -------------------------------------------------------------------------------- /lecture_1/python_essentials.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Python Essentials\n", 8 | "\n", 9 | "AKA the boring stuff that we nonetheless need to cover." 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "### Native Python data types" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "#### Booleans" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 1, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "data": { 33 | "text/plain": [ 34 | "True" 35 | ] 36 | }, 37 | "execution_count": 1, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "x = True\n", 44 | "x" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 2, 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "data": { 54 | "text/plain": [ 55 | "False" 56 | ] 57 | }, 58 | "execution_count": 2, 59 | "metadata": {}, 60 | "output_type": "execute_result" 61 | } 62 | ], 63 | "source": [ 64 | "y = 100 < 10\n", 65 | "y" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 3, 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "data": { 75 | "text/plain": [ 76 | "bool" 77 | ] 78 | }, 79 | "execution_count": 3, 80 | "metadata": {}, 81 | "output_type": "execute_result" 82 | } 83 | ], 84 | "source": [ 85 | "type(y)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 4, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "data": { 95 | "text/plain": [ 96 | "1" 97 | ] 98 | }, 99 | "execution_count": 4, 100 | "metadata": {}, 101 | "output_type": "execute_result" 102 | } 103 | ], 104 | "source": [ 105 | "x + y" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 5, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "data": { 115 | "text/plain": [ 116 | "0" 117 | ] 118 | }, 119 | "execution_count": 5, 120 | "metadata": {}, 121 | "output_type": "execute_result" 122 | } 123 | ], 124 | "source": [ 125 | "x * y" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "#### Numeric types" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 26, 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "data": { 142 | "text/plain": [ 143 | "int" 144 | ] 145 | }, 146 | "execution_count": 26, 147 | "metadata": {}, 148 | "output_type": "execute_result" 149 | } 150 | ], 151 | "source": [ 152 | "a = 1\n", 153 | "type(a)" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 27, 159 | "metadata": {}, 160 | "outputs": [ 161 | { 162 | "data": { 163 | "text/plain": [ 164 | "float" 165 | ] 166 | }, 167 | "execution_count": 27, 168 | "metadata": {}, 169 | "output_type": "execute_result" 170 | } 171 | ], 172 | "source": [ 173 | "b = 1.0\n", 174 | "type(b)" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 28, 180 | "metadata": {}, 181 | "outputs": [ 182 | { 183 | "data": { 184 | "text/plain": [ 185 | "complex" 186 | ] 187 | }, 188 | "execution_count": 28, 189 | "metadata": {}, 190 | "output_type": "execute_result" 191 | } 192 | ], 193 | "source": [ 194 | "c = 1j + 2\n", 195 | "type(c)" 196 | ] 197 | }, 198 | { 199 | "cell_type": "markdown", 200 | "metadata": {}, 201 | "source": [ 202 | "#### Tuples" 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "metadata": {}, 208 | "source": [ 209 | "Tuples are **immutable** sequences" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 34, 215 | "metadata": {}, 216 | "outputs": [], 217 | "source": [ 218 | "x = ('a', 'b') " 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 35, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "data": { 228 | "text/plain": [ 229 | "tuple" 230 | ] 231 | }, 232 | "execution_count": 35, 233 | "metadata": {}, 234 | "output_type": "execute_result" 235 | } 236 | ], 237 | "source": [ 238 | "type(x)" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 36, 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [ 247 | "x = 'a', 'b' # Or no brackets --- the meaning is identical" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 37, 253 | "metadata": {}, 254 | "outputs": [ 255 | { 256 | "data": { 257 | "text/plain": [ 258 | "tuple" 259 | ] 260 | }, 261 | "execution_count": 37, 262 | "metadata": {}, 263 | "output_type": "execute_result" 264 | } 265 | ], 266 | "source": [ 267 | "type(x)" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 38, 273 | "metadata": {}, 274 | "outputs": [ 275 | { 276 | "data": { 277 | "text/plain": [ 278 | "'a'" 279 | ] 280 | }, 281 | "execution_count": 38, 282 | "metadata": {}, 283 | "output_type": "execute_result" 284 | } 285 | ], 286 | "source": [ 287 | "x[0]" 288 | ] 289 | }, 290 | { 291 | "cell_type": "markdown", 292 | "metadata": {}, 293 | "source": [ 294 | "Unlike lists, cannot mutate data:" 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": 39, 300 | "metadata": {}, 301 | "outputs": [ 302 | { 303 | "ename": "TypeError", 304 | "evalue": "'tuple' object does not support item assignment", 305 | "output_type": "error", 306 | "traceback": [ 307 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 308 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 309 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 310 | "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" 311 | ] 312 | } 313 | ], 314 | "source": [ 315 | "x[0] = 10" 316 | ] 317 | }, 318 | { 319 | "cell_type": "markdown", 320 | "metadata": {}, 321 | "source": [ 322 | "#### Slices on sequence types" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": 14, 328 | "metadata": {}, 329 | "outputs": [ 330 | { 331 | "data": { 332 | "text/plain": [ 333 | "[4, 6, 8]" 334 | ] 335 | }, 336 | "execution_count": 14, 337 | "metadata": {}, 338 | "output_type": "execute_result" 339 | } 340 | ], 341 | "source": [ 342 | "a = [2, 4, 6, 8]\n", 343 | "a[1:]" 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": 15, 349 | "metadata": {}, 350 | "outputs": [ 351 | { 352 | "data": { 353 | "text/plain": [ 354 | "[4, 6]" 355 | ] 356 | }, 357 | "execution_count": 15, 358 | "metadata": {}, 359 | "output_type": "execute_result" 360 | } 361 | ], 362 | "source": [ 363 | "a[1:3]" 364 | ] 365 | }, 366 | { 367 | "cell_type": "markdown", 368 | "metadata": {}, 369 | "source": [ 370 | "### Input and Output" 371 | ] 372 | }, 373 | { 374 | "cell_type": "code", 375 | "execution_count": 13, 376 | "metadata": {}, 377 | "outputs": [], 378 | "source": [ 379 | "f = open('newfile.txt', 'w') # Open 'newfile.txt' for writing\n", 380 | "f.write('Testing\\n') # Here '\\n' means new line\n", 381 | "f.write('Testing again')\n", 382 | "f.close()" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": 14, 388 | "metadata": {}, 389 | "outputs": [ 390 | { 391 | "data": { 392 | "text/plain": [ 393 | "'/home/john/sync_dir/quantecon/presentations/copenhagen_2018/repo/lecture_1'" 394 | ] 395 | }, 396 | "execution_count": 14, 397 | "metadata": {}, 398 | "output_type": "execute_result" 399 | } 400 | ], 401 | "source": [ 402 | "%pwd" 403 | ] 404 | }, 405 | { 406 | "cell_type": "code", 407 | "execution_count": 15, 408 | "metadata": {}, 409 | "outputs": [ 410 | { 411 | "data": { 412 | "text/plain": [ 413 | "'Testing\\nTesting again'" 414 | ] 415 | }, 416 | "execution_count": 15, 417 | "metadata": {}, 418 | "output_type": "execute_result" 419 | } 420 | ], 421 | "source": [ 422 | "f = open('newfile.txt', 'r')\n", 423 | "out = f.read()\n", 424 | "out" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": 16, 430 | "metadata": {}, 431 | "outputs": [ 432 | { 433 | "name": "stdout", 434 | "output_type": "stream", 435 | "text": [ 436 | "Testing\n", 437 | "Testing again\n" 438 | ] 439 | } 440 | ], 441 | "source": [ 442 | "print(out)" 443 | ] 444 | }, 445 | { 446 | "cell_type": "markdown", 447 | "metadata": {}, 448 | "source": [ 449 | "A longer example" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 17, 455 | "metadata": {}, 456 | "outputs": [ 457 | { 458 | "name": "stdout", 459 | "output_type": "stream", 460 | "text": [ 461 | "Writing us_cities.txt\n" 462 | ] 463 | } 464 | ], 465 | "source": [ 466 | "%%file us_cities.txt\n", 467 | "new york: 8244910\n", 468 | "los angeles: 3819702\n", 469 | "chicago: 2707120\n", 470 | "houston: 2145146\n", 471 | "philadelphia: 1536471\n", 472 | "phoenix: 1469471\n", 473 | "san antonio: 1359758\n", 474 | "san diego: 1326179\n", 475 | "dallas: 1223229" 476 | ] 477 | }, 478 | { 479 | "cell_type": "code", 480 | "execution_count": 18, 481 | "metadata": {}, 482 | "outputs": [ 483 | { 484 | "name": "stdout", 485 | "output_type": "stream", 486 | "text": [ 487 | "New York 8,244,910\n", 488 | "Los Angeles 3,819,702\n", 489 | "Chicago 2,707,120\n", 490 | "Houston 2,145,146\n", 491 | "Philadelphia 1,536,471\n", 492 | "Phoenix 1,469,471\n", 493 | "San Antonio 1,359,758\n", 494 | "San Diego 1,326,179\n", 495 | "Dallas 1,223,229\n" 496 | ] 497 | } 498 | ], 499 | "source": [ 500 | "data_file = open('us_cities.txt', 'r')\n", 501 | "for line in data_file:\n", 502 | " city, population = line.split(':') # Tuple unpacking\n", 503 | " city = city.title() # Capitalize city names\n", 504 | " population = '{0:,}'.format(int(population)) # Add commas to numbers\n", 505 | " print(city.ljust(15) + population)\n", 506 | "data_file.close()" 507 | ] 508 | }, 509 | { 510 | "cell_type": "markdown", 511 | "metadata": {}, 512 | "source": [ 513 | "### Back to for loops" 514 | ] 515 | }, 516 | { 517 | "cell_type": "markdown", 518 | "metadata": {}, 519 | "source": [ 520 | "The builtin functions `enumerate` and `zip` help with iteration." 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": 21, 526 | "metadata": {}, 527 | "outputs": [ 528 | { 529 | "name": "stdout", 530 | "output_type": "stream", 531 | "text": [ 532 | "The capital of Japan is Tokyo\n", 533 | "The capital of Korea is Seoul\n", 534 | "The capital of China is Beijing\n" 535 | ] 536 | } 537 | ], 538 | "source": [ 539 | "countries = ('Japan', 'Korea', 'China')\n", 540 | "cities = ('Tokyo', 'Seoul', 'Beijing')\n", 541 | "\n", 542 | "for country, city in zip(countries, cities):\n", 543 | " print(f'The capital of {country} is {city}')" 544 | ] 545 | }, 546 | { 547 | "cell_type": "markdown", 548 | "metadata": {}, 549 | "source": [ 550 | "The `enumerate` function:" 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "execution_count": 22, 556 | "metadata": {}, 557 | "outputs": [ 558 | { 559 | "name": "stdout", 560 | "output_type": "stream", 561 | "text": [ 562 | "element 0 is a\n", 563 | "element 1 is b\n", 564 | "element 2 is c\n" 565 | ] 566 | } 567 | ], 568 | "source": [ 569 | "letter_list = ['a', 'b', 'c']\n", 570 | "for index, letter in enumerate(letter_list):\n", 571 | " print(f\"element {index} is {letter}\")" 572 | ] 573 | }, 574 | { 575 | "cell_type": "markdown", 576 | "metadata": {}, 577 | "source": [ 578 | "### Comparisons" 579 | ] 580 | }, 581 | { 582 | "cell_type": "code", 583 | "execution_count": 23, 584 | "metadata": {}, 585 | "outputs": [ 586 | { 587 | "data": { 588 | "text/plain": [ 589 | "True" 590 | ] 591 | }, 592 | "execution_count": 23, 593 | "metadata": {}, 594 | "output_type": "execute_result" 595 | } 596 | ], 597 | "source": [ 598 | "x, y = 1, 2\n", 599 | "x < y" 600 | ] 601 | }, 602 | { 603 | "cell_type": "code", 604 | "execution_count": 24, 605 | "metadata": {}, 606 | "outputs": [ 607 | { 608 | "data": { 609 | "text/plain": [ 610 | "False" 611 | ] 612 | }, 613 | "execution_count": 24, 614 | "metadata": {}, 615 | "output_type": "execute_result" 616 | } 617 | ], 618 | "source": [ 619 | "x > y" 620 | ] 621 | }, 622 | { 623 | "cell_type": "code", 624 | "execution_count": 25, 625 | "metadata": {}, 626 | "outputs": [ 627 | { 628 | "data": { 629 | "text/plain": [ 630 | "True" 631 | ] 632 | }, 633 | "execution_count": 25, 634 | "metadata": {}, 635 | "output_type": "execute_result" 636 | } 637 | ], 638 | "source": [ 639 | "1 < 2 < 3" 640 | ] 641 | }, 642 | { 643 | "cell_type": "code", 644 | "execution_count": 27, 645 | "metadata": {}, 646 | "outputs": [ 647 | { 648 | "data": { 649 | "text/plain": [ 650 | "False" 651 | ] 652 | }, 653 | "execution_count": 27, 654 | "metadata": {}, 655 | "output_type": "execute_result" 656 | } 657 | ], 658 | "source": [ 659 | "x = 1 # Assignment\n", 660 | "x == 2 # Comparison" 661 | ] 662 | }, 663 | { 664 | "cell_type": "markdown", 665 | "metadata": {}, 666 | "source": [ 667 | "#### Compound statements" 668 | ] 669 | }, 670 | { 671 | "cell_type": "code", 672 | "execution_count": 29, 673 | "metadata": {}, 674 | "outputs": [ 675 | { 676 | "data": { 677 | "text/plain": [ 678 | "True" 679 | ] 680 | }, 681 | "execution_count": 29, 682 | "metadata": {}, 683 | "output_type": "execute_result" 684 | } 685 | ], 686 | "source": [ 687 | "1 < 2 and 'f' in 'foo'" 688 | ] 689 | }, 690 | { 691 | "cell_type": "code", 692 | "execution_count": 30, 693 | "metadata": {}, 694 | "outputs": [ 695 | { 696 | "data": { 697 | "text/plain": [ 698 | "False" 699 | ] 700 | }, 701 | "execution_count": 30, 702 | "metadata": {}, 703 | "output_type": "execute_result" 704 | } 705 | ], 706 | "source": [ 707 | "1 < 2 and 'g' in 'foo'" 708 | ] 709 | }, 710 | { 711 | "cell_type": "code", 712 | "execution_count": 31, 713 | "metadata": {}, 714 | "outputs": [ 715 | { 716 | "data": { 717 | "text/plain": [ 718 | "True" 719 | ] 720 | }, 721 | "execution_count": 31, 722 | "metadata": {}, 723 | "output_type": "execute_result" 724 | } 725 | ], 726 | "source": [ 727 | "1 < 2 or 'g' in 'foo'" 728 | ] 729 | }, 730 | { 731 | "cell_type": "code", 732 | "execution_count": 33, 733 | "metadata": {}, 734 | "outputs": [ 735 | { 736 | "data": { 737 | "text/plain": [ 738 | "True" 739 | ] 740 | }, 741 | "execution_count": 33, 742 | "metadata": {}, 743 | "output_type": "execute_result" 744 | } 745 | ], 746 | "source": [ 747 | "not not True" 748 | ] 749 | }, 750 | { 751 | "cell_type": "markdown", 752 | "metadata": {}, 753 | "source": [ 754 | "### Built in functions" 755 | ] 756 | }, 757 | { 758 | "cell_type": "code", 759 | "execution_count": 34, 760 | "metadata": {}, 761 | "outputs": [ 762 | { 763 | "data": { 764 | "text/plain": [ 765 | "20" 766 | ] 767 | }, 768 | "execution_count": 34, 769 | "metadata": {}, 770 | "output_type": "execute_result" 771 | } 772 | ], 773 | "source": [ 774 | "max(19, 20)" 775 | ] 776 | }, 777 | { 778 | "cell_type": "code", 779 | "execution_count": 35, 780 | "metadata": {}, 781 | "outputs": [ 782 | { 783 | "data": { 784 | "text/plain": [ 785 | "range(0, 4)" 786 | ] 787 | }, 788 | "execution_count": 35, 789 | "metadata": {}, 790 | "output_type": "execute_result" 791 | } 792 | ], 793 | "source": [ 794 | "range(4)" 795 | ] 796 | }, 797 | { 798 | "cell_type": "code", 799 | "execution_count": 36, 800 | "metadata": {}, 801 | "outputs": [ 802 | { 803 | "data": { 804 | "text/plain": [ 805 | "[0, 1, 2, 3]" 806 | ] 807 | }, 808 | "execution_count": 36, 809 | "metadata": {}, 810 | "output_type": "execute_result" 811 | } 812 | ], 813 | "source": [ 814 | "list(range(4))" 815 | ] 816 | }, 817 | { 818 | "cell_type": "code", 819 | "execution_count": 37, 820 | "metadata": {}, 821 | "outputs": [ 822 | { 823 | "data": { 824 | "text/plain": [ 825 | "'22'" 826 | ] 827 | }, 828 | "execution_count": 37, 829 | "metadata": {}, 830 | "output_type": "execute_result" 831 | } 832 | ], 833 | "source": [ 834 | "str(22)" 835 | ] 836 | }, 837 | { 838 | "cell_type": "code", 839 | "execution_count": 38, 840 | "metadata": {}, 841 | "outputs": [ 842 | { 843 | "data": { 844 | "text/plain": [ 845 | "int" 846 | ] 847 | }, 848 | "execution_count": 38, 849 | "metadata": {}, 850 | "output_type": "execute_result" 851 | } 852 | ], 853 | "source": [ 854 | "type(22)" 855 | ] 856 | }, 857 | { 858 | "cell_type": "markdown", 859 | "metadata": {}, 860 | "source": [ 861 | "### Back to functions" 862 | ] 863 | }, 864 | { 865 | "cell_type": "markdown", 866 | "metadata": {}, 867 | "source": [ 868 | "Docstrings:" 869 | ] 870 | }, 871 | { 872 | "cell_type": "code", 873 | "execution_count": 39, 874 | "metadata": {}, 875 | "outputs": [], 876 | "source": [ 877 | "def f(x):\n", 878 | " \"\"\"\n", 879 | " This function squares its argument\n", 880 | " \"\"\"\n", 881 | " return x**2" 882 | ] 883 | }, 884 | { 885 | "cell_type": "code", 886 | "execution_count": 40, 887 | "metadata": {}, 888 | "outputs": [], 889 | "source": [ 890 | "f?" 891 | ] 892 | }, 893 | { 894 | "cell_type": "code", 895 | "execution_count": 41, 896 | "metadata": {}, 897 | "outputs": [], 898 | "source": [ 899 | "f??" 900 | ] 901 | }, 902 | { 903 | "cell_type": "markdown", 904 | "metadata": {}, 905 | "source": [ 906 | "#### One line functions (lambda)" 907 | ] 908 | }, 909 | { 910 | "cell_type": "markdown", 911 | "metadata": {}, 912 | "source": [ 913 | "Instead of this" 914 | ] 915 | }, 916 | { 917 | "cell_type": "code", 918 | "execution_count": 42, 919 | "metadata": {}, 920 | "outputs": [], 921 | "source": [ 922 | "def f(x):\n", 923 | " return x**3" 924 | ] 925 | }, 926 | { 927 | "cell_type": "markdown", 928 | "metadata": {}, 929 | "source": [ 930 | "the following syntax is available" 931 | ] 932 | }, 933 | { 934 | "cell_type": "code", 935 | "execution_count": 43, 936 | "metadata": {}, 937 | "outputs": [], 938 | "source": [ 939 | "f = lambda x: x**3" 940 | ] 941 | }, 942 | { 943 | "cell_type": "markdown", 944 | "metadata": {}, 945 | "source": [ 946 | "Here's a use case" 947 | ] 948 | }, 949 | { 950 | "cell_type": "code", 951 | "execution_count": 44, 952 | "metadata": {}, 953 | "outputs": [ 954 | { 955 | "data": { 956 | "text/plain": [ 957 | "(4.0, 4.440892098500626e-14)" 958 | ] 959 | }, 960 | "execution_count": 44, 961 | "metadata": {}, 962 | "output_type": "execute_result" 963 | } 964 | ], 965 | "source": [ 966 | "from scipy.integrate import quad\n", 967 | "\n", 968 | "quad(lambda x: x**3, 0, 2)" 969 | ] 970 | }, 971 | { 972 | "cell_type": "markdown", 973 | "metadata": {}, 974 | "source": [ 975 | "#### Keyword arguments" 976 | ] 977 | }, 978 | { 979 | "cell_type": "code", 980 | "execution_count": 106, 981 | "metadata": {}, 982 | "outputs": [], 983 | "source": [ 984 | "def f(x, a=1, b=1):\n", 985 | " return a + b * x" 986 | ] 987 | }, 988 | { 989 | "cell_type": "code", 990 | "execution_count": 107, 991 | "metadata": {}, 992 | "outputs": [ 993 | { 994 | "data": { 995 | "text/plain": [ 996 | "3" 997 | ] 998 | }, 999 | "execution_count": 107, 1000 | "metadata": {}, 1001 | "output_type": "execute_result" 1002 | } 1003 | ], 1004 | "source": [ 1005 | "f(2)" 1006 | ] 1007 | }, 1008 | { 1009 | "cell_type": "code", 1010 | "execution_count": 108, 1011 | "metadata": {}, 1012 | "outputs": [ 1013 | { 1014 | "data": { 1015 | "text/plain": [ 1016 | "14" 1017 | ] 1018 | }, 1019 | "execution_count": 108, 1020 | "metadata": {}, 1021 | "output_type": "execute_result" 1022 | } 1023 | ], 1024 | "source": [ 1025 | "f(2, a=4, b=5)" 1026 | ] 1027 | }, 1028 | { 1029 | "cell_type": "markdown", 1030 | "metadata": {}, 1031 | "source": [ 1032 | "### Coding style and PEP8" 1033 | ] 1034 | }, 1035 | { 1036 | "cell_type": "markdown", 1037 | "metadata": {}, 1038 | "source": [ 1039 | "Please read:\n", 1040 | "\n", 1041 | "https://www.python.org/dev/peps/pep-0008/" 1042 | ] 1043 | }, 1044 | { 1045 | "cell_type": "markdown", 1046 | "metadata": {}, 1047 | "source": [ 1048 | "### Exercises" 1049 | ] 1050 | }, 1051 | { 1052 | "cell_type": "markdown", 1053 | "metadata": {}, 1054 | "source": [ 1055 | "#### Exercise 1\n", 1056 | "\n", 1057 | "Write a function called `inner` that, given two numeric lists or tuples of equal length, computes their inner product. Try to make use of ``zip()``.\n", 1058 | "\n" 1059 | ] 1060 | }, 1061 | { 1062 | "cell_type": "markdown", 1063 | "metadata": {}, 1064 | "source": [ 1065 | "#### Exercise 2\n", 1066 | "\n", 1067 | "\n", 1068 | "Write a function that takes a string as an argument and returns the number of capital letters in the string\n", 1069 | "\n", 1070 | "Hint: Use string methods." 1071 | ] 1072 | }, 1073 | { 1074 | "cell_type": "code", 1075 | "execution_count": 17, 1076 | "metadata": {}, 1077 | "outputs": [ 1078 | { 1079 | "name": "stdout", 1080 | "output_type": "stream", 1081 | "text": [ 1082 | "solution below\n", 1083 | "solution below\n", 1084 | "solution below\n", 1085 | "solution below\n", 1086 | "solution below\n", 1087 | "solution below\n", 1088 | "solution below\n", 1089 | "solution below\n", 1090 | "solution below\n", 1091 | "solution below\n", 1092 | "solution below\n", 1093 | "solution below\n", 1094 | "solution below\n", 1095 | "solution below\n", 1096 | "solution below\n", 1097 | "solution below\n", 1098 | "solution below\n", 1099 | "solution below\n", 1100 | "solution below\n", 1101 | "solution below\n" 1102 | ] 1103 | } 1104 | ], 1105 | "source": [ 1106 | "for i in range(20):\n", 1107 | " print(\"solution below\")" 1108 | ] 1109 | }, 1110 | { 1111 | "cell_type": "markdown", 1112 | "metadata": {}, 1113 | "source": [ 1114 | "#### Solution to Ex 1." 1115 | ] 1116 | }, 1117 | { 1118 | "cell_type": "code", 1119 | "execution_count": 18, 1120 | "metadata": {}, 1121 | "outputs": [], 1122 | "source": [ 1123 | "def inner(x_vals, y_vals):\n", 1124 | " if len(x_vals) == len(y_vals):\n", 1125 | " out = sum(x * y for x, y in zip(x_vals, y_vals))\n", 1126 | " else:\n", 1127 | " raise ValueError(\"Sequences must be equal length\")\n", 1128 | " return out" 1129 | ] 1130 | }, 1131 | { 1132 | "cell_type": "code", 1133 | "execution_count": 20, 1134 | "metadata": {}, 1135 | "outputs": [ 1136 | { 1137 | "data": { 1138 | "text/plain": [ 1139 | "12" 1140 | ] 1141 | }, 1142 | "execution_count": 20, 1143 | "metadata": {}, 1144 | "output_type": "execute_result" 1145 | } 1146 | ], 1147 | "source": [ 1148 | "x_vals, y_vals = [2, 3, 10], [1, 0, 1]\n", 1149 | "\n", 1150 | "inner(x_vals, y_vals)" 1151 | ] 1152 | }, 1153 | { 1154 | "cell_type": "code", 1155 | "execution_count": 21, 1156 | "metadata": {}, 1157 | "outputs": [ 1158 | { 1159 | "ename": "ValueError", 1160 | "evalue": "Sequences must be equal length", 1161 | "output_type": "error", 1162 | "traceback": [ 1163 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 1164 | "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", 1165 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx_vals\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_vals\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m100\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0minner\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_vals\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_vals\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 1166 | "\u001b[0;32m\u001b[0m in \u001b[0;36minner\u001b[0;34m(x_vals, y_vals)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mout\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0my\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_vals\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_vals\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Sequences must be equal length\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 1167 | "\u001b[0;31mValueError\u001b[0m: Sequences must be equal length" 1168 | ] 1169 | } 1170 | ], 1171 | "source": [ 1172 | "x_vals, y_vals = [2, 3, 10], [1, 0, 1, 100]\n", 1173 | "\n", 1174 | "inner(x_vals, y_vals)" 1175 | ] 1176 | }, 1177 | { 1178 | "cell_type": "markdown", 1179 | "metadata": {}, 1180 | "source": [ 1181 | "#### Solution to Ex 2." 1182 | ] 1183 | }, 1184 | { 1185 | "cell_type": "code", 1186 | "execution_count": 22, 1187 | "metadata": {}, 1188 | "outputs": [], 1189 | "source": [ 1190 | "def capital_counter(input_string):\n", 1191 | " upper_case_version = input_string.upper()\n", 1192 | " num_upper = 0\n", 1193 | " for i, j in zip(input_string, upper_case_version):\n", 1194 | " if i == j:\n", 1195 | " num_upper += 1\n", 1196 | " return num_upper\n", 1197 | " \n", 1198 | " " 1199 | ] 1200 | }, 1201 | { 1202 | "cell_type": "code", 1203 | "execution_count": 23, 1204 | "metadata": {}, 1205 | "outputs": [ 1206 | { 1207 | "data": { 1208 | "text/plain": [ 1209 | "2" 1210 | ] 1211 | }, 1212 | "execution_count": 23, 1213 | "metadata": {}, 1214 | "output_type": "execute_result" 1215 | } 1216 | ], 1217 | "source": [ 1218 | "capital_counter('FooBar')" 1219 | ] 1220 | }, 1221 | { 1222 | "cell_type": "code", 1223 | "execution_count": null, 1224 | "metadata": {}, 1225 | "outputs": [], 1226 | "source": [] 1227 | } 1228 | ], 1229 | "metadata": { 1230 | "kernelspec": { 1231 | "display_name": "Python 3", 1232 | "language": "python", 1233 | "name": "python3" 1234 | }, 1235 | "language_info": { 1236 | "codemirror_mode": { 1237 | "name": "ipython", 1238 | "version": 3 1239 | }, 1240 | "file_extension": ".py", 1241 | "mimetype": "text/x-python", 1242 | "name": "python", 1243 | "nbconvert_exporter": "python", 1244 | "pygments_lexer": "ipython3", 1245 | "version": "3.6.4" 1246 | } 1247 | }, 1248 | "nbformat": 4, 1249 | "nbformat_minor": 2 1250 | } 1251 | -------------------------------------------------------------------------------- /lecture_1/.ipynb_checkpoints/python_essentials-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Python Essentials\n", 8 | "\n", 9 | "AKA the boring stuff that we nonetheless need to cover." 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "### Native Python data types" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "#### Booleans" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 1, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "data": { 33 | "text/plain": [ 34 | "True" 35 | ] 36 | }, 37 | "execution_count": 1, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "x = True\n", 44 | "x" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 2, 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "data": { 54 | "text/plain": [ 55 | "False" 56 | ] 57 | }, 58 | "execution_count": 2, 59 | "metadata": {}, 60 | "output_type": "execute_result" 61 | } 62 | ], 63 | "source": [ 64 | "y = 100 < 10\n", 65 | "y" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 3, 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "data": { 75 | "text/plain": [ 76 | "bool" 77 | ] 78 | }, 79 | "execution_count": 3, 80 | "metadata": {}, 81 | "output_type": "execute_result" 82 | } 83 | ], 84 | "source": [ 85 | "type(y)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 4, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "data": { 95 | "text/plain": [ 96 | "1" 97 | ] 98 | }, 99 | "execution_count": 4, 100 | "metadata": {}, 101 | "output_type": "execute_result" 102 | } 103 | ], 104 | "source": [ 105 | "x + y" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 5, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "data": { 115 | "text/plain": [ 116 | "0" 117 | ] 118 | }, 119 | "execution_count": 5, 120 | "metadata": {}, 121 | "output_type": "execute_result" 122 | } 123 | ], 124 | "source": [ 125 | "x * y" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "#### Numeric types" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 26, 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "data": { 142 | "text/plain": [ 143 | "int" 144 | ] 145 | }, 146 | "execution_count": 26, 147 | "metadata": {}, 148 | "output_type": "execute_result" 149 | } 150 | ], 151 | "source": [ 152 | "a = 1\n", 153 | "type(a)" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 27, 159 | "metadata": {}, 160 | "outputs": [ 161 | { 162 | "data": { 163 | "text/plain": [ 164 | "float" 165 | ] 166 | }, 167 | "execution_count": 27, 168 | "metadata": {}, 169 | "output_type": "execute_result" 170 | } 171 | ], 172 | "source": [ 173 | "b = 1.0\n", 174 | "type(b)" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 28, 180 | "metadata": {}, 181 | "outputs": [ 182 | { 183 | "data": { 184 | "text/plain": [ 185 | "complex" 186 | ] 187 | }, 188 | "execution_count": 28, 189 | "metadata": {}, 190 | "output_type": "execute_result" 191 | } 192 | ], 193 | "source": [ 194 | "c = 1j + 2\n", 195 | "type(c)" 196 | ] 197 | }, 198 | { 199 | "cell_type": "markdown", 200 | "metadata": {}, 201 | "source": [ 202 | "#### Tuples" 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "metadata": {}, 208 | "source": [ 209 | "Tuples are **immutable** sequences" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 34, 215 | "metadata": {}, 216 | "outputs": [], 217 | "source": [ 218 | "x = ('a', 'b') " 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 35, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "data": { 228 | "text/plain": [ 229 | "tuple" 230 | ] 231 | }, 232 | "execution_count": 35, 233 | "metadata": {}, 234 | "output_type": "execute_result" 235 | } 236 | ], 237 | "source": [ 238 | "type(x)" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 36, 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [ 247 | "x = 'a', 'b' # Or no brackets --- the meaning is identical" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 37, 253 | "metadata": {}, 254 | "outputs": [ 255 | { 256 | "data": { 257 | "text/plain": [ 258 | "tuple" 259 | ] 260 | }, 261 | "execution_count": 37, 262 | "metadata": {}, 263 | "output_type": "execute_result" 264 | } 265 | ], 266 | "source": [ 267 | "type(x)" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 38, 273 | "metadata": {}, 274 | "outputs": [ 275 | { 276 | "data": { 277 | "text/plain": [ 278 | "'a'" 279 | ] 280 | }, 281 | "execution_count": 38, 282 | "metadata": {}, 283 | "output_type": "execute_result" 284 | } 285 | ], 286 | "source": [ 287 | "x[0]" 288 | ] 289 | }, 290 | { 291 | "cell_type": "markdown", 292 | "metadata": {}, 293 | "source": [ 294 | "Unlike lists, cannot mutate data:" 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": 39, 300 | "metadata": {}, 301 | "outputs": [ 302 | { 303 | "ename": "TypeError", 304 | "evalue": "'tuple' object does not support item assignment", 305 | "output_type": "error", 306 | "traceback": [ 307 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 308 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 309 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 310 | "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" 311 | ] 312 | } 313 | ], 314 | "source": [ 315 | "x[0] = 10" 316 | ] 317 | }, 318 | { 319 | "cell_type": "markdown", 320 | "metadata": {}, 321 | "source": [ 322 | "#### Slices on sequence types" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": 14, 328 | "metadata": {}, 329 | "outputs": [ 330 | { 331 | "data": { 332 | "text/plain": [ 333 | "[4, 6, 8]" 334 | ] 335 | }, 336 | "execution_count": 14, 337 | "metadata": {}, 338 | "output_type": "execute_result" 339 | } 340 | ], 341 | "source": [ 342 | "a = [2, 4, 6, 8]\n", 343 | "a[1:]" 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": 15, 349 | "metadata": {}, 350 | "outputs": [ 351 | { 352 | "data": { 353 | "text/plain": [ 354 | "[4, 6]" 355 | ] 356 | }, 357 | "execution_count": 15, 358 | "metadata": {}, 359 | "output_type": "execute_result" 360 | } 361 | ], 362 | "source": [ 363 | "a[1:3]" 364 | ] 365 | }, 366 | { 367 | "cell_type": "markdown", 368 | "metadata": {}, 369 | "source": [ 370 | "### Input and Output" 371 | ] 372 | }, 373 | { 374 | "cell_type": "code", 375 | "execution_count": 13, 376 | "metadata": {}, 377 | "outputs": [], 378 | "source": [ 379 | "f = open('newfile.txt', 'w') # Open 'newfile.txt' for writing\n", 380 | "f.write('Testing\\n') # Here '\\n' means new line\n", 381 | "f.write('Testing again')\n", 382 | "f.close()" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": 14, 388 | "metadata": {}, 389 | "outputs": [ 390 | { 391 | "data": { 392 | "text/plain": [ 393 | "'/home/john/sync_dir/quantecon/presentations/copenhagen_2018/repo/lecture_1'" 394 | ] 395 | }, 396 | "execution_count": 14, 397 | "metadata": {}, 398 | "output_type": "execute_result" 399 | } 400 | ], 401 | "source": [ 402 | "%pwd" 403 | ] 404 | }, 405 | { 406 | "cell_type": "code", 407 | "execution_count": 15, 408 | "metadata": {}, 409 | "outputs": [ 410 | { 411 | "data": { 412 | "text/plain": [ 413 | "'Testing\\nTesting again'" 414 | ] 415 | }, 416 | "execution_count": 15, 417 | "metadata": {}, 418 | "output_type": "execute_result" 419 | } 420 | ], 421 | "source": [ 422 | "f = open('newfile.txt', 'r')\n", 423 | "out = f.read()\n", 424 | "out" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": 16, 430 | "metadata": {}, 431 | "outputs": [ 432 | { 433 | "name": "stdout", 434 | "output_type": "stream", 435 | "text": [ 436 | "Testing\n", 437 | "Testing again\n" 438 | ] 439 | } 440 | ], 441 | "source": [ 442 | "print(out)" 443 | ] 444 | }, 445 | { 446 | "cell_type": "markdown", 447 | "metadata": {}, 448 | "source": [ 449 | "A longer example" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 17, 455 | "metadata": {}, 456 | "outputs": [ 457 | { 458 | "name": "stdout", 459 | "output_type": "stream", 460 | "text": [ 461 | "Writing us_cities.txt\n" 462 | ] 463 | } 464 | ], 465 | "source": [ 466 | "%%file us_cities.txt\n", 467 | "new york: 8244910\n", 468 | "los angeles: 3819702\n", 469 | "chicago: 2707120\n", 470 | "houston: 2145146\n", 471 | "philadelphia: 1536471\n", 472 | "phoenix: 1469471\n", 473 | "san antonio: 1359758\n", 474 | "san diego: 1326179\n", 475 | "dallas: 1223229" 476 | ] 477 | }, 478 | { 479 | "cell_type": "code", 480 | "execution_count": 18, 481 | "metadata": {}, 482 | "outputs": [ 483 | { 484 | "name": "stdout", 485 | "output_type": "stream", 486 | "text": [ 487 | "New York 8,244,910\n", 488 | "Los Angeles 3,819,702\n", 489 | "Chicago 2,707,120\n", 490 | "Houston 2,145,146\n", 491 | "Philadelphia 1,536,471\n", 492 | "Phoenix 1,469,471\n", 493 | "San Antonio 1,359,758\n", 494 | "San Diego 1,326,179\n", 495 | "Dallas 1,223,229\n" 496 | ] 497 | } 498 | ], 499 | "source": [ 500 | "data_file = open('us_cities.txt', 'r')\n", 501 | "for line in data_file:\n", 502 | " city, population = line.split(':') # Tuple unpacking\n", 503 | " city = city.title() # Capitalize city names\n", 504 | " population = '{0:,}'.format(int(population)) # Add commas to numbers\n", 505 | " print(city.ljust(15) + population)\n", 506 | "data_file.close()" 507 | ] 508 | }, 509 | { 510 | "cell_type": "markdown", 511 | "metadata": {}, 512 | "source": [ 513 | "### Back to for loops" 514 | ] 515 | }, 516 | { 517 | "cell_type": "markdown", 518 | "metadata": {}, 519 | "source": [ 520 | "The builtin functions `enumerate` and `zip` help with iteration." 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": 21, 526 | "metadata": {}, 527 | "outputs": [ 528 | { 529 | "name": "stdout", 530 | "output_type": "stream", 531 | "text": [ 532 | "The capital of Japan is Tokyo\n", 533 | "The capital of Korea is Seoul\n", 534 | "The capital of China is Beijing\n" 535 | ] 536 | } 537 | ], 538 | "source": [ 539 | "countries = ('Japan', 'Korea', 'China')\n", 540 | "cities = ('Tokyo', 'Seoul', 'Beijing')\n", 541 | "\n", 542 | "for country, city in zip(countries, cities):\n", 543 | " print(f'The capital of {country} is {city}')" 544 | ] 545 | }, 546 | { 547 | "cell_type": "markdown", 548 | "metadata": {}, 549 | "source": [ 550 | "The `enumerate` function:" 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "execution_count": 22, 556 | "metadata": {}, 557 | "outputs": [ 558 | { 559 | "name": "stdout", 560 | "output_type": "stream", 561 | "text": [ 562 | "element 0 is a\n", 563 | "element 1 is b\n", 564 | "element 2 is c\n" 565 | ] 566 | } 567 | ], 568 | "source": [ 569 | "letter_list = ['a', 'b', 'c']\n", 570 | "for index, letter in enumerate(letter_list):\n", 571 | " print(f\"element {index} is {letter}\")" 572 | ] 573 | }, 574 | { 575 | "cell_type": "markdown", 576 | "metadata": {}, 577 | "source": [ 578 | "### Comparisons" 579 | ] 580 | }, 581 | { 582 | "cell_type": "code", 583 | "execution_count": 23, 584 | "metadata": {}, 585 | "outputs": [ 586 | { 587 | "data": { 588 | "text/plain": [ 589 | "True" 590 | ] 591 | }, 592 | "execution_count": 23, 593 | "metadata": {}, 594 | "output_type": "execute_result" 595 | } 596 | ], 597 | "source": [ 598 | "x, y = 1, 2\n", 599 | "x < y" 600 | ] 601 | }, 602 | { 603 | "cell_type": "code", 604 | "execution_count": 24, 605 | "metadata": {}, 606 | "outputs": [ 607 | { 608 | "data": { 609 | "text/plain": [ 610 | "False" 611 | ] 612 | }, 613 | "execution_count": 24, 614 | "metadata": {}, 615 | "output_type": "execute_result" 616 | } 617 | ], 618 | "source": [ 619 | "x > y" 620 | ] 621 | }, 622 | { 623 | "cell_type": "code", 624 | "execution_count": 25, 625 | "metadata": {}, 626 | "outputs": [ 627 | { 628 | "data": { 629 | "text/plain": [ 630 | "True" 631 | ] 632 | }, 633 | "execution_count": 25, 634 | "metadata": {}, 635 | "output_type": "execute_result" 636 | } 637 | ], 638 | "source": [ 639 | "1 < 2 < 3" 640 | ] 641 | }, 642 | { 643 | "cell_type": "code", 644 | "execution_count": 27, 645 | "metadata": {}, 646 | "outputs": [ 647 | { 648 | "data": { 649 | "text/plain": [ 650 | "False" 651 | ] 652 | }, 653 | "execution_count": 27, 654 | "metadata": {}, 655 | "output_type": "execute_result" 656 | } 657 | ], 658 | "source": [ 659 | "x = 1 # Assignment\n", 660 | "x == 2 # Comparison" 661 | ] 662 | }, 663 | { 664 | "cell_type": "markdown", 665 | "metadata": {}, 666 | "source": [ 667 | "#### Compound statements" 668 | ] 669 | }, 670 | { 671 | "cell_type": "code", 672 | "execution_count": 29, 673 | "metadata": {}, 674 | "outputs": [ 675 | { 676 | "data": { 677 | "text/plain": [ 678 | "True" 679 | ] 680 | }, 681 | "execution_count": 29, 682 | "metadata": {}, 683 | "output_type": "execute_result" 684 | } 685 | ], 686 | "source": [ 687 | "1 < 2 and 'f' in 'foo'" 688 | ] 689 | }, 690 | { 691 | "cell_type": "code", 692 | "execution_count": 30, 693 | "metadata": {}, 694 | "outputs": [ 695 | { 696 | "data": { 697 | "text/plain": [ 698 | "False" 699 | ] 700 | }, 701 | "execution_count": 30, 702 | "metadata": {}, 703 | "output_type": "execute_result" 704 | } 705 | ], 706 | "source": [ 707 | "1 < 2 and 'g' in 'foo'" 708 | ] 709 | }, 710 | { 711 | "cell_type": "code", 712 | "execution_count": 31, 713 | "metadata": {}, 714 | "outputs": [ 715 | { 716 | "data": { 717 | "text/plain": [ 718 | "True" 719 | ] 720 | }, 721 | "execution_count": 31, 722 | "metadata": {}, 723 | "output_type": "execute_result" 724 | } 725 | ], 726 | "source": [ 727 | "1 < 2 or 'g' in 'foo'" 728 | ] 729 | }, 730 | { 731 | "cell_type": "code", 732 | "execution_count": 33, 733 | "metadata": {}, 734 | "outputs": [ 735 | { 736 | "data": { 737 | "text/plain": [ 738 | "True" 739 | ] 740 | }, 741 | "execution_count": 33, 742 | "metadata": {}, 743 | "output_type": "execute_result" 744 | } 745 | ], 746 | "source": [ 747 | "not not True" 748 | ] 749 | }, 750 | { 751 | "cell_type": "markdown", 752 | "metadata": {}, 753 | "source": [ 754 | "### Built in functions" 755 | ] 756 | }, 757 | { 758 | "cell_type": "code", 759 | "execution_count": 34, 760 | "metadata": {}, 761 | "outputs": [ 762 | { 763 | "data": { 764 | "text/plain": [ 765 | "20" 766 | ] 767 | }, 768 | "execution_count": 34, 769 | "metadata": {}, 770 | "output_type": "execute_result" 771 | } 772 | ], 773 | "source": [ 774 | "max(19, 20)" 775 | ] 776 | }, 777 | { 778 | "cell_type": "code", 779 | "execution_count": 35, 780 | "metadata": {}, 781 | "outputs": [ 782 | { 783 | "data": { 784 | "text/plain": [ 785 | "range(0, 4)" 786 | ] 787 | }, 788 | "execution_count": 35, 789 | "metadata": {}, 790 | "output_type": "execute_result" 791 | } 792 | ], 793 | "source": [ 794 | "range(4)" 795 | ] 796 | }, 797 | { 798 | "cell_type": "code", 799 | "execution_count": 36, 800 | "metadata": {}, 801 | "outputs": [ 802 | { 803 | "data": { 804 | "text/plain": [ 805 | "[0, 1, 2, 3]" 806 | ] 807 | }, 808 | "execution_count": 36, 809 | "metadata": {}, 810 | "output_type": "execute_result" 811 | } 812 | ], 813 | "source": [ 814 | "list(range(4))" 815 | ] 816 | }, 817 | { 818 | "cell_type": "code", 819 | "execution_count": 37, 820 | "metadata": {}, 821 | "outputs": [ 822 | { 823 | "data": { 824 | "text/plain": [ 825 | "'22'" 826 | ] 827 | }, 828 | "execution_count": 37, 829 | "metadata": {}, 830 | "output_type": "execute_result" 831 | } 832 | ], 833 | "source": [ 834 | "str(22)" 835 | ] 836 | }, 837 | { 838 | "cell_type": "code", 839 | "execution_count": 38, 840 | "metadata": {}, 841 | "outputs": [ 842 | { 843 | "data": { 844 | "text/plain": [ 845 | "int" 846 | ] 847 | }, 848 | "execution_count": 38, 849 | "metadata": {}, 850 | "output_type": "execute_result" 851 | } 852 | ], 853 | "source": [ 854 | "type(22)" 855 | ] 856 | }, 857 | { 858 | "cell_type": "markdown", 859 | "metadata": {}, 860 | "source": [ 861 | "### Back to functions" 862 | ] 863 | }, 864 | { 865 | "cell_type": "markdown", 866 | "metadata": {}, 867 | "source": [ 868 | "Docstrings:" 869 | ] 870 | }, 871 | { 872 | "cell_type": "code", 873 | "execution_count": 39, 874 | "metadata": {}, 875 | "outputs": [], 876 | "source": [ 877 | "def f(x):\n", 878 | " \"\"\"\n", 879 | " This function squares its argument\n", 880 | " \"\"\"\n", 881 | " return x**2" 882 | ] 883 | }, 884 | { 885 | "cell_type": "code", 886 | "execution_count": 40, 887 | "metadata": {}, 888 | "outputs": [], 889 | "source": [ 890 | "f?" 891 | ] 892 | }, 893 | { 894 | "cell_type": "code", 895 | "execution_count": 41, 896 | "metadata": {}, 897 | "outputs": [], 898 | "source": [ 899 | "f??" 900 | ] 901 | }, 902 | { 903 | "cell_type": "markdown", 904 | "metadata": {}, 905 | "source": [ 906 | "#### One line functions (lambda)" 907 | ] 908 | }, 909 | { 910 | "cell_type": "markdown", 911 | "metadata": {}, 912 | "source": [ 913 | "Instead of this" 914 | ] 915 | }, 916 | { 917 | "cell_type": "code", 918 | "execution_count": 42, 919 | "metadata": {}, 920 | "outputs": [], 921 | "source": [ 922 | "def f(x):\n", 923 | " return x**3" 924 | ] 925 | }, 926 | { 927 | "cell_type": "markdown", 928 | "metadata": {}, 929 | "source": [ 930 | "the following syntax is available" 931 | ] 932 | }, 933 | { 934 | "cell_type": "code", 935 | "execution_count": 43, 936 | "metadata": {}, 937 | "outputs": [], 938 | "source": [ 939 | "f = lambda x: x**3" 940 | ] 941 | }, 942 | { 943 | "cell_type": "markdown", 944 | "metadata": {}, 945 | "source": [ 946 | "Here's a use case" 947 | ] 948 | }, 949 | { 950 | "cell_type": "code", 951 | "execution_count": 44, 952 | "metadata": {}, 953 | "outputs": [ 954 | { 955 | "data": { 956 | "text/plain": [ 957 | "(4.0, 4.440892098500626e-14)" 958 | ] 959 | }, 960 | "execution_count": 44, 961 | "metadata": {}, 962 | "output_type": "execute_result" 963 | } 964 | ], 965 | "source": [ 966 | "from scipy.integrate import quad\n", 967 | "\n", 968 | "quad(lambda x: x**3, 0, 2)" 969 | ] 970 | }, 971 | { 972 | "cell_type": "markdown", 973 | "metadata": {}, 974 | "source": [ 975 | "#### Keyword arguments" 976 | ] 977 | }, 978 | { 979 | "cell_type": "code", 980 | "execution_count": 106, 981 | "metadata": {}, 982 | "outputs": [], 983 | "source": [ 984 | "def f(x, a=1, b=1):\n", 985 | " return a + b * x" 986 | ] 987 | }, 988 | { 989 | "cell_type": "code", 990 | "execution_count": 107, 991 | "metadata": {}, 992 | "outputs": [ 993 | { 994 | "data": { 995 | "text/plain": [ 996 | "3" 997 | ] 998 | }, 999 | "execution_count": 107, 1000 | "metadata": {}, 1001 | "output_type": "execute_result" 1002 | } 1003 | ], 1004 | "source": [ 1005 | "f(2)" 1006 | ] 1007 | }, 1008 | { 1009 | "cell_type": "code", 1010 | "execution_count": 108, 1011 | "metadata": {}, 1012 | "outputs": [ 1013 | { 1014 | "data": { 1015 | "text/plain": [ 1016 | "14" 1017 | ] 1018 | }, 1019 | "execution_count": 108, 1020 | "metadata": {}, 1021 | "output_type": "execute_result" 1022 | } 1023 | ], 1024 | "source": [ 1025 | "f(2, a=4, b=5)" 1026 | ] 1027 | }, 1028 | { 1029 | "cell_type": "markdown", 1030 | "metadata": {}, 1031 | "source": [ 1032 | "### Coding style and PEP8" 1033 | ] 1034 | }, 1035 | { 1036 | "cell_type": "markdown", 1037 | "metadata": {}, 1038 | "source": [ 1039 | "Please read:\n", 1040 | "\n", 1041 | "https://www.python.org/dev/peps/pep-0008/" 1042 | ] 1043 | }, 1044 | { 1045 | "cell_type": "markdown", 1046 | "metadata": {}, 1047 | "source": [ 1048 | "### Exercises" 1049 | ] 1050 | }, 1051 | { 1052 | "cell_type": "markdown", 1053 | "metadata": {}, 1054 | "source": [ 1055 | "#### Exercise 1\n", 1056 | "\n", 1057 | "Write a function called `inner` that, given two numeric lists or tuples of equal length, computes their inner product. Try to make use of ``zip()``.\n", 1058 | "\n" 1059 | ] 1060 | }, 1061 | { 1062 | "cell_type": "markdown", 1063 | "metadata": {}, 1064 | "source": [ 1065 | "#### Exercise 2\n", 1066 | "\n", 1067 | "\n", 1068 | "Write a function that takes a string as an argument and returns the number of capital letters in the string\n", 1069 | "\n", 1070 | "Hint: Use string methods." 1071 | ] 1072 | }, 1073 | { 1074 | "cell_type": "code", 1075 | "execution_count": 17, 1076 | "metadata": {}, 1077 | "outputs": [ 1078 | { 1079 | "name": "stdout", 1080 | "output_type": "stream", 1081 | "text": [ 1082 | "solution below\n", 1083 | "solution below\n", 1084 | "solution below\n", 1085 | "solution below\n", 1086 | "solution below\n", 1087 | "solution below\n", 1088 | "solution below\n", 1089 | "solution below\n", 1090 | "solution below\n", 1091 | "solution below\n", 1092 | "solution below\n", 1093 | "solution below\n", 1094 | "solution below\n", 1095 | "solution below\n", 1096 | "solution below\n", 1097 | "solution below\n", 1098 | "solution below\n", 1099 | "solution below\n", 1100 | "solution below\n", 1101 | "solution below\n" 1102 | ] 1103 | } 1104 | ], 1105 | "source": [ 1106 | "for i in range(20):\n", 1107 | " print(\"solution below\")" 1108 | ] 1109 | }, 1110 | { 1111 | "cell_type": "markdown", 1112 | "metadata": {}, 1113 | "source": [ 1114 | "#### Solution to Ex 1." 1115 | ] 1116 | }, 1117 | { 1118 | "cell_type": "code", 1119 | "execution_count": 18, 1120 | "metadata": {}, 1121 | "outputs": [], 1122 | "source": [ 1123 | "def inner(x_vals, y_vals):\n", 1124 | " if len(x_vals) == len(y_vals):\n", 1125 | " out = sum(x * y for x, y in zip(x_vals, y_vals))\n", 1126 | " else:\n", 1127 | " raise ValueError(\"Sequences must be equal length\")\n", 1128 | " return out" 1129 | ] 1130 | }, 1131 | { 1132 | "cell_type": "code", 1133 | "execution_count": 20, 1134 | "metadata": {}, 1135 | "outputs": [ 1136 | { 1137 | "data": { 1138 | "text/plain": [ 1139 | "12" 1140 | ] 1141 | }, 1142 | "execution_count": 20, 1143 | "metadata": {}, 1144 | "output_type": "execute_result" 1145 | } 1146 | ], 1147 | "source": [ 1148 | "x_vals, y_vals = [2, 3, 10], [1, 0, 1]\n", 1149 | "\n", 1150 | "inner(x_vals, y_vals)" 1151 | ] 1152 | }, 1153 | { 1154 | "cell_type": "code", 1155 | "execution_count": 21, 1156 | "metadata": {}, 1157 | "outputs": [ 1158 | { 1159 | "ename": "ValueError", 1160 | "evalue": "Sequences must be equal length", 1161 | "output_type": "error", 1162 | "traceback": [ 1163 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 1164 | "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", 1165 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx_vals\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_vals\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m100\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0minner\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_vals\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_vals\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 1166 | "\u001b[0;32m\u001b[0m in \u001b[0;36minner\u001b[0;34m(x_vals, y_vals)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mout\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0my\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_vals\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_vals\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Sequences must be equal length\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 1167 | "\u001b[0;31mValueError\u001b[0m: Sequences must be equal length" 1168 | ] 1169 | } 1170 | ], 1171 | "source": [ 1172 | "x_vals, y_vals = [2, 3, 10], [1, 0, 1, 100]\n", 1173 | "\n", 1174 | "inner(x_vals, y_vals)" 1175 | ] 1176 | }, 1177 | { 1178 | "cell_type": "markdown", 1179 | "metadata": {}, 1180 | "source": [ 1181 | "#### Solution to Ex 2." 1182 | ] 1183 | }, 1184 | { 1185 | "cell_type": "code", 1186 | "execution_count": 22, 1187 | "metadata": {}, 1188 | "outputs": [], 1189 | "source": [ 1190 | "def capital_counter(input_string):\n", 1191 | " upper_case_version = input_string.upper()\n", 1192 | " num_upper = 0\n", 1193 | " for i, j in zip(input_string, upper_case_version):\n", 1194 | " if i == j:\n", 1195 | " num_upper += 1\n", 1196 | " return num_upper\n", 1197 | " \n", 1198 | " " 1199 | ] 1200 | }, 1201 | { 1202 | "cell_type": "code", 1203 | "execution_count": 23, 1204 | "metadata": {}, 1205 | "outputs": [ 1206 | { 1207 | "data": { 1208 | "text/plain": [ 1209 | "2" 1210 | ] 1211 | }, 1212 | "execution_count": 23, 1213 | "metadata": {}, 1214 | "output_type": "execute_result" 1215 | } 1216 | ], 1217 | "source": [ 1218 | "capital_counter('FooBar')" 1219 | ] 1220 | }, 1221 | { 1222 | "cell_type": "code", 1223 | "execution_count": null, 1224 | "metadata": {}, 1225 | "outputs": [], 1226 | "source": [] 1227 | } 1228 | ], 1229 | "metadata": { 1230 | "kernelspec": { 1231 | "display_name": "Python 3", 1232 | "language": "python", 1233 | "name": "python3" 1234 | }, 1235 | "language_info": { 1236 | "codemirror_mode": { 1237 | "name": "ipython", 1238 | "version": 3 1239 | }, 1240 | "file_extension": ".py", 1241 | "mimetype": "text/x-python", 1242 | "name": "python", 1243 | "nbconvert_exporter": "python", 1244 | "pygments_lexer": "ipython3", 1245 | "version": "3.6.4" 1246 | } 1247 | }, 1248 | "nbformat": 4, 1249 | "nbformat_minor": 2 1250 | } 1251 | -------------------------------------------------------------------------------- /lecture_2/numpy.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Introduction to NumPy" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "* [NumPy](https://docs.scipy.org/doc/numpy/reference/) is a widely-used scientific computing package for brings fast array processing to Python\n", 15 | "\n", 16 | "* Runs fast compiled code written in C & Fortran under the hood\n", 17 | "\n", 18 | "Consider the following example... we want to calculate the mean of 10,000 numbers\n", 19 | "\n", 20 | "We will do this in both standard Python and using NumPy and compare computing times" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "metadata": {}, 26 | "source": [ 27 | "#### Python version" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 1, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "10000 loops, best of 3: 172 µs per loop\n" 40 | ] 41 | } 42 | ], 43 | "source": [ 44 | "%%timeit\n", 45 | "\n", 46 | "python_list = list(range(10000))\n", 47 | "sum(python_list) / len(python_list)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "#### NumPy version" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 2, 60 | "metadata": { 61 | "collapsed": true 62 | }, 63 | "outputs": [], 64 | "source": [ 65 | "import numpy as np" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 3, 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "name": "stdout", 75 | "output_type": "stream", 76 | "text": [ 77 | "The slowest run took 22.39 times longer than the fastest. This could mean that an intermediate result is being cached.\n", 78 | "10000 loops, best of 3: 21.4 µs per loop\n" 79 | ] 80 | } 81 | ], 82 | "source": [ 83 | "%%timeit\n", 84 | "\n", 85 | "numpy_array = np.arange(10000)\n", 86 | "numpy_array.mean()" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "As you can see, the NumPy version is significantly faster\n", 94 | "* Much of this speed-up is a result of NumPy knowing the **type** of data it is dealing with" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 4, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "array([1.5, 2. , 4. ])" 106 | ] 107 | }, 108 | "execution_count": 4, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "a = np.array([1.5, 2, 4])\n", 115 | "a" 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "metadata": {}, 121 | "source": [ 122 | "All elements of the array must be of the same type" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 5, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "data": { 132 | "text/plain": [ 133 | "[numpy.float64, numpy.float64, numpy.float64]" 134 | ] 135 | }, 136 | "execution_count": 5, 137 | "metadata": {}, 138 | "output_type": "execute_result" 139 | } 140 | ], 141 | "source": [ 142 | "[type(a_element) for a_element in a]" 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "We can specify the data type of the array\n", 150 | "\n", 151 | "The most common data types are:\n", 152 | "\n", 153 | "* float64: 64 bit floating point number\n", 154 | "* int64: 64 bit integer\n", 155 | "* bool: 8 bit True or False" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 6, 161 | "metadata": {}, 162 | "outputs": [ 163 | { 164 | "data": { 165 | "text/plain": [ 166 | "array([1, 2, 4])" 167 | ] 168 | }, 169 | "execution_count": 6, 170 | "metadata": {}, 171 | "output_type": "execute_result" 172 | } 173 | ], 174 | "source": [ 175 | "\n", 176 | "a = np.array([1.8, 2, 4], dtype=int)\n", 177 | "a" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 7, 183 | "metadata": {}, 184 | "outputs": [ 185 | { 186 | "data": { 187 | "text/plain": [ 188 | "[numpy.int64, numpy.int64, numpy.int64]" 189 | ] 190 | }, 191 | "execution_count": 7, 192 | "metadata": {}, 193 | "output_type": "execute_result" 194 | } 195 | ], 196 | "source": [ 197 | "[type(a_element) for a_element in a]" 198 | ] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "metadata": {}, 203 | "source": [ 204 | "By construction, one dimensional NumPy arrays are **flat**" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 8, 210 | "metadata": { 211 | "collapsed": true 212 | }, 213 | "outputs": [], 214 | "source": [ 215 | "z = np.zeros(10)" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 9, 221 | "metadata": {}, 222 | "outputs": [ 223 | { 224 | "data": { 225 | "text/plain": [ 226 | "(10,)" 227 | ] 228 | }, 229 | "execution_count": 9, 230 | "metadata": {}, 231 | "output_type": "execute_result" 232 | } 233 | ], 234 | "source": [ 235 | "z.shape" 236 | ] 237 | }, 238 | { 239 | "cell_type": "markdown", 240 | "metadata": {}, 241 | "source": [ 242 | "Although we can transform them into \"column vectors\" and \"row vectors\" if we wish:" 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": 10, 248 | "metadata": {}, 249 | "outputs": [ 250 | { 251 | "data": { 252 | "text/plain": [ 253 | "array([[0.],\n", 254 | " [0.],\n", 255 | " [0.],\n", 256 | " [0.],\n", 257 | " [0.],\n", 258 | " [0.],\n", 259 | " [0.],\n", 260 | " [0.],\n", 261 | " [0.],\n", 262 | " [0.]])" 263 | ] 264 | }, 265 | "execution_count": 10, 266 | "metadata": {}, 267 | "output_type": "execute_result" 268 | } 269 | ], 270 | "source": [ 271 | "z.shape = (10, 1)\n", 272 | "z" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": 11, 278 | "metadata": {}, 279 | "outputs": [ 280 | { 281 | "data": { 282 | "text/plain": [ 283 | "array([[0., 0., 0., 0., 0.],\n", 284 | " [0., 0., 0., 0., 0.]])" 285 | ] 286 | }, 287 | "execution_count": 11, 288 | "metadata": {}, 289 | "output_type": "execute_result" 290 | } 291 | ], 292 | "source": [ 293 | "z.reshape(2, 5)" 294 | ] 295 | }, 296 | { 297 | "cell_type": "markdown", 298 | "metadata": {}, 299 | "source": [ 300 | "## NumPy array basics\n", 301 | "\n", 302 | "A reference sheet can be found at [QuantEcon.cheatsheets](https://cheatsheets.quantecon.org/)" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": 12, 308 | "metadata": {}, 309 | "outputs": [ 310 | { 311 | "data": { 312 | "text/plain": [ 313 | "array([1.5, 2. , 4. ])" 314 | ] 315 | }, 316 | "execution_count": 12, 317 | "metadata": {}, 318 | "output_type": "execute_result" 319 | } 320 | ], 321 | "source": [ 322 | "z = np.empty(3)\n", 323 | "z" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": 13, 329 | "metadata": {}, 330 | "outputs": [ 331 | { 332 | "data": { 333 | "text/plain": [ 334 | "array([2. , 2.5, 3. , 3.5, 4. ])" 335 | ] 336 | }, 337 | "execution_count": 13, 338 | "metadata": {}, 339 | "output_type": "execute_result" 340 | } 341 | ], 342 | "source": [ 343 | "z = np.linspace(2, 4, 5) # From 2 to 4, with 5 elements\n", 344 | "z" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": 14, 350 | "metadata": {}, 351 | "outputs": [ 352 | { 353 | "data": { 354 | "text/plain": [ 355 | "array([[1., 0.],\n", 356 | " [0., 1.]])" 357 | ] 358 | }, 359 | "execution_count": 14, 360 | "metadata": {}, 361 | "output_type": "execute_result" 362 | } 363 | ], 364 | "source": [ 365 | "z = np.identity(2)\n", 366 | "z" 367 | ] 368 | }, 369 | { 370 | "cell_type": "markdown", 371 | "metadata": {}, 372 | "source": [ 373 | "We can build arrays from lists and tuples, like so:" 374 | ] 375 | }, 376 | { 377 | "cell_type": "code", 378 | "execution_count": 15, 379 | "metadata": { 380 | "collapsed": true 381 | }, 382 | "outputs": [], 383 | "source": [ 384 | "z = np.array([10, 20])" 385 | ] 386 | }, 387 | { 388 | "cell_type": "markdown", 389 | "metadata": {}, 390 | "source": [ 391 | "2D array from list of lists:" 392 | ] 393 | }, 394 | { 395 | "cell_type": "code", 396 | "execution_count": 16, 397 | "metadata": {}, 398 | "outputs": [ 399 | { 400 | "data": { 401 | "text/plain": [ 402 | "array([[1, 2],\n", 403 | " [3, 4]])" 404 | ] 405 | }, 406 | "execution_count": 16, 407 | "metadata": {}, 408 | "output_type": "execute_result" 409 | } 410 | ], 411 | "source": [ 412 | "z = np.array([[1, 2], [3, 4]]) \n", 413 | "z" 414 | ] 415 | }, 416 | { 417 | "cell_type": "markdown", 418 | "metadata": {}, 419 | "source": [ 420 | "### Indexing" 421 | ] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "execution_count": 17, 426 | "metadata": {}, 427 | "outputs": [ 428 | { 429 | "data": { 430 | "text/plain": [ 431 | "array([1. , 1.25, 1.5 , 1.75, 2. ])" 432 | ] 433 | }, 434 | "execution_count": 17, 435 | "metadata": {}, 436 | "output_type": "execute_result" 437 | } 438 | ], 439 | "source": [ 440 | "z = np.linspace(1, 2, 5)\n", 441 | "z" 442 | ] 443 | }, 444 | { 445 | "cell_type": "code", 446 | "execution_count": 18, 447 | "metadata": {}, 448 | "outputs": [ 449 | { 450 | "data": { 451 | "text/plain": [ 452 | "1.0" 453 | ] 454 | }, 455 | "execution_count": 18, 456 | "metadata": {}, 457 | "output_type": "execute_result" 458 | } 459 | ], 460 | "source": [ 461 | "z[0]" 462 | ] 463 | }, 464 | { 465 | "cell_type": "code", 466 | "execution_count": 19, 467 | "metadata": {}, 468 | "outputs": [ 469 | { 470 | "data": { 471 | "text/plain": [ 472 | "array([1. , 1.25])" 473 | ] 474 | }, 475 | "execution_count": 19, 476 | "metadata": {}, 477 | "output_type": "execute_result" 478 | } 479 | ], 480 | "source": [ 481 | "z[0:2] # Two elements, starting at element 0 up until (but not including) 2" 482 | ] 483 | }, 484 | { 485 | "cell_type": "code", 486 | "execution_count": 20, 487 | "metadata": {}, 488 | "outputs": [ 489 | { 490 | "data": { 491 | "text/plain": [ 492 | "2.0" 493 | ] 494 | }, 495 | "execution_count": 20, 496 | "metadata": {}, 497 | "output_type": "execute_result" 498 | } 499 | ], 500 | "source": [ 501 | "z[-1] # Last element" 502 | ] 503 | }, 504 | { 505 | "cell_type": "code", 506 | "execution_count": 21, 507 | "metadata": {}, 508 | "outputs": [ 509 | { 510 | "data": { 511 | "text/plain": [ 512 | "array([1. , 1.5, 2. ])" 513 | ] 514 | }, 515 | "execution_count": 21, 516 | "metadata": {}, 517 | "output_type": "execute_result" 518 | } 519 | ], 520 | "source": [ 521 | "z[::2] # Every second element" 522 | ] 523 | }, 524 | { 525 | "cell_type": "code", 526 | "execution_count": 22, 527 | "metadata": {}, 528 | "outputs": [ 529 | { 530 | "data": { 531 | "text/plain": [ 532 | "array([[1, 2],\n", 533 | " [3, 4]])" 534 | ] 535 | }, 536 | "execution_count": 22, 537 | "metadata": {}, 538 | "output_type": "execute_result" 539 | } 540 | ], 541 | "source": [ 542 | "z = np.array([[1, 2], [3, 4]])\n", 543 | "z" 544 | ] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "execution_count": 23, 549 | "metadata": {}, 550 | "outputs": [ 551 | { 552 | "data": { 553 | "text/plain": [ 554 | "2" 555 | ] 556 | }, 557 | "execution_count": 23, 558 | "metadata": {}, 559 | "output_type": "execute_result" 560 | } 561 | ], 562 | "source": [ 563 | "z[0, 1]" 564 | ] 565 | }, 566 | { 567 | "cell_type": "markdown", 568 | "metadata": {}, 569 | "source": [ 570 | "Selecting rows and columns:" 571 | ] 572 | }, 573 | { 574 | "cell_type": "code", 575 | "execution_count": 24, 576 | "metadata": {}, 577 | "outputs": [ 578 | { 579 | "data": { 580 | "text/plain": [ 581 | "array([1, 2])" 582 | ] 583 | }, 584 | "execution_count": 24, 585 | "metadata": {}, 586 | "output_type": "execute_result" 587 | } 588 | ], 589 | "source": [ 590 | "z[0, :]" 591 | ] 592 | }, 593 | { 594 | "cell_type": "code", 595 | "execution_count": 25, 596 | "metadata": {}, 597 | "outputs": [ 598 | { 599 | "data": { 600 | "text/plain": [ 601 | "array([2, 4])" 602 | ] 603 | }, 604 | "execution_count": 25, 605 | "metadata": {}, 606 | "output_type": "execute_result" 607 | } 608 | ], 609 | "source": [ 610 | "z[:, 1]" 611 | ] 612 | }, 613 | { 614 | "cell_type": "markdown", 615 | "metadata": {}, 616 | "source": [ 617 | "### NumPy array methods\n", 618 | "\n", 619 | "Type `.` after your numpy array name and click `tab` to see available methods" 620 | ] 621 | }, 622 | { 623 | "cell_type": "code", 624 | "execution_count": 26, 625 | "metadata": {}, 626 | "outputs": [ 627 | { 628 | "data": { 629 | "text/plain": [ 630 | "array([4, 3, 2, 1])" 631 | ] 632 | }, 633 | "execution_count": 26, 634 | "metadata": {}, 635 | "output_type": "execute_result" 636 | } 637 | ], 638 | "source": [ 639 | "a = np.array((4, 3, 2, 1))\n", 640 | "a" 641 | ] 642 | }, 643 | { 644 | "cell_type": "code", 645 | "execution_count": 27, 646 | "metadata": {}, 647 | "outputs": [ 648 | { 649 | "data": { 650 | "text/plain": [ 651 | "10" 652 | ] 653 | }, 654 | "execution_count": 27, 655 | "metadata": {}, 656 | "output_type": "execute_result" 657 | } 658 | ], 659 | "source": [ 660 | "a.sum() # Sum" 661 | ] 662 | }, 663 | { 664 | "cell_type": "code", 665 | "execution_count": 28, 666 | "metadata": {}, 667 | "outputs": [ 668 | { 669 | "data": { 670 | "text/plain": [ 671 | "array([[4, 2],\n", 672 | " [3, 1]])" 673 | ] 674 | }, 675 | "execution_count": 28, 676 | "metadata": {}, 677 | "output_type": "execute_result" 678 | } 679 | ], 680 | "source": [ 681 | "a.shape = (2, 2)\n", 682 | "a.T # Equivalent to a.transpose()" 683 | ] 684 | }, 685 | { 686 | "cell_type": "markdown", 687 | "metadata": {}, 688 | "source": [ 689 | "### Arithmetic operations" 690 | ] 691 | }, 692 | { 693 | "cell_type": "markdown", 694 | "metadata": {}, 695 | "source": [ 696 | "Standard arithmetic operators act **elementwise** on arrays:" 697 | ] 698 | }, 699 | { 700 | "cell_type": "code", 701 | "execution_count": 29, 702 | "metadata": {}, 703 | "outputs": [ 704 | { 705 | "data": { 706 | "text/plain": [ 707 | "array([ 6, 8, 10, 12])" 708 | ] 709 | }, 710 | "execution_count": 29, 711 | "metadata": {}, 712 | "output_type": "execute_result" 713 | } 714 | ], 715 | "source": [ 716 | "a = np.array([1, 2, 3, 4])\n", 717 | "b = np.array([5, 6, 7, 8])\n", 718 | "a + b" 719 | ] 720 | }, 721 | { 722 | "cell_type": "code", 723 | "execution_count": 30, 724 | "metadata": {}, 725 | "outputs": [ 726 | { 727 | "data": { 728 | "text/plain": [ 729 | "array([ 5, 12, 21, 32])" 730 | ] 731 | }, 732 | "execution_count": 30, 733 | "metadata": {}, 734 | "output_type": "execute_result" 735 | } 736 | ], 737 | "source": [ 738 | "a * b" 739 | ] 740 | }, 741 | { 742 | "cell_type": "code", 743 | "execution_count": 31, 744 | "metadata": {}, 745 | "outputs": [ 746 | { 747 | "data": { 748 | "text/plain": [ 749 | "array([11, 12, 13, 14])" 750 | ] 751 | }, 752 | "execution_count": 31, 753 | "metadata": {}, 754 | "output_type": "execute_result" 755 | } 756 | ], 757 | "source": [ 758 | "a + 10" 759 | ] 760 | }, 761 | { 762 | "cell_type": "code", 763 | "execution_count": 32, 764 | "metadata": {}, 765 | "outputs": [ 766 | { 767 | "data": { 768 | "text/plain": [ 769 | "array([[2., 2.],\n", 770 | " [2., 2.]])" 771 | ] 772 | }, 773 | "execution_count": 32, 774 | "metadata": {}, 775 | "output_type": "execute_result" 776 | } 777 | ], 778 | "source": [ 779 | "A = np.ones((2, 2))\n", 780 | "B = np.ones((2, 2))\n", 781 | "A + B" 782 | ] 783 | }, 784 | { 785 | "cell_type": "code", 786 | "execution_count": 33, 787 | "metadata": {}, 788 | "outputs": [ 789 | { 790 | "data": { 791 | "text/plain": [ 792 | "array([[11., 11.],\n", 793 | " [11., 11.]])" 794 | ] 795 | }, 796 | "execution_count": 33, 797 | "metadata": {}, 798 | "output_type": "execute_result" 799 | } 800 | ], 801 | "source": [ 802 | "A + 10" 803 | ] 804 | }, 805 | { 806 | "cell_type": "code", 807 | "execution_count": 34, 808 | "metadata": { 809 | "scrolled": true 810 | }, 811 | "outputs": [ 812 | { 813 | "data": { 814 | "text/plain": [ 815 | "array([[1., 1.],\n", 816 | " [1., 1.]])" 817 | ] 818 | }, 819 | "execution_count": 34, 820 | "metadata": {}, 821 | "output_type": "execute_result" 822 | } 823 | ], 824 | "source": [ 825 | "A * B" 826 | ] 827 | }, 828 | { 829 | "cell_type": "markdown", 830 | "metadata": {}, 831 | "source": [ 832 | "As you can see, `*` is *not* matrix multiplication.\n", 833 | "\n", 834 | "Here's how you do it:" 835 | ] 836 | }, 837 | { 838 | "cell_type": "code", 839 | "execution_count": 35, 840 | "metadata": {}, 841 | "outputs": [ 842 | { 843 | "data": { 844 | "text/plain": [ 845 | "array([[2., 2.],\n", 846 | " [2., 2.]])" 847 | ] 848 | }, 849 | "execution_count": 35, 850 | "metadata": {}, 851 | "output_type": "execute_result" 852 | } 853 | ], 854 | "source": [ 855 | "A = np.ones((2, 2))\n", 856 | "B = np.ones((2, 2))\n", 857 | "A @ B" 858 | ] 859 | }, 860 | { 861 | "cell_type": "code", 862 | "execution_count": 36, 863 | "metadata": {}, 864 | "outputs": [ 865 | { 866 | "data": { 867 | "text/plain": [ 868 | "50" 869 | ] 870 | }, 871 | "execution_count": 36, 872 | "metadata": {}, 873 | "output_type": "execute_result" 874 | } 875 | ], 876 | "source": [ 877 | "A = np.array((1, 2))\n", 878 | "B = np.array((10, 20))\n", 879 | "A @ B" 880 | ] 881 | }, 882 | { 883 | "cell_type": "markdown", 884 | "metadata": {}, 885 | "source": [ 886 | "## Exercise\n", 887 | "\n", 888 | "Write a function `matrix_power` to compute $A^n$\n", 889 | "* The function should take a square array and an integer as arguments" 890 | ] 891 | }, 892 | { 893 | "cell_type": "code", 894 | "execution_count": null, 895 | "metadata": { 896 | "collapsed": true 897 | }, 898 | "outputs": [], 899 | "source": [] 900 | }, 901 | { 902 | "cell_type": "markdown", 903 | "metadata": {}, 904 | "source": [ 905 | "### Implications of mutability\n", 906 | "\n", 907 | "* NumPy arrays are mutable, ie. their contents can be changed\n", 908 | "* This has an implication which often tricks people..." 909 | ] 910 | }, 911 | { 912 | "cell_type": "code", 913 | "execution_count": 37, 914 | "metadata": {}, 915 | "outputs": [ 916 | { 917 | "data": { 918 | "text/plain": [ 919 | "array([1., 1., 1.])" 920 | ] 921 | }, 922 | "execution_count": 37, 923 | "metadata": {}, 924 | "output_type": "execute_result" 925 | } 926 | ], 927 | "source": [ 928 | "a = np.ones(3)\n", 929 | "a" 930 | ] 931 | }, 932 | { 933 | "cell_type": "markdown", 934 | "metadata": {}, 935 | "source": [ 936 | "The next statement binds `b` to the same object" 937 | ] 938 | }, 939 | { 940 | "cell_type": "code", 941 | "execution_count": 38, 942 | "metadata": { 943 | "collapsed": true 944 | }, 945 | "outputs": [], 946 | "source": [ 947 | "b = a" 948 | ] 949 | }, 950 | { 951 | "cell_type": "markdown", 952 | "metadata": {}, 953 | "source": [ 954 | "Now changing `b` mutates the data that `a` points to" 955 | ] 956 | }, 957 | { 958 | "cell_type": "code", 959 | "execution_count": 39, 960 | "metadata": {}, 961 | "outputs": [ 962 | { 963 | "data": { 964 | "text/plain": [ 965 | "array([0., 1., 1.])" 966 | ] 967 | }, 968 | "execution_count": 39, 969 | "metadata": {}, 970 | "output_type": "execute_result" 971 | } 972 | ], 973 | "source": [ 974 | "b[0] = 0.0\n", 975 | "a" 976 | ] 977 | }, 978 | { 979 | "cell_type": "code", 980 | "execution_count": 40, 981 | "metadata": {}, 982 | "outputs": [ 983 | { 984 | "data": { 985 | "text/plain": [ 986 | "True" 987 | ] 988 | }, 989 | "execution_count": 40, 990 | "metadata": {}, 991 | "output_type": "execute_result" 992 | } 993 | ], 994 | "source": [ 995 | "a is b" 996 | ] 997 | }, 998 | { 999 | "cell_type": "markdown", 1000 | "metadata": {}, 1001 | "source": [ 1002 | "How to make a separate copy when you need to\n", 1003 | "\n", 1004 | "* Note that making a copy is a more expensive operation" 1005 | ] 1006 | }, 1007 | { 1008 | "cell_type": "code", 1009 | "execution_count": 41, 1010 | "metadata": {}, 1011 | "outputs": [ 1012 | { 1013 | "data": { 1014 | "text/plain": [ 1015 | "array([1., 1., 1.])" 1016 | ] 1017 | }, 1018 | "execution_count": 41, 1019 | "metadata": {}, 1020 | "output_type": "execute_result" 1021 | } 1022 | ], 1023 | "source": [ 1024 | "a = np.ones(3)\n", 1025 | "a" 1026 | ] 1027 | }, 1028 | { 1029 | "cell_type": "code", 1030 | "execution_count": 42, 1031 | "metadata": {}, 1032 | "outputs": [ 1033 | { 1034 | "data": { 1035 | "text/plain": [ 1036 | "array([2., 1., 1.])" 1037 | ] 1038 | }, 1039 | "execution_count": 42, 1040 | "metadata": {}, 1041 | "output_type": "execute_result" 1042 | } 1043 | ], 1044 | "source": [ 1045 | "b = np.copy(a)\n", 1046 | "b[0] = 2\n", 1047 | "b" 1048 | ] 1049 | }, 1050 | { 1051 | "cell_type": "code", 1052 | "execution_count": 43, 1053 | "metadata": {}, 1054 | "outputs": [ 1055 | { 1056 | "data": { 1057 | "text/plain": [ 1058 | "array([1., 1., 1.])" 1059 | ] 1060 | }, 1061 | "execution_count": 43, 1062 | "metadata": {}, 1063 | "output_type": "execute_result" 1064 | } 1065 | ], 1066 | "source": [ 1067 | "a" 1068 | ] 1069 | }, 1070 | { 1071 | "cell_type": "markdown", 1072 | "metadata": {}, 1073 | "source": [ 1074 | "### Ufuncs\n", 1075 | "\n", 1076 | "* *Universal functions* are *vectorized functions* that act element-wise on arrays\n", 1077 | "* Instead of looping through an array and applying an operation, the operation is sent in batches to optimized C and Fortran code" 1078 | ] 1079 | }, 1080 | { 1081 | "cell_type": "code", 1082 | "execution_count": 44, 1083 | "metadata": { 1084 | "collapsed": true 1085 | }, 1086 | "outputs": [], 1087 | "source": [ 1088 | "z = np.arange(10000)\n", 1089 | "z_2 = np.empty_like(z)" 1090 | ] 1091 | }, 1092 | { 1093 | "cell_type": "code", 1094 | "execution_count": 45, 1095 | "metadata": {}, 1096 | "outputs": [ 1097 | { 1098 | "name": "stdout", 1099 | "output_type": "stream", 1100 | "text": [ 1101 | "10 loops, best of 3: 78.2 ms per loop\n" 1102 | ] 1103 | } 1104 | ], 1105 | "source": [ 1106 | "%%timeit\n", 1107 | "\n", 1108 | "for i in z:\n", 1109 | " z_2[:] = z**2" 1110 | ] 1111 | }, 1112 | { 1113 | "cell_type": "code", 1114 | "execution_count": 46, 1115 | "metadata": { 1116 | "scrolled": true 1117 | }, 1118 | "outputs": [ 1119 | { 1120 | "name": "stdout", 1121 | "output_type": "stream", 1122 | "text": [ 1123 | "The slowest run took 11.20 times longer than the fastest. This could mean that an intermediate result is being cached.\n", 1124 | "100000 loops, best of 3: 4.06 µs per loop\n" 1125 | ] 1126 | } 1127 | ], 1128 | "source": [ 1129 | "%%timeit\n", 1130 | "\n", 1131 | "z**2" 1132 | ] 1133 | }, 1134 | { 1135 | "cell_type": "markdown", 1136 | "metadata": {}, 1137 | "source": [ 1138 | "Scalar functions (`sin`, `log`, `exp`, etc.) act individually on scalars and elementwise on arrays\n", 1139 | "\n", 1140 | "A list of available ufuncs can be found [here](https://docs.scipy.org/doc/numpy-1.14.0/reference/ufuncs.html#available-ufuncs) " 1141 | ] 1142 | }, 1143 | { 1144 | "cell_type": "code", 1145 | "execution_count": 47, 1146 | "metadata": {}, 1147 | "outputs": [ 1148 | { 1149 | "data": { 1150 | "text/plain": [ 1151 | "0.8414709848078965" 1152 | ] 1153 | }, 1154 | "execution_count": 47, 1155 | "metadata": {}, 1156 | "output_type": "execute_result" 1157 | } 1158 | ], 1159 | "source": [ 1160 | "np.sin(1)" 1161 | ] 1162 | }, 1163 | { 1164 | "cell_type": "code", 1165 | "execution_count": 48, 1166 | "metadata": {}, 1167 | "outputs": [ 1168 | { 1169 | "data": { 1170 | "text/plain": [ 1171 | "array([0.84147098, 0.90929743, 0.14112001])" 1172 | ] 1173 | }, 1174 | "execution_count": 48, 1175 | "metadata": {}, 1176 | "output_type": "execute_result" 1177 | } 1178 | ], 1179 | "source": [ 1180 | "z = np.array([1, 2, 3])\n", 1181 | "np.sin(z)" 1182 | ] 1183 | }, 1184 | { 1185 | "cell_type": "markdown", 1186 | "metadata": {}, 1187 | "source": [ 1188 | "### NumPy Subpackages" 1189 | ] 1190 | }, 1191 | { 1192 | "cell_type": "markdown", 1193 | "metadata": {}, 1194 | "source": [ 1195 | "The `random` subpackage:" 1196 | ] 1197 | }, 1198 | { 1199 | "cell_type": "code", 1200 | "execution_count": 49, 1201 | "metadata": { 1202 | "collapsed": true 1203 | }, 1204 | "outputs": [], 1205 | "source": [ 1206 | "z = np.random.randn(5)" 1207 | ] 1208 | }, 1209 | { 1210 | "cell_type": "code", 1211 | "execution_count": 50, 1212 | "metadata": {}, 1213 | "outputs": [ 1214 | { 1215 | "data": { 1216 | "text/plain": [ 1217 | "5.035" 1218 | ] 1219 | }, 1220 | "execution_count": 50, 1221 | "metadata": {}, 1222 | "output_type": "execute_result" 1223 | } 1224 | ], 1225 | "source": [ 1226 | "y = np.random.binomial(10, 0.5, size=1000) \n", 1227 | "y.mean()" 1228 | ] 1229 | }, 1230 | { 1231 | "cell_type": "markdown", 1232 | "metadata": {}, 1233 | "source": [ 1234 | "The `linalg` subpackage" 1235 | ] 1236 | }, 1237 | { 1238 | "cell_type": "code", 1239 | "execution_count": 51, 1240 | "metadata": {}, 1241 | "outputs": [ 1242 | { 1243 | "data": { 1244 | "text/plain": [ 1245 | "-2.0000000000000004" 1246 | ] 1247 | }, 1248 | "execution_count": 51, 1249 | "metadata": {}, 1250 | "output_type": "execute_result" 1251 | } 1252 | ], 1253 | "source": [ 1254 | "A = np.array([[1, 2], [3, 4]])\n", 1255 | "\n", 1256 | "np.linalg.det(A) # Compute the determinant" 1257 | ] 1258 | }, 1259 | { 1260 | "cell_type": "code", 1261 | "execution_count": 52, 1262 | "metadata": {}, 1263 | "outputs": [ 1264 | { 1265 | "data": { 1266 | "text/plain": [ 1267 | "array([[-2. , 1. ],\n", 1268 | " [ 1.5, -0.5]])" 1269 | ] 1270 | }, 1271 | "execution_count": 52, 1272 | "metadata": {}, 1273 | "output_type": "execute_result" 1274 | } 1275 | ], 1276 | "source": [ 1277 | "np.linalg.inv(A) # Compute the inverse" 1278 | ] 1279 | }, 1280 | { 1281 | "cell_type": "markdown", 1282 | "metadata": {}, 1283 | "source": [ 1284 | "Solve $Ax = B$" 1285 | ] 1286 | }, 1287 | { 1288 | "cell_type": "code", 1289 | "execution_count": 53, 1290 | "metadata": {}, 1291 | "outputs": [ 1292 | { 1293 | "data": { 1294 | "text/plain": [ 1295 | "array([-5., 4.])" 1296 | ] 1297 | }, 1298 | "execution_count": 53, 1299 | "metadata": {}, 1300 | "output_type": "execute_result" 1301 | } 1302 | ], 1303 | "source": [ 1304 | "B = np.array([3, 1])\n", 1305 | "np.linalg.solve(A, B)" 1306 | ] 1307 | }, 1308 | { 1309 | "cell_type": "markdown", 1310 | "metadata": {}, 1311 | "source": [ 1312 | "Computer $A^n$" 1313 | ] 1314 | }, 1315 | { 1316 | "cell_type": "code", 1317 | "execution_count": 54, 1318 | "metadata": {}, 1319 | "outputs": [ 1320 | { 1321 | "data": { 1322 | "text/plain": [ 1323 | "array([[1069, 1558],\n", 1324 | " [2337, 3406]])" 1325 | ] 1326 | }, 1327 | "execution_count": 54, 1328 | "metadata": {}, 1329 | "output_type": "execute_result" 1330 | } 1331 | ], 1332 | "source": [ 1333 | "np.linalg.matrix_power(A, 5)" 1334 | ] 1335 | }, 1336 | { 1337 | "cell_type": "markdown", 1338 | "metadata": {}, 1339 | "source": [ 1340 | "## Exercise" 1341 | ] 1342 | }, 1343 | { 1344 | "cell_type": "markdown", 1345 | "metadata": {}, 1346 | "source": [ 1347 | "Consider a linear regression model\n", 1348 | "\n", 1349 | "$$\n", 1350 | "y = X \\beta + \\varepsilon \\quad \\quad \\varepsilon \\sim N(0, 1)\n", 1351 | "$$\n", 1352 | "\n", 1353 | "We can estimate $\\beta$ as\n", 1354 | "$$\n", 1355 | "\\hat{\\beta} = (X'X)^{-1} X' y\n", 1356 | "$$\n", 1357 | "\n", 1358 | "where $X'$ is the transpose of $X$\n", 1359 | "\n", 1360 | "Given\n", 1361 | "\n", 1362 | "$$\n", 1363 | "y =\n", 1364 | "\\begin{bmatrix}\n", 1365 | "3 \\\\\n", 1366 | "7 \\\\\n", 1367 | "10 \\\\\n", 1368 | "5 \\\\\n", 1369 | "\\end{bmatrix}\n", 1370 | "$$\n", 1371 | "\n", 1372 | "$$\n", 1373 | "X = \n", 1374 | "\\begin{bmatrix}\n", 1375 | "5 & 3 \\\\\n", 1376 | "2 & 3 \\\\\n", 1377 | "3 & 1 \\\\\n", 1378 | "2 & 8 \\\\\n", 1379 | "\\end{bmatrix}\n", 1380 | "$$\n", 1381 | "\n", 1382 | "Compute $\\hat{\\beta}$" 1383 | ] 1384 | }, 1385 | { 1386 | "cell_type": "code", 1387 | "execution_count": null, 1388 | "metadata": { 1389 | "collapsed": true 1390 | }, 1391 | "outputs": [], 1392 | "source": [] 1393 | }, 1394 | { 1395 | "cell_type": "markdown", 1396 | "metadata": {}, 1397 | "source": [ 1398 | "## Exercise" 1399 | ] 1400 | }, 1401 | { 1402 | "cell_type": "markdown", 1403 | "metadata": {}, 1404 | "source": [ 1405 | "We can represent an AR(1) model in the form\n", 1406 | "\n", 1407 | "$$\n", 1408 | "Ay = \\varepsilon \\quad \\quad \\varepsilon \\sim N(0, 1)\n", 1409 | "$$\n", 1410 | "\n", 1411 | "where $A$ is\n", 1412 | "\n", 1413 | "$$ A = \\begin{bmatrix} 1 & 0 & \\cdots & 0 & 0 \\cr\n", 1414 | " -a & 1 & \\cdots & 0 & 0 \\cr\n", 1415 | " \\vdots & \\vdots & \\cdots & \\vdots & \\vdots \\cr\n", 1416 | " \\vdots & \\vdots & \\cdots & 1 & 0 \\cr\n", 1417 | " 0 & 0 & \\cdots & -a & 1 \\end{bmatrix} $$\n", 1418 | " \n", 1419 | "and $y$ and $\\varepsilon$ are $(T x 1)$ vectors\n", 1420 | "\n", 1421 | "Generate an AR(1) series with $T=100$ and $\\alpha = 0.9$ using matrix algebra\n", 1422 | "\n", 1423 | "Hint: you will need to use `np.eye`, `np.random.randn` and `np.linalg.inv`" 1424 | ] 1425 | }, 1426 | { 1427 | "cell_type": "code", 1428 | "execution_count": null, 1429 | "metadata": { 1430 | "collapsed": true 1431 | }, 1432 | "outputs": [], 1433 | "source": [] 1434 | }, 1435 | { 1436 | "cell_type": "markdown", 1437 | "metadata": {}, 1438 | "source": [ 1439 | "### More resources\n", 1440 | "\n", 1441 | "* [QuantEcon NumPy Tutorial](https://lectures.quantecon.org/py/numpy.html)\n", 1442 | "* [QuantEcon Numerical Computing Cheatsheet](https://cheatsheets.quantecon.org/)\n", 1443 | "* [Introduction to Numerical Computing with NumPy - SciPy 2017](https://www.youtube.com/watch?v=lKcwuPnSHIQ&ab_channel=Enthought)\n", 1444 | "* [NumPy documentation](https://docs.scipy.org/doc/numpy/reference/)" 1445 | ] 1446 | }, 1447 | { 1448 | "cell_type": "code", 1449 | "execution_count": null, 1450 | "metadata": { 1451 | "collapsed": true 1452 | }, 1453 | "outputs": [], 1454 | "source": [] 1455 | } 1456 | ], 1457 | "metadata": { 1458 | "kernelspec": { 1459 | "display_name": "Python [default]", 1460 | "language": "python", 1461 | "name": "python3" 1462 | }, 1463 | "language_info": { 1464 | "codemirror_mode": { 1465 | "name": "ipython", 1466 | "version": 3 1467 | }, 1468 | "file_extension": ".py", 1469 | "mimetype": "text/x-python", 1470 | "name": "python", 1471 | "nbconvert_exporter": "python", 1472 | "pygments_lexer": "ipython3", 1473 | "version": "3.6.4" 1474 | } 1475 | }, 1476 | "nbformat": 4, 1477 | "nbformat_minor": 2 1478 | } 1479 | --------------------------------------------------------------------------------