├── .gitignore ├── LICENSE ├── README.md ├── check_env.py ├── environment.yml ├── exercises ├── calc_return │ ├── aapl_2008_close_values.csv │ ├── calc_return.ipynb │ ├── calc_return.py │ ├── calc_return_solution.ipynb │ └── calc_return_solution.py ├── dow_selection │ ├── dow.csv │ ├── dow_selection.ipynb │ ├── dow_selection.py │ ├── dow_selection_solution.ipynb │ └── dow_selection_solution.py ├── filter_image │ ├── dc_metro.png │ ├── filter_image.ipynb │ ├── filter_image.py │ ├── filter_image_solution.ipynb │ └── filter_image_solution.py ├── load_text │ ├── complex_data_file.txt │ ├── float_data.txt │ ├── float_data_with_header.txt │ ├── load_text.ipynb │ ├── load_text.py │ ├── load_text_solution.ipynb │ └── load_text_solution.py ├── sinc_function │ ├── sinc_function.ipynb │ ├── sinc_function.py │ ├── sinc_function_solution.ipynb │ └── sinc_function_solution.py └── wind_statistics │ ├── wind.data │ ├── wind.desc │ ├── wind_statistics.ipynb │ ├── wind_statistics.py │ ├── wind_statistics_solution.ipynb │ └── wind_statistics_solution.py ├── history ├── session1.txt ├── session2.txt ├── session3.txt └── session4.txt └── introduction_to_numerical_computing_with_numpy_manual.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | © 2001-2021, Enthought, Inc. 2 | All Rights Reserved. Use only permitted under license. Copying, sharing, redistributing or other unauthorized use strictly prohibited. 3 | All trademarks and registered trademarks are the property of their respective owners. 4 | Enthought, Inc. 5 | 200 W Cesar Chavez Suite 202 6 | Austin, TX 78701 7 | www.enthought.com 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SciPy 2021 Tutorial: Introduction to Numerical Computing With NumPy 2 | 3 | #### Presented by: Logan Thomas, [Enthought, Inc.](https://www.enthought.com) 4 | #### YouTube recording of live tutorial [here](https://youtu.be/8L1MgStSZhk) 5 | 6 | This repository contains all the material needed by students registered for the Numpy tutorial of SciPy 2021 on Monday, July 12th 2021. 7 | 8 | For a smooth experience, you will need to make sure that you install or update your Python distribution and download the tutorial material _before_ the day of the tutorial. 9 | 10 | ## Running the Exercises the (recommended) Easy Way 11 | 12 | Run with Binder by clicking this icon: [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/enthought/Numpy-Tutorial-SciPyConf-2021/main) 13 | 14 | 15 | ## Running the Exercise Locally 16 | 17 | ### Install Python 18 | 19 | If you don't already have a working python distribution, you may download Anaconda Python ([https://www.anaconda.com/products/individual](https://www.anaconda.com/products/individual)). 20 | 21 | 22 | ### Install Packages 23 | 24 | To be able to run the examples, demos and exercises, you must have the following packages installed: 25 | 26 | - ipython 7.16+ (for running, experimenting and doing exercises) 27 | - jupyter 1.0+ 28 | - matplotlib 3.3+ 29 | - numpy 1.17+ 30 | - pillow 7.2+ 31 | - pyqt 5.9+ 32 | 33 | If you are using Anaconda, you can use the Anaconda Prompt (Windows) or Terminal.app (macOS) to create an environment with the necessary packages: 34 | 35 | 1. Open the Anaconda Prompt or Terminal.app using the below instructions: 36 | - **Windows**: Click Start and search for "Anaconda Prompt". Click on the application to launch a new Anaconda Prompt window. 37 | - **macOS**: Open Spotlight Search (using Cmd+Space) and type "Terminal.app". Click on the application to launch a new Terminal.app window. 38 | 39 | 2. Create a new Anaconda virtual environment by executing the below command in the application window you opened in step 1 above. 40 | 41 | ``` 42 | $ conda create -n numpy-tutorial ipython jupyter matplotlib numpy pillow pyqt 43 | ``` 44 | 45 | 3. To test your installation, please execute the `check_env.py` script in the environment where you have installed the requirements. If you created an Anaconda environment using the instructions above, keep the application window that you opened in step 1 active (or launch the platform specific application again -- Anaconda Prompt for Windows or Terminal.app for macOS), navigate to where you have this GitHub repository, and type: 46 | 47 | ``` 48 | $ conda activate numpy-tutorial 49 | $ python check_env.py 50 | ``` 51 | 52 | You should see a window pop up with a plot that looks vaguely like a smiley face. 53 | 54 | ## Download Tutorial Materials 55 | 56 | This GitHub repository is all that is needed in terms of tutorial content. The simplest solution is to download the material using this link: 57 | 58 | https://github.com/enthought/Numpy-Tutorial-SciPyConf-2021/archive/main.zip 59 | 60 | If you are familiar with Git, you can also clone this repository with: 61 | 62 | ``` 63 | $ git clone https://github.com/enthought/Numpy-Tutorial-SciPyConf-2021.git 64 | ``` 65 | 66 | It will create a new folder named `Numpy-Tutorial-SciPyConf-2021/` with all the content you will need: the slides I will go through (`slides.pdf`), and a folder of exercises. 67 | 68 | 69 | ## Questions? Problems? 70 | 71 | You may post messages to the `#tutorial-intro-to-numerical-computing-with-numpy` Slack channel for this tutorial at in the official Slack team: [https://scipy2021.slack.com](https://scipy2021.slack.com) . 72 | -------------------------------------------------------------------------------- /check_env.py: -------------------------------------------------------------------------------- 1 | """ Run this file to check your python installation. 2 | """ 3 | import matplotlib.pyplot as plt 4 | import numpy as np 5 | 6 | assert np.allclose(np.array([3.3], dtype="float32"), np.array([1.65], dtype="float32") * 2) 7 | fig, ax = plt.subplots() 8 | ax.scatter(x=[-3, -2, -1, 0, 1, 2, 3], y=[0, -1, -1.5, -1.75, -1.5, -1, 0]) 9 | ax.scatter(x=[-1.5, 1.5], y=[2, 2], s=1000) 10 | ax.set_ylim((-3, 3)) 11 | plt.show() 12 | -------------------------------------------------------------------------------- /environment.yml: -------------------------------------------------------------------------------- 1 | name: numpy-tutorial 2 | dependencies: 3 | - ipython 4 | - jupyter 5 | - matplotlib 6 | - numpy 7 | - pillow 8 | - pyqt 9 | 10 | -------------------------------------------------------------------------------- /exercises/calc_return/aapl_2008_close_values.csv: -------------------------------------------------------------------------------- 1 | 2008-01-02,194.84 2 | 2008-01-03,194.93 3 | 2008-01-04,180.05 4 | 2008-01-07,177.64 5 | 2008-01-08,171.25 6 | 2008-01-09,179.4 7 | 2008-01-10,178.02 8 | 2008-01-11,172.69 9 | 2008-01-14,178.78 10 | 2008-01-15,169.04 11 | 2008-01-16,159.64 12 | 2008-01-17,160.89 13 | 2008-01-18,161.36 14 | 2008-01-22,155.64 15 | 2008-01-23,139.07 16 | 2008-01-24,135.6 17 | 2008-01-25,130.01 18 | 2008-01-28,130.01 19 | 2008-01-29,131.54 20 | 2008-01-30,132.18 21 | 2008-01-31,135.36 22 | 2008-02-01,133.75 23 | 2008-02-04,131.65 24 | 2008-02-05,129.36 25 | 2008-02-06,122.0 26 | 2008-02-07,121.24 27 | 2008-02-08,125.48 28 | 2008-02-11,129.45 29 | 2008-02-12,124.86 30 | 2008-02-13,129.4 31 | 2008-02-14,127.46 32 | 2008-02-15,124.63 33 | 2008-02-19,122.18 34 | 2008-02-20,123.82 35 | 2008-02-21,121.54 36 | 2008-02-22,119.46 37 | 2008-02-25,119.74 38 | 2008-02-26,119.15 39 | 2008-02-27,122.96 40 | 2008-02-28,129.91 41 | 2008-02-29,125.02 42 | 2008-03-03,121.73 43 | 2008-03-04,124.62 44 | 2008-03-05,124.49 45 | 2008-03-06,120.93 46 | 2008-03-07,122.25 47 | 2008-03-10,119.69 48 | 2008-03-11,127.35 49 | 2008-03-12,126.03 50 | 2008-03-13,127.94 51 | 2008-03-14,126.61 52 | 2008-03-17,126.73 53 | 2008-03-18,132.82 54 | 2008-03-19,129.67 55 | 2008-03-20,133.27 56 | 2008-03-24,139.53 57 | 2008-03-25,140.98 58 | 2008-03-26,145.06 59 | 2008-03-27,140.25 60 | 2008-03-28,143.01 61 | 2008-03-31,143.5 62 | 2008-04-01,149.53 63 | 2008-04-02,147.49 64 | 2008-04-03,151.61 65 | 2008-04-04,153.08 66 | 2008-04-07,155.89 67 | 2008-04-08,152.84 68 | 2008-04-09,151.44 69 | 2008-04-10,154.55 70 | 2008-04-11,147.14 71 | 2008-04-14,147.78 72 | 2008-04-15,148.38 73 | 2008-04-16,153.7 74 | 2008-04-17,154.49 75 | 2008-04-18,161.04 76 | 2008-04-21,168.16 77 | 2008-04-22,160.2 78 | 2008-04-23,162.89 79 | 2008-04-24,168.94 80 | 2008-04-25,169.73 81 | 2008-04-28,172.24 82 | 2008-04-29,175.05 83 | 2008-04-30,173.95 84 | 2008-05-01,180.0 85 | 2008-05-02,180.94 86 | 2008-05-05,184.73 87 | 2008-05-06,186.66 88 | 2008-05-07,182.59 89 | 2008-05-08,185.06 90 | 2008-05-09,183.45 91 | 2008-05-12,188.16 92 | 2008-05-13,189.96 93 | 2008-05-14,186.26 94 | 2008-05-15,189.73 95 | 2008-05-16,187.62 96 | 2008-05-19,183.6 97 | 2008-05-20,185.9 98 | 2008-05-21,178.19 99 | 2008-05-22,177.05 100 | 2008-05-23,181.17 101 | 2008-05-27,186.43 102 | 2008-05-28,187.01 103 | 2008-05-29,186.69 104 | 2008-05-30,188.75 105 | 2008-06-02,186.1 106 | 2008-06-03,185.37 107 | 2008-06-04,185.19 108 | 2008-06-05,189.43 109 | 2008-06-06,185.64 110 | 2008-06-09,181.61 111 | 2008-06-10,185.64 112 | 2008-06-11,180.81 113 | 2008-06-12,173.26 114 | 2008-06-13,172.37 115 | 2008-06-16,176.84 116 | 2008-06-17,181.43 117 | 2008-06-18,178.75 118 | 2008-06-19,180.9 119 | 2008-06-20,175.27 120 | 2008-06-23,173.16 121 | 2008-06-24,173.25 122 | 2008-06-25,177.39 123 | 2008-06-26,168.26 124 | 2008-06-27,170.09 125 | 2008-06-30,167.44 126 | 2008-07-01,174.68 127 | 2008-07-02,168.18 128 | 2008-07-03,170.12 129 | 2008-07-07,175.16 130 | 2008-07-08,179.55 131 | 2008-07-09,174.25 132 | 2008-07-10,176.63 133 | 2008-07-11,172.58 134 | 2008-07-14,173.88 135 | 2008-07-15,169.64 136 | 2008-07-16,172.81 137 | 2008-07-17,171.81 138 | 2008-07-18,165.15 139 | 2008-07-21,166.29 140 | 2008-07-22,162.02 141 | 2008-07-23,166.26 142 | 2008-07-24,159.03 143 | 2008-07-25,162.12 144 | 2008-07-28,154.4 145 | 2008-07-29,157.08 146 | 2008-07-30,159.88 147 | 2008-07-31,158.95 148 | 2008-08-01,156.66 149 | 2008-08-04,153.23 150 | 2008-08-05,160.64 151 | 2008-08-06,164.19 152 | 2008-08-07,163.57 153 | 2008-08-08,169.55 154 | 2008-08-11,173.56 155 | 2008-08-12,176.73 156 | 2008-08-13,179.3 157 | 2008-08-14,179.32 158 | 2008-08-15,175.74 159 | 2008-08-18,175.39 160 | 2008-08-19,173.53 161 | 2008-08-20,175.84 162 | 2008-08-21,174.29 163 | 2008-08-22,176.79 164 | 2008-08-25,172.55 165 | 2008-08-26,173.64 166 | 2008-08-27,174.67 167 | 2008-08-28,173.74 168 | 2008-08-29,169.53 169 | 2008-09-02,166.19 170 | 2008-09-03,166.96 171 | 2008-09-04,161.22 172 | 2008-09-05,160.18 173 | 2008-09-08,157.92 174 | 2008-09-09,151.68 175 | 2008-09-10,151.61 176 | 2008-09-11,152.65 177 | 2008-09-12,148.94 178 | 2008-09-15,140.36 179 | 2008-09-16,139.88 180 | 2008-09-17,127.83 181 | 2008-09-18,134.09 182 | 2008-09-19,140.91 183 | 2008-09-22,131.05 184 | 2008-09-23,126.84 185 | 2008-09-24,128.71 186 | 2008-09-25,131.93 187 | 2008-09-26,128.24 188 | 2008-09-29,105.26 189 | 2008-09-30,113.66 190 | 2008-10-01,109.12 191 | 2008-10-02,100.1 192 | 2008-10-03,97.07 193 | 2008-10-06,98.14 194 | 2008-10-07,89.16 195 | 2008-10-08,89.79 196 | 2008-10-09,88.74 197 | 2008-10-10,96.8 198 | 2008-10-13,110.26 199 | 2008-10-14,104.08 200 | 2008-10-15,97.95 201 | 2008-10-16,101.89 202 | 2008-10-17,97.4 203 | 2008-10-20,98.44 204 | 2008-10-21,91.49 205 | 2008-10-22,96.87 206 | 2008-10-23,98.23 207 | 2008-10-24,96.38 208 | 2008-10-27,92.09 209 | 2008-10-28,99.91 210 | 2008-10-29,104.55 211 | 2008-10-30,111.04 212 | 2008-10-31,107.59 213 | 2008-11-03,106.96 214 | 2008-11-04,110.99 215 | 2008-11-05,103.3 216 | 2008-11-06,99.1 217 | 2008-11-07,98.24 218 | 2008-11-10,95.88 219 | 2008-11-11,94.77 220 | 2008-11-12,90.12 221 | 2008-11-13,96.44 222 | 2008-11-14,90.24 223 | 2008-11-17,88.14 224 | 2008-11-18,89.91 225 | 2008-11-19,86.29 226 | 2008-11-20,80.49 227 | 2008-11-21,82.58 228 | 2008-11-24,92.95 229 | 2008-11-25,90.8 230 | 2008-11-26,95.0 231 | 2008-11-28,92.67 232 | 2008-12-01,88.93 233 | 2008-12-02,92.47 234 | 2008-12-03,95.9 235 | 2008-12-04,91.41 236 | 2008-12-05,94.0 237 | 2008-12-08,99.72 238 | 2008-12-09,100.06 239 | 2008-12-10,98.21 240 | 2008-12-11,95.0 241 | 2008-12-12,98.27 242 | 2008-12-15,94.75 243 | 2008-12-16,95.43 244 | 2008-12-17,89.16 245 | 2008-12-18,89.43 246 | 2008-12-19,90.0 247 | 2008-12-22,85.74 248 | 2008-12-23,86.38 249 | 2008-12-24,85.04 250 | 2008-12-26,85.81 251 | 2008-12-29,86.61 252 | 2008-12-30,86.29 253 | 2008-12-31,85.35 254 | -------------------------------------------------------------------------------- /exercises/calc_return/calc_return.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Calc Return\n", 8 | "\n", 9 | "For a given stock, the return `ret(t)` is connected to its close price `p(t)` by\n", 10 | "\n", 11 | " \n", 12 | "$\\large ret(t) = \\frac{p(t) - p(t-1)}{p(t-1)} $\n", 13 | "\n", 14 | "\n", 15 | "The close price for Apple stock for all business days in 2008 is loaded for you\n", 16 | "from the data file `aapl_2008_close_values.csv`.\n", 17 | "\n", 18 | "1. Use these values to compute the corresponding daily return for every business day of that year (except the first one).\n", 19 | "\n", 20 | "2. Plot these returns, converted to percentages, over the course of the year. On the same plot, draw a red line at 0.\n", 21 | "\n", 22 | "**Note**: a for loop is neither necessary nor recommended for this calculation\n", 23 | "\n", 24 | "## Bonus\n", 25 | "\n", 26 | "3. There is some blank space in the plot made in question 2 because by default, matplotlib displays plots with a range along the x axis that is larger than the highest x coordinate. Use IPython to learn about matplotlib's `plt.xlim` function and make the limits of your plot tighter." 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "%matplotlib inline\n", 36 | "from numpy import arange, loadtxt, zeros, diff\n", 37 | "import matplotlib.pyplot as plt" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "0. The close price for Apple stock for all business days in 2008 is loaded for you from the data file `aapl_2008_close_values.csv.`" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": null, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "prices = loadtxt(\"aapl_2008_close_values.csv\", usecols=[1], delimiter=\",\")\n", 54 | "\n", 55 | "print(\"Prices for AAPL stock in 2008:\")\n", 56 | "print(prices)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "1. Use these values (`prices`) to compute the corresponding daily return for every business day of that year (except the first one)." 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [ 72 | "# Your code goes here" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "2. Plot these returns, converted to percentages, over the course of the year. On the same plot, draw a red line at 0.\n", 80 | " - The matplotlib code for plotting has been provided for you." 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": null, 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [ 89 | "# Create an array of zeros that corresponds to the length of daily returns\n", 90 | "days = # Your code goes here\n", 91 | "zero_line = # Your code goes here" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": null, 97 | "metadata": {}, 98 | "outputs": [], 99 | "source": [ 100 | "# Plot the zero line acrros days\n", 101 | "plt.plot(days, zero_line, 'r-')\n", 102 | "\n", 103 | "# Plot the daily returns as a percentage\n", 104 | "plt.plot(_____, _____, 'b-')\n", 105 | "\n", 106 | "# Specify a plot title\n", 107 | "plt.title('Daily return of AAPL stock in 2008 (%)');" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "## Bonus\n", 115 | "3. There is some blank space in the plot made in question 2 because by default, `matplotlib` displays plots with a range along the x axis that is larger than the highest x coordinate. Use IPython to learn about `matplotlib`'s `plt.xlim` function and make the limits of your plot tighter." 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "# Plot the daily returns with xlim\n", 125 | "plt.plot(days, zero_line, 'r-', days, returns * 100 , 'b-')\n", 126 | "plt.title('Daily return of AAPL stock in 2008 (%)');\n", 127 | "plt.xlim(____);" 128 | ] 129 | } 130 | ], 131 | "metadata": { 132 | "kernelspec": { 133 | "display_name": "Python 3", 134 | "language": "python", 135 | "name": "python3" 136 | }, 137 | "language_info": { 138 | "codemirror_mode": { 139 | "name": "ipython", 140 | "version": 3 141 | }, 142 | "file_extension": ".py", 143 | "mimetype": "text/x-python", 144 | "name": "python", 145 | "nbconvert_exporter": "python", 146 | "pygments_lexer": "ipython3", 147 | "version": "3.6.13" 148 | } 149 | }, 150 | "nbformat": 4, 151 | "nbformat_minor": 4 152 | } 153 | -------------------------------------------------------------------------------- /exercises/calc_return/calc_return.py: -------------------------------------------------------------------------------- 1 | """ 2 | Calc return 3 | =========== 4 | 5 | For a given stock, the return is connected to its close price p by 6 | 7 | p(t) - p(t-1) 8 | ret(t) = ------------- 9 | p(t-1) 10 | 11 | The close price for Apple stock for all business days in 2008 is loaded for you 12 | from the data file `aapl_2008_close_values.csv`. 13 | 14 | 1. Use these values to compute the corresponding daily return for every 15 | business day of that year (except the first one). 16 | 17 | 2. Plot these returns, converted to percentages, over the course of the year. 18 | On the same plot, draw a red line at 0. 19 | 20 | Note: a for loop is neither necessary nor recommended for this calculation 21 | 22 | Bonus 23 | ~~~~~ 24 | 3. There is some blank space in the plot made in question 2 because by default, 25 | matplotlib displays plots with a range along the x axis that is larger than the 26 | highest x coordinate. Use IPython to learn about matplotlib's `plt.xlim` 27 | function and make the limits of your plot tighter. 28 | """ 29 | from numpy import arange, loadtxt, zeros 30 | import matplotlib.pyplot as plt 31 | 32 | prices = loadtxt("aapl_2008_close_values.csv", usecols=[1], delimiter=",") 33 | 34 | print("Prices for AAPL stock in 2008:") 35 | print(prices) 36 | -------------------------------------------------------------------------------- /exercises/calc_return/calc_return_solution.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Calc Return Solution\n", 8 | "\n", 9 | "For a given stock, the return `ret(t)` is connected to its close price `p(t)` by\n", 10 | "\n", 11 | " \n", 12 | "$\\large ret(t) = \\frac{p(t) - p(t-1)}{p(t-1)} $\n", 13 | "\n", 14 | "\n", 15 | "The close price for Apple stock for all business days in 2008 is loaded for you\n", 16 | "from the data file `aapl_2008_close_values.csv`.\n", 17 | "\n", 18 | "1. Use these values to compute the corresponding daily return for every business day of that year (except the first one).\n", 19 | "\n", 20 | "2. Plot these returns, converted to percentages, over the course of the year. On the same plot, draw a red line at 0.\n", 21 | "\n", 22 | "**Note**: a for loop is neither necessary nor recommended for this calculation\n", 23 | "\n", 24 | "## Bonus\n", 25 | "\n", 26 | "3. There is some blank space in the plot made in question 2 because by default, matplotlib displays plots with a range along the x axis that is larger than the highest x coordinate. Use IPython to learn about matplotlib's `plt.xlim` function and make the limits of your plot tighter." 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "%matplotlib inline\n", 36 | "from numpy import arange, loadtxt, zeros, diff\n", 37 | "import matplotlib.pyplot as plt" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "0. The close price for Apple stock for all business days in 2008 is loaded for you from the data file `aapl_2008_close_values.csv.`" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": null, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "prices = loadtxt(\"aapl_2008_close_values.csv\", usecols=[1], delimiter=\",\")\n", 54 | "\n", 55 | "print(\"Prices for AAPL stock in 2008:\")\n", 56 | "print(prices)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "1. Use these values (`prices`) to compute the corresponding daily return for every business day of that year (except the first one)." 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [ 72 | "# 1a. Compute the daily returns\n", 73 | "diffs = prices[1:] - prices[:-1]\n", 74 | "returns = diffs / prices[:-1]" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [ 83 | "# 1b. Compute the daily returns\n", 84 | "# import numpy as np\n", 85 | "# returns = np.diff(prices) / prices[:-1]" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "2. Plot these returns, converted to percentages, over the course of the year. On the same plot, draw a red line at 0.\n", 93 | " - The matplotlib code for plotting has been provided for you." 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "# Create an array of zeros that corresponds to the length of daily returns\n", 103 | "days = arange(len(returns))\n", 104 | "zero_line = zeros(len(returns))" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "# Plot the zero line acrros days\n", 114 | "plt.plot(days, zero_line, 'r-')\n", 115 | "\n", 116 | "# Plot the daily returns as a percentage\n", 117 | "plt.plot(days, returns * 100, 'b-')\n", 118 | "\n", 119 | "# Specify a plot title\n", 120 | "plt.title('Daily return of AAPL stock in 2008 (%)');" 121 | ] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "metadata": {}, 126 | "source": [ 127 | "## Bonus\n", 128 | "3. There is some blank space in the plot made in question 2 because by default, `matplotlib` displays plots with a range along the x axis that is larger than the highest x coordinate. Use IPython to learn about `matplotlib`'s `plt.xlim` function and make the limits of your plot tighter." 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [ 137 | "# Plot the daily returns with xlim\n", 138 | "plt.plot(days, zero_line, 'r-', days, returns * 100 , 'b-')\n", 139 | "plt.title('Daily return of AAPL stock in 2008 (%)');\n", 140 | "plt.xlim(xmax=len(returns));" 141 | ] 142 | } 143 | ], 144 | "metadata": { 145 | "kernelspec": { 146 | "display_name": "Python 3", 147 | "language": "python", 148 | "name": "python3" 149 | }, 150 | "language_info": { 151 | "codemirror_mode": { 152 | "name": "ipython", 153 | "version": 3 154 | }, 155 | "file_extension": ".py", 156 | "mimetype": "text/x-python", 157 | "name": "python", 158 | "nbconvert_exporter": "python", 159 | "pygments_lexer": "ipython3", 160 | "version": "3.6.13" 161 | } 162 | }, 163 | "nbformat": 4, 164 | "nbformat_minor": 4 165 | } 166 | -------------------------------------------------------------------------------- /exercises/calc_return/calc_return_solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | Calc return 3 | =========== 4 | 5 | For a given stock, the return is connected to its close price p by 6 | 7 | p(t) - p(t-1) 8 | ret(t) = ------------- 9 | p(t-1) 10 | 11 | The close price for Apple stock for all business days in 2008 is loaded for you 12 | from the data file `aapl_2008_close_values.csv`. 13 | 14 | 1. Use these values to compute the corresponding daily return for every 15 | business day of that year (except the first one). 16 | 17 | 2. Plot these returns, converted to percentages, over the course of the year. 18 | On the same plot, draw a red line at 0. 19 | 20 | Note: a for loop is neither necessary nor recommended for this calculation 21 | 22 | Bonus 23 | ~~~~~ 24 | 3. There is some blank space in the plot made in question 2 because by default, 25 | matplotlib displays plots with a range along the x axis that is larger than the 26 | highest x coordinate. Use IPython to learn about matplotlib's `plt.xlim` 27 | function and make the limits of your plot tighter. 28 | """ 29 | from numpy import arange, loadtxt, zeros 30 | import matplotlib.pyplot as plt 31 | 32 | prices = loadtxt("aapl_2008_close_values.csv", usecols=[1], delimiter=",") 33 | 34 | print("Prices for AAPL stock in 2008:") 35 | print(prices) 36 | 37 | # 1. Compute the daily returns 38 | diffs = prices[1:] - prices[:-1] 39 | returns = diffs / prices[:-1] 40 | 41 | # 2. Creating the line of 0 return 42 | days = arange(len(returns)) 43 | zero_line = zeros(len(returns)) 44 | 45 | plt.plot(days, zero_line, 'r-', days, returns * 100, 'b-') 46 | plt.title("Daily return of the AAPL stock in 2008 (%)") 47 | 48 | # 3. Make the plot tighter 49 | plt.xlim(xmax=len(returns)) 50 | plt.show() 51 | -------------------------------------------------------------------------------- /exercises/dow_selection/dow.csv: -------------------------------------------------------------------------------- 1 | 13261.82,13338.23,12969.42,13043.96,3452650000,13043.96 2 | 13044.12,13197.43,12968.44,13056.72,3429500000,13056.72 3 | 13046.56,13049.65,12740.51,12800.18,4166000000,12800.18 4 | 12801.15,12984.95,12640.44,12827.49,4221260000,12827.49 5 | 12820.9,12998.11,12511.03,12589.07,4705390000,12589.07 6 | 12590.21,12814.97,12431.53,12735.31,5351030000,12735.31 7 | 12733.11,12931.29,12632.15,12853.09,5170490000,12853.09 8 | 12850.74,12863.34,12495.91,12606.3,4495840000,12606.3 9 | 12613.78,12866.1,12596.95,12778.15,3682090000,12778.15 10 | 12777.5,12777.5,12425.92,12501.11,4601640000,12501.11 11 | 12476.81,12699.05,12294.48,12466.16,5440620000,12466.16 12 | 12467.05,12597.85,12089.38,12159.21,5303130000,12159.21 13 | 12159.94,12441.85,11953.71,12099.3,6004840000,12099.3 14 | 12092.72,12167.42,11508.74,11971.19,6544690000,11971.19 15 | 11969.08,12339.1,11530.12,12270.17,3241680000,12270.17 16 | 12272.69,12522.82,12114.83,12378.61,5735300000,12378.61 17 | 12391.7,12590.69,12103.61,12207.17,4882250000,12207.17 18 | 12205.71,12423.81,12061.42,12383.89,4100930000,12383.89 19 | 12385.19,12604.92,12262.29,12480.3,4232960000,12480.3 20 | 12480.14,12715.96,12311.55,12442.83,4742760000,12442.83 21 | 12438.28,12734.74,12197.09,12650.36,4970290000,12650.36 22 | 12638.17,12841.88,12510.05,12743.19,4650770000,12743.19 23 | 12743.11,12810.34,12557.61,12635.16,3495780000,12635.16 24 | 12631.85,12631.85,12234.97,12265.13,4315740000,12265.13 25 | 12257.25,12436.33,12142.14,12200.1,4008120000,12200.1 26 | 12196.2,12366.99,12045,12247,4589160000,12247 27 | 12248.47,12330.97,12058.01,12182.13,3768490000,12182.13 28 | 12181.89,12332.76,12006.79,12240.01,3593140000,12240.01 29 | 12241.56,12524.12,12207.9,12373.41,4044640000,12373.41 30 | 12368.12,12627.76,12354.22,12552.24,3856420000,12552.24 31 | 12551.51,12611.26,12332.03,12376.98,3644760000,12376.98 32 | 12376.66,12441.2,12216.68,12348.21,3583300000,12348.21 33 | 12349.59,12571.11,12276.81,12337.22,3613550000,12337.22 34 | 12333.31,12489.29,12159.42,12427.26,3870520000,12427.26 35 | 12426.85,12545.79,12225.36,12284.3,3696660000,12284.3 36 | 12281.09,12429.05,12116.92,12381.02,3572660000,12381.02 37 | 12380.77,12612.47,12292.03,12570.22,3866350000,12570.22 38 | 12569.48,12771.14,12449.08,12684.92,4096060000,12684.92 39 | 12683.54,12815.59,12527.64,12694.28,3904700000,12694.28 40 | 12689.28,12713.99,12463.32,12582.18,3938580000,12582.18 41 | 12579.58,12579.58,12210.3,12266.39,4426730000,12266.39 42 | 12264.36,12344.71,12101.29,12258.9,4117570000,12258.9 43 | 12259.14,12291.22,11991.06,12213.8,4757180000,12213.8 44 | 12204.93,12392.74,12105.36,12254.99,4277710000,12254.99 45 | 12254.59,12267.86,12010.03,12040.39,4323460000,12040.39 46 | 12039.09,12131.33,11778.66,11893.69,4565410000,11893.69 47 | 11893.04,11993.75,11691.47,11740.15,4261240000,11740.15 48 | 11741.33,12205.98,11741.33,12156.81,5109080000,12156.81 49 | 12148.61,12360.58,12037.79,12110.24,4414280000,12110.24 50 | 12096.49,12242.29,11832.88,12145.74,5073360000,12145.74 51 | 12146.39,12249.86,11781.43,11951.09,5153780000,11951.09 52 | 11946.45,12119.69,11650.44,11972.25,5683010000,11972.25 53 | 11975.92,12411.63,11975.92,12392.66,5335630000,12392.66 54 | 12391.52,12525.19,12077.27,12099.66,1203830000,12099.66 55 | 12102.43,12434.34,12024.68,12361.32,6145220000,12361.32 56 | 12361.97,12687.61,12346.17,12548.64,4499000000,12548.64 57 | 12547.34,12639.82,12397.62,12532.6,4145120000,12532.6 58 | 12531.79,12531.79,12309.62,12422.86,4055670000,12422.86 59 | 12421.88,12528.13,12264.76,12302.46,4037930000,12302.46 60 | 12303.92,12441.67,12164.22,12216.4,3686980000,12216.4 61 | 12215.92,12384.84,12095.18,12262.89,4188990000,12262.89 62 | 12266.64,12693.93,12266.64,12654.36,4745120000,12654.36 63 | 12651.67,12790.28,12488.22,12608.92,4320440000,12608.92 64 | 12604.69,12734.97,12455.04,12626.03,3920100000,12626.03 65 | 12626.35,12738.3,12489.4,12609.42,3703100000,12609.42 66 | 12612.59,12786.83,12550.22,12612.43,3747780000,12612.43 67 | 12602.66,12664.38,12440.55,12576.44,3602500000,12576.44 68 | 12574.65,12686.93,12416.53,12527.26,3556670000,12527.26 69 | 12526.78,12705.9,12447.96,12581.98,3686150000,12581.98 70 | 12579.78,12579.78,12280.89,12325.42,3723790000,12325.42 71 | 12324.77,12430.86,12208.42,12302.06,3565020000,12302.06 72 | 12303.6,12459.36,12223.97,12362.47,3581230000,12362.47 73 | 12371.51,12670.56,12371.51,12619.27,4260370000,12619.27 74 | 12617.4,12725.93,12472.71,12620.49,3713880000,12620.49 75 | 12626.76,12965.47,12626.76,12849.36,4222380000,12849.36 76 | 12850.91,12902.69,12666.08,12825.02,3420570000,12825.02 77 | 12825.02,12870.86,12604.53,12720.23,3821900000,12720.23 78 | 12721.45,12883.8,12627,12763.22,4103610000,12763.22 79 | 12764.68,12979.88,12651.51,12848.95,4461660000,12848.95 80 | 12848.38,12987.29,12703.7,12891.86,3891150000,12891.86 81 | 12890.76,13015.62,12791.55,12871.75,3607000000,12871.75 82 | 12870.37,12970.27,12737.82,12831.94,3815320000,12831.94 83 | 12831.45,13052.91,12746.45,12820.13,4508890000,12820.13 84 | 12818.34,13079.94,12721.94,13010,4448780000,13010 85 | 13012.53,13191.49,12931.35,13058.2,3953030000,13058.2 86 | 13056.57,13105.75,12896.5,12969.54,3410090000,12969.54 87 | 12968.89,13071.07,12817.53,13020.83,3924100000,13020.83 88 | 13010.82,13097.77,12756.14,12814.35,4075860000,12814.35 89 | 12814.84,12965.95,12727.56,12866.78,3827550000,12866.78 90 | 12860.68,12871.75,12648.09,12745.88,3518620000,12745.88 91 | 12768.38,12903.33,12746.36,12876.05,3370630000,12876.05 92 | 12872.08,12957.65,12716.16,12832.18,4018590000,12832.18 93 | 12825.12,13037.44,12806.21,12898.38,3979370000,12898.38 94 | 12891.29,13028.16,12798.39,12992.66,3836480000,12992.66 95 | 12992.74,13069.52,12860.6,12986.8,3842590000,12986.8 96 | 12985.41,13170.97,12899.19,13028.16,3683970000,13028.16 97 | 13026.04,13026.04,12742.29,12828.68,3854320000,12828.68 98 | 12824.94,12926.71,12550.39,12601.19,4517990000,12601.19 99 | 12597.69,12743.68,12515.78,12625.62,3955960000,12625.62 100 | 12620.9,12637.43,12420.2,12479.63,3516380000,12479.63 101 | 12479.63,12626.84,12397.56,12548.35,3588860000,12548.35 102 | 12542.9,12693.77,12437.38,12594.03,3927240000,12594.03 103 | 12593.87,12760.21,12493.47,12646.22,3894440000,12646.22 104 | 12647.36,12750.84,12555.6,12638.32,3845630000,12638.32 105 | 12637.67,12645.4,12385.76,12503.82,3714320000,12503.82 106 | 12503.2,12620.98,12317.61,12402.85,4396380000,12402.85 107 | 12391.86,12540.37,12283.74,12390.48,4338640000,12390.48 108 | 12388.81,12652.81,12358.07,12604.45,4350790000,12604.45 109 | 12602.74,12602.74,12180.5,12209.81,4771660000,12209.81 110 | 12210.13,12406.36,12102.5,12280.32,4404570000,12280.32 111 | 12277.71,12425.98,12116.58,12289.76,4635070000,12289.76 112 | 12286.34,12317.2,12029.46,12083.77,4779980000,12083.77 113 | 12089.63,12337.72,12041.43,12141.58,4734240000,12141.58 114 | 12144.59,12376.72,12096.23,12307.35,4080420000,12307.35 115 | 12306.86,12381.44,12139.79,12269.08,3706940000,12269.08 116 | 12269.65,12378.67,12114.14,12160.3,3801960000,12160.3 117 | 12158.68,12212.33,11947.07,12029.06,4573570000,12029.06 118 | 12022.54,12188.31,11881.03,12063.09,4811670000,12063.09 119 | 12062.19,12078.23,11785.04,11842.69,5324900000,11842.69 120 | 11843.83,11986.94,11731.06,11842.36,4186370000,11842.36 121 | 11842.36,11962.37,11668.53,11807.43,4705050000,11807.43 122 | 11805.31,12008.7,11683.75,11811.83,4825640000,11811.83 123 | 11808.57,11808.57,11431.92,11453.42,5231280000,11453.42 124 | 11452.85,11556.33,11248.48,11346.51,6208260000,11346.51 125 | 11345.7,11504.55,11226.34,11350.01,5032330000,11350.01 126 | 11344.64,11465.79,11106.65,11382.26,5846290000,11382.26 127 | 11382.34,11510.41,11180.58,11215.51,5276090000,11215.51 128 | 11297.33,11336.49,11158.02,11288.53,3247590000,11288.53 129 | 11289.19,11477.52,11094.44,11231.96,5265420000,11231.96 130 | 11225.03,11459.52,11101.19,11384.21,6034110000,11384.21 131 | 11381.93,11505.12,11115.61,11147.44,5181000000,11147.44 132 | 11148.01,11351.24,11006.01,11229.02,5840430000,11229.02 133 | 11226.17,11292.04,10908.64,11100.54,6742200000,11100.54 134 | 11103.64,11299.7,10972.63,11055.19,5434860000,11055.19 135 | 11050.8,11201.67,10731.96,10962.54,7363640000,10962.54 136 | 10961.89,11308.41,10831.61,11239.28,6738630400,11239.28 137 | 11238.39,11538.5,11118.46,11446.66,7365209600,11446.66 138 | 11436.56,11599.57,11290.5,11496.57,5653280000,11496.57 139 | 11495.02,11663.4,11339.02,11467.34,4630640000,11467.34 140 | 11457.9,11692.79,11273.32,11602.5,6180230000,11602.5 141 | 11603.39,11820.21,11410.02,11632.38,6705830000,11632.38 142 | 11630.34,11714.21,11288.79,11349.28,6127980000,11349.28 143 | 11341.14,11540.78,11252.47,11370.69,4672560000,11370.69 144 | 11369.47,11439.25,11094.76,11131.08,4282960000,11131.08 145 | 11133.44,11444.05,11086.13,11397.56,5414240000,11397.56 146 | 11397.56,11681.47,11328.68,11583.69,5631330000,11583.69 147 | 11577.99,11631.16,11317.69,11378.02,5346050000,11378.02 148 | 11379.89,11512.61,11205.41,11326.32,4684870000,11326.32 149 | 11326.32,11449.67,11144.59,11284.15,4562280000,11284.15 150 | 11286.02,11652.24,11286.02,11615.77,1219310000,11615.77 151 | 11603.64,11745.71,11454.64,11656.07,4873420000,11656.07 152 | 11655.42,11680.5,11355.63,11431.43,5319380000,11431.43 153 | 11432.09,11808.49,11344.23,11734.32,4966810000,11734.32 154 | 11729.67,11933.55,11580.19,11782.35,5067310000,11782.35 155 | 11781.7,11830.39,11541.43,11642.47,4711290000,11642.47 156 | 11632.81,11689.05,11377.37,11532.96,4787600000,11532.96 157 | 11532.07,11744.33,11399.84,11615.93,4064000000,11615.93 158 | 11611.21,11776.41,11540.05,11659.9,4041820000,11659.9 159 | 11659.65,11744.49,11410.18,11479.39,3829290000,11479.39 160 | 11478.09,11501.45,11260.53,11348.55,4159760000,11348.55 161 | 11345.94,11511.06,11240.18,11417.43,4555030000,11417.43 162 | 11415.23,11501.29,11263.63,11430.21,4032590000,11430.21 163 | 11426.79,11684,11426.79,11628.06,3741070000,11628.06 164 | 11626.19,11626.19,11336.82,11386.25,3420600000,11386.25 165 | 11383.56,11483.62,11284.47,11412.87,3587570000,11412.87 166 | 11412.46,11575.14,11349.69,11502.51,3499610000,11502.51 167 | 11499.87,11756.46,11493.72,11715.18,3854280000,11715.18 168 | 11713.23,11730.49,11508.78,11543.55,3288120000,11543.55 169 | -------------------------------------------------------------------------------- /exercises/dow_selection/dow_selection.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Dow Selection\n", 8 | "\n", 9 | "Topics: Boolean array operators, sum function, where function, plotting.\n", 10 | "\n", 11 | "The array 'dow' is a 2-D array with each row holding the\n", 12 | "daily performance of the Dow Jones Industrial Average from the\n", 13 | "beginning of 2008 (dates have been removed for exercise simplicity).\n", 14 | "The array has the following structure::\n", 15 | "\n", 16 | " OPEN HIGH LOW CLOSE VOLUME ADJ_CLOSE\n", 17 | " 13261.82 13338.23 12969.42 13043.96 3452650000 13043.96\n", 18 | " 13044.12 13197.43 12968.44 13056.72 3429500000 13056.72\n", 19 | " 13046.56 13049.65 12740.51 12800.18 4166000000 12800.18\n", 20 | " 12801.15 12984.95 12640.44 12827.49 4221260000 12827.49\n", 21 | " 12820.9 12998.11 12511.03 12589.07 4705390000 12589.07\n", 22 | " 12590.21 12814.97 12431.53 12735.31 5351030000 12735.31" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "%matplotlib inline\n", 32 | "from numpy import loadtxt, sum, where\n", 33 | "import matplotlib.pyplot as plt" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "# Constants that indicate what data is held in each column of\n", 43 | "# the 'dow' array.\n", 44 | "OPEN = 0\n", 45 | "HIGH = 1\n", 46 | "LOW = 2\n", 47 | "CLOSE = 3\n", 48 | "VOLUME = 4\n", 49 | "ADJ_CLOSE = 5" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "0. The data has been loaded from a .csv file for you." 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [ 65 | "# 'dow' is our NumPy array that we will manipulate.\n", 66 | "dow = loadtxt('dow.csv', delimiter=',')" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "1. Create a \"mask\" array that indicates which rows have a volume\n", 74 | " greater than 5.5 billion." 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "2. How many are there? (hint: use sum)." 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "3. Find the index of every row (or day) where the volume is greater\n", 103 | " than 5.5 billion. hint: look at the `where()` command." 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "## Bonus\n", 118 | "\n", 119 | "1. Plot the adjusted close for EVERY day in 2008.\n", 120 | "2. Now over-plot this plot with a 'red dot' marker for every\n", 121 | " day where the volume was greater than 5.5 billion." 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [] 130 | } 131 | ], 132 | "metadata": { 133 | "kernelspec": { 134 | "display_name": "Python 3", 135 | "language": "python", 136 | "name": "python3" 137 | }, 138 | "language_info": { 139 | "codemirror_mode": { 140 | "name": "ipython", 141 | "version": 3 142 | }, 143 | "file_extension": ".py", 144 | "mimetype": "text/x-python", 145 | "name": "python", 146 | "nbconvert_exporter": "python", 147 | "pygments_lexer": "ipython3", 148 | "version": "3.6.10" 149 | } 150 | }, 151 | "nbformat": 4, 152 | "nbformat_minor": 2 153 | } 154 | -------------------------------------------------------------------------------- /exercises/dow_selection/dow_selection.py: -------------------------------------------------------------------------------- 1 | """ 2 | Dow Selection 3 | ------------- 4 | 5 | Topics: Boolean array operators, sum function, where function, plotting. 6 | 7 | The array 'dow' is a 2-D array with each row holding the 8 | daily performance of the Dow Jones Industrial Average from the 9 | beginning of 2008 (dates have been removed for exercise simplicity). 10 | The array has the following structure:: 11 | 12 | OPEN HIGH LOW CLOSE VOLUME ADJ_CLOSE 13 | 13261.82 13338.23 12969.42 13043.96 3452650000 13043.96 14 | 13044.12 13197.43 12968.44 13056.72 3429500000 13056.72 15 | 13046.56 13049.65 12740.51 12800.18 4166000000 12800.18 16 | 12801.15 12984.95 12640.44 12827.49 4221260000 12827.49 17 | 12820.9 12998.11 12511.03 12589.07 4705390000 12589.07 18 | 12590.21 12814.97 12431.53 12735.31 5351030000 12735.31 19 | 20 | 0. The data has been loaded from a .csv file for you. 21 | 1. Create a "mask" array that indicates which rows have a volume 22 | greater than 5.5 billion. 23 | 2. How many are there? (hint: use sum). 24 | 3. Find the index of every row (or day) where the volume is greater 25 | than 5.5 billion. hint: look at the where() command. 26 | 27 | Bonus 28 | ~~~~~ 29 | 30 | 1. Plot the adjusted close for *every* day in 2008. 31 | 2. Now over-plot this plot with a 'red dot' marker for every 32 | day where the volume was greater than 5.5 billion. 33 | 34 | See :ref:`dow-selection-solution`. 35 | """ 36 | 37 | from numpy import loadtxt, sum, where 38 | import matplotlib.pyplot as plt 39 | # Constants that indicate what data is held in each column of 40 | # the 'dow' array. 41 | OPEN = 0 42 | HIGH = 1 43 | LOW = 2 44 | CLOSE = 3 45 | VOLUME = 4 46 | ADJ_CLOSE = 5 47 | 48 | # 0. The data has been loaded from a .csv file for you. 49 | 50 | # 'dow' is our NumPy array that we will manipulate. 51 | dow = loadtxt('dow.csv', delimiter=',') 52 | 53 | # 1. Create a "mask" array that indicates which rows have a volume 54 | # greater than 5.5 billion. 55 | 56 | 57 | # 2. How many are there? (hint: use sum). 58 | 59 | # 3. Find the index of every row (or day) where the volume is greater 60 | # than 5.5 billion. hint: look at the where() command. 61 | 62 | # BONUS: 63 | # a. Plot the adjusted close for EVERY day in 2008. 64 | # b. Now over-plot this plot with a 'red dot' marker for every 65 | # day where the volume was greater than 5.5 billion. 66 | -------------------------------------------------------------------------------- /exercises/dow_selection/dow_selection_solution.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Dow Selection Solution\n", 8 | "\n", 9 | "The array 'dow' is a 2-D array with each row holding the\n", 10 | "daily performance of the Dow Jones Industrial Average from the\n", 11 | "beginning of 2008 (dates have been removed for exercise simplicity).\n", 12 | "The array has the following structure::\n", 13 | "\n", 14 | " OPEN HIGH LOW CLOSE VOLUME ADJ_CLOSE\n", 15 | " 13261.82 13338.23 12969.42 13043.96 3452650000 13043.96\n", 16 | " 13044.12 13197.43 12968.44 13056.72 3429500000 13056.72\n", 17 | " 13046.56 13049.65 12740.51 12800.18 4166000000 12800.18\n", 18 | " 12801.15 12984.95 12640.44 12827.49 4221260000 12827.49\n", 19 | " 12820.9 12998.11 12511.03 12589.07 4705390000 12589.07\n", 20 | " 12590.21 12814.97 12431.53 12735.31 5351030000 12735.31\n", 21 | "\n", 22 | "0. The data has been loaded from a .csv file for you.\n", 23 | "1. Create a \"mask\" array that indicates which rows have a volume\n", 24 | " greater than 5.5 billion.\n", 25 | "2. How many are there? (hint: use sum).\n", 26 | "3. Find the index of every row (or day) where the volume is greater\n", 27 | " than 5.5 billion. hint: look at the where() command.\n", 28 | "\n", 29 | "## Bonus\n", 30 | "\n", 31 | "1. Plot the adjusted close for *every* day in 2008.\n", 32 | "2. Now over-plot this plot with a 'red dot' marker for every\n", 33 | " day where the volume was greater than 5.5 billion." 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "%matplotlib inline\n", 43 | "from numpy import loadtxt, sum, where\n", 44 | "import matplotlib.pyplot as plt" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": null, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "# Constants that indicate what data is held in each column of\n", 54 | "# the 'dow' array.\n", 55 | "OPEN = 0\n", 56 | "HIGH = 1\n", 57 | "LOW = 2\n", 58 | "CLOSE = 3\n", 59 | "VOLUME = 4\n", 60 | "ADJ_CLOSE = 5" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "\n", 68 | "0. The data has been loaded from a csv file for you." 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "# 'dow' is our NumPy array that we will manipulate.\n", 78 | "dow = loadtxt('dow.csv', delimiter=',')" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "1. Create a \"mask\" array that indicates which rows have a volume\n", 86 | " greater than 5.5 billion." 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "high_volume_mask = dow[:, VOLUME] > 5.5e9" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "2. How many are there? (hint: use sum)." 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "metadata": {}, 109 | "outputs": [], 110 | "source": [ 111 | "high_volume_days = sum(high_volume_mask)\n", 112 | "print(\"The dow volume has been above 5.5 billion on\" \\\n", 113 | " \" %d days this year.\" % high_volume_days)" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "3. Find the index of every row (or day) where the volume is greater\n", 121 | " than 5.5 billion. hint: look at the where() command." 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "high_vol_index = where(high_volume_mask)[0]" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "## BONUS:\n", 138 | "\n", 139 | "1. Plot the adjusted close for EVERY day in 2008.\n", 140 | "2. Now over-plot this plot with a 'red dot' marker for every\n", 141 | " day where the dow was greater than 5.5 billion." 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [ 150 | "# Create a new plot.\n", 151 | "plt.figure()\n", 152 | "\n", 153 | "# Plot the adjusted close for every day of the year as a line (default)\n", 154 | "plt.plot(dow[:, ADJ_CLOSE])\n", 155 | "\n", 156 | "# Plot the days where the volume was high with red dots...\n", 157 | "plt.plot(high_vol_index, dow[high_vol_index, ADJ_CLOSE], 'ro')" 158 | ] 159 | } 160 | ], 161 | "metadata": { 162 | "kernelspec": { 163 | "display_name": "Python 3", 164 | "language": "python", 165 | "name": "python3" 166 | }, 167 | "language_info": { 168 | "codemirror_mode": { 169 | "name": "ipython", 170 | "version": 3 171 | }, 172 | "file_extension": ".py", 173 | "mimetype": "text/x-python", 174 | "name": "python", 175 | "nbconvert_exporter": "python", 176 | "pygments_lexer": "ipython3", 177 | "version": "3.6.10" 178 | } 179 | }, 180 | "nbformat": 4, 181 | "nbformat_minor": 2 182 | } 183 | -------------------------------------------------------------------------------- /exercises/dow_selection/dow_selection_solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | Topics: Boolean array operators, sum function, where function, plotting. 4 | 5 | The array 'dow' is a 2-D array with each row holding the 6 | daily performance of the Dow Jones Industrial Average from the 7 | beginning of 2008 (dates have been removed for exercise simplicity). 8 | The array has the following structure:: 9 | 10 | OPEN HIGH LOW CLOSE VOLUME ADJ_CLOSE 11 | 13261.82 13338.23 12969.42 13043.96 3452650000 13043.96 12 | 13044.12 13197.43 12968.44 13056.72 3429500000 13056.72 13 | 13046.56 13049.65 12740.51 12800.18 4166000000 12800.18 14 | 12801.15 12984.95 12640.44 12827.49 4221260000 12827.49 15 | 12820.9 12998.11 12511.03 12589.07 4705390000 12589.07 16 | 12590.21 12814.97 12431.53 12735.31 5351030000 12735.31 17 | 18 | 0. The data has been loaded from a .csv file for you. 19 | 1. Create a "mask" array that indicates which rows have a volume 20 | greater than 5.5 billion. 21 | 2. How many are there? (hint: use sum). 22 | 3. Find the index of every row (or day) where the volume is greater 23 | than 5.5 billion. hint: look at the where() command. 24 | 25 | Bonus 26 | ~~~~~ 27 | 28 | 1. Plot the adjusted close for *every* day in 2008. 29 | 2. Now over-plot this plot with a 'red dot' marker for every 30 | day where the volume was greater than 5.5 billion. 31 | 32 | """ 33 | 34 | from __future__ import print_function 35 | from numpy import loadtxt, sum, where 36 | import matplotlib.pyplot as plt 37 | # Constants that indicate what data is held in each column of 38 | # the 'dow' array. 39 | OPEN = 0 40 | HIGH = 1 41 | LOW = 2 42 | CLOSE = 3 43 | VOLUME = 4 44 | ADJ_CLOSE = 5 45 | 46 | # 0. The data has been loaded from a csv file for you. 47 | 48 | # 'dow' is our NumPy array that we will manipulate. 49 | dow = loadtxt('dow.csv', delimiter=',') 50 | 51 | 52 | # 1. Create a "mask" array that indicates which rows have a volume 53 | # greater than 5.5 billion. 54 | high_volume_mask = dow[:, VOLUME] > 5.5e9 55 | 56 | # 2. How many are there? (hint: use sum). 57 | high_volume_days = sum(high_volume_mask) 58 | print("The dow volume has been above 5.5 billion on" \ 59 | " %d days this year." % high_volume_days) 60 | 61 | # 3. Find the index of every row (or day) where the volume is greater 62 | # than 5.5 billion. hint: look at the where() command. 63 | high_vol_index = where(high_volume_mask)[0] 64 | 65 | # BONUS: 66 | # 1. Plot the adjusted close for EVERY day in 2008. 67 | # 2. Now over-plot this plot with a 'red dot' marker for every 68 | # day where the dow was greater than 5.5 billion. 69 | 70 | # Create a new plot. 71 | plt.figure() 72 | 73 | # Plot the adjusted close for every day of the year as a blue line. 74 | # In the format string 'b-', 'b' means blue and '-' indicates a line. 75 | plt.plot(dow[:, ADJ_CLOSE], 'b-') 76 | 77 | # Plot the days where the volume was high with red dots... 78 | plt.plot(high_vol_index, dow[high_vol_index, ADJ_CLOSE], 'ro') 79 | 80 | # Scripts must call the "plt.show" command to display the plot 81 | # to the screen. 82 | plt.show() 83 | -------------------------------------------------------------------------------- /exercises/filter_image/dc_metro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enthought/Numpy-Tutorial-SciPyConf-2021/6ae55cdf01eb0a0c6df64a179d74ad8ad6de78b8/exercises/filter_image/dc_metro.png -------------------------------------------------------------------------------- /exercises/filter_image/filter_image.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Filter Image\n", 8 | "\n", 9 | "Read in the \"dc_metro\" image and use an averaging filter\n", 10 | "to \"smooth\" the image. Use a \"5 point stencil\" where\n", 11 | "you average the current pixel with its neighboring pixels::\n", 12 | "\n", 13 | " 0 0 0 0 0 0 0\n", 14 | " 0 0 0 x 0 0 0\n", 15 | " 0 0 x x x 0 0\n", 16 | " 0 0 0 x 0 0 0\n", 17 | " 0 0 0 0 0 0 0\n", 18 | "\n", 19 | "Plot the image, the smoothed image, and the difference between the\n", 20 | "two.\n", 21 | "\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": null, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "%matplotlib inline\n", 31 | "import numpy as np\n", 32 | "import matplotlib.pyplot as plt" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "## Plotting utilities (helpful examples)" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": null, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "# Read an image from a file into an array\n", 49 | "img = plt.imread('dc_metro.png')" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "# Display 2D array as an image (can specify color map as `cmap`)\n", 59 | "plt.imshow(img, cmap=plt.cm.hot);" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "# Use plt.subplot for comparing images\n", 69 | "plt.figure(figsize=(12, 6))\n", 70 | "\n", 71 | "# Plot the original\n", 72 | "plt.subplot(1,3,1)\n", 73 | "plt.imshow(img)\n", 74 | "plt.title('original')\n", 75 | "\n", 76 | "# Plot another image\n", 77 | "plt.subplot(1,3,2)\n", 78 | "plt.imshow(img, cmap='gray')\n", 79 | "plt.title('grayscale')\n", 80 | "\n", 81 | "# Plot another image\n", 82 | "plt.subplot(1,3,3)\n", 83 | "plt.imshow(img, cmap=plt.cm.hot)\n", 84 | "plt.title('hot');" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "1. Use an averaging filter to \"smooth\" the `'dc_metro.png'` image. Use a \"5 point stencil\" where you average the current pixel with its neighboring pixels::\n", 92 | "\n", 93 | " 0 0 0 0 0 0 0\n", 94 | " 0 0 0 x 0 0 0\n", 95 | " 0 0 x x x 0 0\n", 96 | " 0 0 0 x 0 0 0\n", 97 | " 0 0 0 0 0 0 0\n", 98 | "Plot the image, the smoothed image, and the difference between the two." 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "# Your code goes here" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "## Bonus\n", 115 | "\n", 116 | "Re-filter the image by passing the result image through the filter again. Do this 50 times and plot the resulting image.\n", 117 | "\n" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": null, 123 | "metadata": {}, 124 | "outputs": [], 125 | "source": [ 126 | "# Your code goes here" 127 | ] 128 | } 129 | ], 130 | "metadata": { 131 | "kernelspec": { 132 | "display_name": "Python 3", 133 | "language": "python", 134 | "name": "python3" 135 | }, 136 | "language_info": { 137 | "codemirror_mode": { 138 | "name": "ipython", 139 | "version": 3 140 | }, 141 | "file_extension": ".py", 142 | "mimetype": "text/x-python", 143 | "name": "python", 144 | "nbconvert_exporter": "python", 145 | "pygments_lexer": "ipython3", 146 | "version": "3.6.13" 147 | } 148 | }, 149 | "nbformat": 4, 150 | "nbformat_minor": 2 151 | } 152 | -------------------------------------------------------------------------------- /exercises/filter_image/filter_image.py: -------------------------------------------------------------------------------- 1 | """ 2 | Filter Image 3 | ------------ 4 | 5 | Read in the "dc_metro" image and use an averaging filter 6 | to "smooth" the image. Use a "5 point stencil" where 7 | you average the current pixel with its neighboring pixels:: 8 | 9 | 0 0 0 0 0 0 0 10 | 0 0 0 x 0 0 0 11 | 0 0 x x x 0 0 12 | 0 0 0 x 0 0 0 13 | 0 0 0 0 0 0 0 14 | 15 | Plot the image, the smoothed image, and the difference between the 16 | two. 17 | 18 | Bonus 19 | ~~~~~ 20 | 21 | Re-filter the image by passing the result image through the filter again. Do 22 | this 50 times and plot the resulting image. 23 | 24 | See :ref:`filter-image-solution`. 25 | """ 26 | 27 | import matplotlib.pyplot as plt 28 | 29 | img = plt.imread('dc_metro.png') 30 | 31 | plt.imshow(img, cmap=plt.cm.hot) 32 | plt.show() 33 | -------------------------------------------------------------------------------- /exercises/filter_image/filter_image_solution.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Filter Image Solution\n", 8 | "\n", 9 | "Read in the \"dc_metro\" image and use an averaging filter\n", 10 | "to \"smooth\" the image. Use a \"5 point stencil\" where\n", 11 | "you average the current pixel with its neighboring pixels::\n", 12 | "\n", 13 | " 0 0 0 0 0 0 0\n", 14 | " 0 0 0 x 0 0 0\n", 15 | " 0 0 x x x 0 0\n", 16 | " 0 0 0 x 0 0 0\n", 17 | " 0 0 0 0 0 0 0\n", 18 | "\n", 19 | "Plot the image, the smoothed image, and the difference between the\n", 20 | "two.\n" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": null, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "%matplotlib inline\n", 30 | "import numpy as np\n", 31 | "import matplotlib.pyplot as plt" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "## Plotting utilities (helpful examples)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "# Read an image from a file into an array\n", 48 | "img = plt.imread('dc_metro.png')" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "# Display 2D array as an image (can specify color map as `cmap`)\n", 58 | "plt.imshow(img, cmap=plt.cm.hot);" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": null, 64 | "metadata": {}, 65 | "outputs": [], 66 | "source": [ 67 | "# Use plt.subplot for comparing images\n", 68 | "plt.figure(figsize=(12, 6))\n", 69 | "\n", 70 | "# Plot the original\n", 71 | "plt.subplot(1,3,1)\n", 72 | "plt.imshow(img)\n", 73 | "plt.title('original')\n", 74 | "\n", 75 | "# Plot another image\n", 76 | "plt.subplot(1,3,2)\n", 77 | "plt.imshow(img, cmap='gray')\n", 78 | "plt.title('grayscale')\n", 79 | "\n", 80 | "# Plot another image\n", 81 | "plt.subplot(1,3,3)\n", 82 | "plt.imshow(img, cmap=plt.cm.hot)\n", 83 | "plt.title('hot');" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "1. Use an averaging filter to \"smooth\" the `'dc_metro.png'` image. Use a \"5 point stencil\" where you average the current pixel with its neighboring pixels::\n", 91 | "\n", 92 | " 0 0 0 0 0 0 0\n", 93 | " 0 0 0 x 0 0 0\n", 94 | " 0 0 x x x 0 0\n", 95 | " 0 0 0 x 0 0 0\n", 96 | " 0 0 0 0 0 0 0\n", 97 | "Plot the image, the smoothed image, and the difference between the two." 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": null, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "def smooth(img):\n", 107 | " avg_img =( img[1:-1 ,1:-1] # center\n", 108 | " + img[ :-2 ,1:-1] # top\n", 109 | " + img[2: ,1:-1] # bottom\n", 110 | " + img[1:-1 , :-2] # left\n", 111 | " + img[1:-1 ,2: ] # right\n", 112 | " ) / 5.0\n", 113 | " return avg_img\n", 114 | "\n", 115 | "\n", 116 | "def smooth_loop(img):\n", 117 | " smoothed = np.zeros((img.shape[0]-2, img.shape[1]-2))\n", 118 | " for r in range(0, img.shape[0]-2):\n", 119 | " for c in range(0, img.shape[1]-2):\n", 120 | " smoothed[r, c] = ( img[r+1, c+1] # center\n", 121 | " + img[r , c+1] # top\n", 122 | " + img[r+2, c+1] # bottom\n", 123 | " + img[r+1, c ] # left\n", 124 | " + img[r+1, c+2] # right\n", 125 | " ) / 5.0\n", 126 | " return smoothed" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": null, 132 | "metadata": {}, 133 | "outputs": [], 134 | "source": [ 135 | "img = plt.imread('dc_metro.png')\n", 136 | "avg_img = smooth(img)\n", 137 | "\n", 138 | "plt.figure(figsize=(12, 6))\n", 139 | "# Set colormap so that images are plotted in gray scale.\n", 140 | "plt.gray()\n", 141 | "# Plot the original image first\n", 142 | "plt.subplot(1,3,1)\n", 143 | "plt.imshow(img)\n", 144 | "plt.title('original')\n", 145 | "\n", 146 | "# Now the filtered image.\n", 147 | "plt.subplot(1,3,2)\n", 148 | "plt.imshow(avg_img)\n", 149 | "plt.title('smoothed once')\n", 150 | "\n", 151 | "# And finally the difference between the two.\n", 152 | "plt.subplot(1,3,3)\n", 153 | "plt.imshow(img[1:-1,1:-1] - avg_img)\n", 154 | "plt.title('difference')" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "## Bonus\n", 162 | "\n", 163 | "Re-filter the image by passing the result image through the filter again. Do\n", 164 | "this 50 times and plot the resulting image." 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": null, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [ 173 | "for num in range(50):\n", 174 | " avg_img = smooth(avg_img)\n", 175 | "\n", 176 | "# Plot the original image first\n", 177 | "plt.figure(figsize=(12, 6))\n", 178 | "plt.subplot(1,2,1)\n", 179 | "plt.imshow(img)\n", 180 | "plt.title('original')\n", 181 | "\n", 182 | "# Now the filtered image.\n", 183 | "plt.subplot(1,2,2)\n", 184 | "plt.imshow(avg_img)\n", 185 | "plt.title('smoothed 50 times')\n", 186 | "\n", 187 | "assert np.allclose(smooth(img), smooth_loop(img))" 188 | ] 189 | } 190 | ], 191 | "metadata": { 192 | "kernelspec": { 193 | "display_name": "Python 3", 194 | "language": "python", 195 | "name": "python3" 196 | }, 197 | "language_info": { 198 | "codemirror_mode": { 199 | "name": "ipython", 200 | "version": 3 201 | }, 202 | "file_extension": ".py", 203 | "mimetype": "text/x-python", 204 | "name": "python", 205 | "nbconvert_exporter": "python", 206 | "pygments_lexer": "ipython3", 207 | "version": "3.6.13" 208 | } 209 | }, 210 | "nbformat": 4, 211 | "nbformat_minor": 2 212 | } 213 | -------------------------------------------------------------------------------- /exercises/filter_image/filter_image_solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | Filter Image 3 | ------------ 4 | 5 | Read in the "dc_metro" image and use an averaging filter 6 | to "smooth" the image. Use a "5 point stencil" where 7 | you average the current pixel with its neighboring pixels:: 8 | 9 | 0 0 0 0 0 0 0 10 | 0 0 0 x 0 0 0 11 | 0 0 x x x 0 0 12 | 0 0 0 x 0 0 0 13 | 0 0 0 0 0 0 0 14 | 15 | Plot the image, the smoothed image, and the difference between the 16 | two. 17 | 18 | Bonus 19 | ~~~~~ 20 | 21 | Re-filter the image by passing the result image through the filter again. Do 22 | this 50 times and plot the resulting image. 23 | 24 | """ 25 | import numpy as np 26 | import matplotlib.pyplot as plt 27 | 28 | def smooth(img): 29 | avg_img =( img[1:-1 ,1:-1] # center 30 | + img[ :-2 ,1:-1] # top 31 | + img[2: ,1:-1] # bottom 32 | + img[1:-1 , :-2] # left 33 | + img[1:-1 ,2: ] # right 34 | ) / 5.0 35 | return avg_img 36 | 37 | 38 | def smooth_loop(img): 39 | smoothed = np.zeros((img.shape[0]-2, img.shape[1]-2)) 40 | for r in range(0, img.shape[0]-2): 41 | for c in range(0, img.shape[1]-2): 42 | smoothed[r, c] = ( img[r+1, c+1] # center 43 | + img[r , c+1] # top 44 | + img[r+2, c+1] # bottom 45 | + img[r+1, c ] # left 46 | + img[r+1, c+2] # right 47 | ) / 5.0 48 | return smoothed 49 | 50 | 51 | img = plt.imread('dc_metro.png') 52 | avg_img = smooth(img) 53 | 54 | plt.figure() 55 | # Set colormap so that images are plotted in gray scale. 56 | plt.gray() 57 | # Plot the original image first 58 | plt.subplot(1,3,1) 59 | plt.imshow(img) 60 | plt.title('original') 61 | 62 | # Now the filtered image. 63 | plt.subplot(1,3,2) 64 | plt.imshow(avg_img) 65 | plt.title('smoothed once') 66 | 67 | # And finally the difference between the two. 68 | plt.subplot(1,3,3) 69 | plt.imshow(img[1:-1,1:-1] - avg_img) 70 | plt.title('difference') 71 | 72 | 73 | # Bonus: Re-filter the image by passing the result image 74 | # through the filter again. Do this 50 times and plot 75 | # the resulting image. 76 | 77 | for num in range(50): 78 | avg_img = smooth(avg_img) 79 | 80 | # Plot the original image first 81 | plt.figure() 82 | plt.subplot(1,2,1) 83 | plt.imshow(img) 84 | plt.title('original') 85 | 86 | # Now the filtered image. 87 | plt.subplot(1,2,2) 88 | plt.imshow(avg_img) 89 | plt.title('smoothed 50 times') 90 | 91 | assert np.allclose(smooth(img), smooth_loop(img)) 92 | 93 | plt.show() 94 | -------------------------------------------------------------------------------- /exercises/load_text/complex_data_file.txt: -------------------------------------------------------------------------------- 1 | -- THIS IS THE BEGINNING OF THE FILE -- 2 | % This is a more complex file to read! 3 | 4 | % Day, Month, Year, Useless Col, Avg Power 5 | 01, 01, 2000, ad766, 30 6 | 02, 01, 2000, t873, 41 7 | % we don't have Jan 03rd! 8 | 04, 01, 2000, r441, 55 9 | 05, 01, 2000, s345, 78 10 | 06, 01, 2000, x273, 134 % that day was crazy 11 | 07, 01, 2000, x355, 42 12 | 13 | %-- THIS IS THE END OF THE FILE -- 14 | -------------------------------------------------------------------------------- /exercises/load_text/float_data.txt: -------------------------------------------------------------------------------- 1 | 1 2 3 4 2 | 5 6 7 8 -------------------------------------------------------------------------------- /exercises/load_text/float_data_with_header.txt: -------------------------------------------------------------------------------- 1 | c1 c2 c3 c4 2 | 1 2 3 4 3 | 5 6 7 8 -------------------------------------------------------------------------------- /exercises/load_text/load_text.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Load Array from Text File\n", 8 | "\n", 9 | "0. In a clean notebook cell, type:\n", 10 | "\n", 11 | " In [ ]: loadtxt?\n", 12 | "\n", 13 | " to see the options on how to use the `loadtxt` command.\n", 14 | "\n", 15 | "\n", 16 | "1. Use `loadtxt` to load in a 2D array of floating point values from `'float_data.txt'`.\n", 17 | " The data in the file looks like:\n", 18 | "\n", 19 | " 1 2 3 4\n", 20 | " 5 6 7 8\n", 21 | "\n", 22 | " The resulting data should be a 2x4 array of floating point values.\n", 23 | "\n", 24 | "\n", 25 | "2. In the second example, the file `'float_data_with_header.txt'` has\n", 26 | " strings as column names in the first row:\n", 27 | "\n", 28 | " c1 c2 c3 c4\n", 29 | " 1 2 3 4\n", 30 | " 5 6 7 8\n", 31 | "\n", 32 | " Ignore these column names, and read the remainder of the data into\n", 33 | " a 2D array.\n", 34 | "\n", 35 | "## Bonus\n", 36 | "\n", 37 | "\n", 38 | "3. A third example is more involved. It contains comments in multiple\n", 39 | " locations, uses multiple formats, and includes a useless column to\n", 40 | " skip:\n", 41 | "\n", 42 | " -- THIS IS THE BEGINNING OF THE FILE -- \n", 43 | " % This is a more complex file to read!\n", 44 | "\n", 45 | " % Day, Month, Year, Useless Col, Avg Power\n", 46 | " 01, 01, 2000, ad766, 30\n", 47 | " 02, 01, 2000, t873, 41\n", 48 | " % we don't have Jan 03rd!\n", 49 | " 04, 01, 2000, r441, 55\n", 50 | " 05, 01, 2000, s345, 78\n", 51 | " 06, 01, 2000, x273, 134 % that day was crazy\n", 52 | " 07, 01, 2000, x355, 42\n", 53 | "\n", 54 | " %-- THIS IS THE END OF THE FILE --\n" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "from numpy import loadtxt" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "0. Use the command `loadtxt?` below to see the options on how to use the `loadtxt` command." 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "# Your code goes here" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "1. Use `loadtxt` to load in a 2D array of floating point values from `'float_data.txt'`.\n", 87 | " The data in the file looks like:\n", 88 | "\n", 89 | " 1 2 3 4\n", 90 | " 5 6 7 8\n", 91 | "\n", 92 | " The resulting data should be a 2x4 array of floating point values." 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": null, 98 | "metadata": {}, 99 | "outputs": [], 100 | "source": [ 101 | "# Simple example loading a 2x4 array of floats from a file\n", 102 | "ary1 = # Your code goes here\n", 103 | "\n", 104 | "print('example 1:')\n", 105 | "print(ary1)" 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": {}, 111 | "source": [ 112 | "2. In the second example, the file `'float_data_with_header.txt'` has\n", 113 | " strings as column names in the first row:\n", 114 | "\n", 115 | " c1 c2 c3 c4\n", 116 | " 1 2 3 4\n", 117 | " 5 6 7 8\n", 118 | "\n", 119 | " Ignore these column names, and read the remainder of the data into\n", 120 | " a 2D array." 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "# Same example, but skipping the first row of column headers\n", 130 | "ary2 = # Your code goes here\n", 131 | "\n", 132 | "print('example 2:')\n", 133 | "print(ary2)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "## Bonus\n", 141 | "\n", 142 | "\n", 143 | "3. A third example is more involved. It contains comments in multiple\n", 144 | " locations, uses multiple formats, and includes a useless column to\n", 145 | " skip:\n", 146 | "\n", 147 | " -- THIS IS THE BEGINNING OF THE FILE -- \n", 148 | " % This is a more complex file to read!\n", 149 | "\n", 150 | " % Day, Month, Year, Useless Col, Avg Power\n", 151 | " 01, 01, 2000, ad766, 30\n", 152 | " 02, 01, 2000, t873, 41\n", 153 | " % we don't have Jan 03rd!\n", 154 | " 04, 01, 2000, r441, 55\n", 155 | " 05, 01, 2000, s345, 78\n", 156 | " 06, 01, 2000, x273, 134 % that day was crazy\n", 157 | " 07, 01, 2000, x355, 42\n", 158 | "\n", 159 | " %-- THIS IS THE END OF THE FILE --" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "# More complex example with comments and columns to skip\n", 169 | "ary3 = # Your code goes here\n", 170 | "\n", 171 | "print('example 3:')\n", 172 | "print(ary3)" 173 | ] 174 | } 175 | ], 176 | "metadata": { 177 | "kernelspec": { 178 | "display_name": "Python 3", 179 | "language": "python", 180 | "name": "python3" 181 | }, 182 | "language_info": { 183 | "codemirror_mode": { 184 | "name": "ipython", 185 | "version": 3 186 | }, 187 | "file_extension": ".py", 188 | "mimetype": "text/x-python", 189 | "name": "python", 190 | "nbconvert_exporter": "python", 191 | "pygments_lexer": "ipython3", 192 | "version": "3.6.13" 193 | } 194 | }, 195 | "nbformat": 4, 196 | "nbformat_minor": 4 197 | } 198 | -------------------------------------------------------------------------------- /exercises/load_text/load_text.py: -------------------------------------------------------------------------------- 1 | """ 2 | Load Array from Text File 3 | ------------------------- 4 | 5 | 0. From the IPython prompt, type:: 6 | 7 | In [1]: loadtxt? 8 | 9 | to see the options on how to use the loadtxt command. 10 | 11 | 12 | 1. Use loadtxt to load in a 2D array of floating point values from 13 | 'float_data.txt'. The data in the file looks like:: 14 | 15 | 1 2 3 4 16 | 5 6 7 8 17 | 18 | The resulting data should be a 2x4 array of floating point values. 19 | 20 | 2. In the second example, the file 'float_data_with_header.txt' has 21 | strings as column names in the first row:: 22 | 23 | c1 c2 c3 c4 24 | 1 2 3 4 25 | 5 6 7 8 26 | 27 | Ignore these column names, and read the remainder of the data into 28 | a 2D array. 29 | 30 | Later on, we'll learn how to create a "structured array" using 31 | these column names to create fields within an array. 32 | 33 | Bonus 34 | ~~~~~ 35 | 36 | 3. A third example is more involved (the file is called 37 | 'complex_data_file.txt'). It contains comments in multiple 38 | locations, uses multiple formats, and includes a useless column to 39 | skip:: 40 | 41 | -- THIS IS THE BEGINNING OF THE FILE -- 42 | % This is a more complex file to read! 43 | 44 | % Day, Month, Year, Useless Col, Avg Power 45 | 01, 01, 2000, ad766, 30 46 | 02, 01, 2000, t873, 41 47 | % we don't have Jan 03rd! 48 | 04, 01, 2000, r441, 55 49 | 05, 01, 2000, s345, 78 50 | 06, 01, 2000, x273, 134 % that day was crazy 51 | 07, 01, 2000, x355, 42 52 | 53 | %-- THIS IS THE END OF THE FILE -- 54 | 55 | 56 | See :ref:`load-text-solution` 57 | """ 58 | 59 | from numpy import loadtxt 60 | -------------------------------------------------------------------------------- /exercises/load_text/load_text_solution.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Load Array from Text File Solution\n", 8 | "\n", 9 | "0. In a clean notebook cell, type:\n", 10 | "\n", 11 | " In [ ]: loadtxt?\n", 12 | "\n", 13 | " to see the options on how to use the `loadtxt` command.\n", 14 | "\n", 15 | "\n", 16 | "1. Use `loadtxt` to load in a 2D array of floating point values from `'float_data.txt'`.\n", 17 | " The data in the file looks like:\n", 18 | "\n", 19 | " 1 2 3 4\n", 20 | " 5 6 7 8\n", 21 | "\n", 22 | " The resulting data should be a 2x4 array of floating point values.\n", 23 | "\n", 24 | "\n", 25 | "2. In the second example, the file `'float_data_with_header.txt'` has\n", 26 | " strings as column names in the first row:\n", 27 | "\n", 28 | " c1 c2 c3 c4\n", 29 | " 1 2 3 4\n", 30 | " 5 6 7 8\n", 31 | "\n", 32 | " Ignore these column names, and read the remainder of the data into\n", 33 | " a 2D array.\n", 34 | "\n", 35 | "## Bonus\n", 36 | "\n", 37 | "\n", 38 | "3. A third example is more involved. It contains comments in multiple\n", 39 | " locations, uses multiple formats, and includes a useless column to\n", 40 | " skip:\n", 41 | "\n", 42 | " -- THIS IS THE BEGINNING OF THE FILE -- \n", 43 | " % This is a more complex file to read!\n", 44 | "\n", 45 | " % Day, Month, Year, Useless Col, Avg Power\n", 46 | " 01, 01, 2000, ad766, 30\n", 47 | " 02, 01, 2000, t873, 41\n", 48 | " % we don't have Jan 03rd!\n", 49 | " 04, 01, 2000, r441, 55\n", 50 | " 05, 01, 2000, s345, 78\n", 51 | " 06, 01, 2000, x273, 134 % that day was crazy\n", 52 | " 07, 01, 2000, x355, 42\n", 53 | "\n", 54 | " %-- THIS IS THE END OF THE FILE --\n" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "from numpy import loadtxt" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "0. Use the command `loadtxt?` below to see the options on how to use the `loadtxt` command." 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "loadtxt?" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "1. Use `loadtxt` to load in a 2D array of floating point values from `'float_data.txt'`.\n", 87 | " The data in the file looks like:\n", 88 | "\n", 89 | " 1 2 3 4\n", 90 | " 5 6 7 8\n", 91 | "\n", 92 | " The resulting data should be a 2x4 array of floating point values." 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": null, 98 | "metadata": {}, 99 | "outputs": [], 100 | "source": [ 101 | "# Simple example loading a 2x4 array of floats from a file\n", 102 | "ary1 = loadtxt('float_data.txt')\n", 103 | "\n", 104 | "print('example 1:')\n", 105 | "print(ary1)" 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": {}, 111 | "source": [ 112 | "2. In the second example, the file `'float_data_with_header.txt'` has\n", 113 | " strings as column names in the first row:\n", 114 | "\n", 115 | " c1 c2 c3 c4\n", 116 | " 1 2 3 4\n", 117 | " 5 6 7 8\n", 118 | "\n", 119 | " Ignore these column names, and read the remainder of the data into\n", 120 | " a 2D array." 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "# Same example, but skipping the first row of column headers\n", 130 | "ary2 = loadtxt('float_data_with_header.txt', skiprows=1)\n", 131 | "\n", 132 | "print('example 2:')\n", 133 | "print(ary2)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "## Bonus\n", 141 | "\n", 142 | "\n", 143 | "3. A third example is more involved. It contains comments in multiple\n", 144 | " locations, uses multiple formats, and includes a useless column to\n", 145 | " skip:\n", 146 | "\n", 147 | " -- THIS IS THE BEGINNING OF THE FILE -- \n", 148 | " % This is a more complex file to read!\n", 149 | "\n", 150 | " % Day, Month, Year, Useless Col, Avg Power\n", 151 | " 01, 01, 2000, ad766, 30\n", 152 | " 02, 01, 2000, t873, 41\n", 153 | " % we don't have Jan 03rd!\n", 154 | " 04, 01, 2000, r441, 55\n", 155 | " 05, 01, 2000, s345, 78\n", 156 | " 06, 01, 2000, x273, 134 % that day was crazy\n", 157 | " 07, 01, 2000, x355, 42\n", 158 | "\n", 159 | " %-- THIS IS THE END OF THE FILE --" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "# More complex example with comments and columns to skip\n", 169 | "ary3 = loadtxt('complex_data_file.txt', delimiter=',', comments='%',\n", 170 | " usecols=(0,1,2,4), dtype=int, skiprows=1)\n", 171 | "\n", 172 | "print('example 3:')\n", 173 | "print(ary3)" 174 | ] 175 | } 176 | ], 177 | "metadata": { 178 | "kernelspec": { 179 | "display_name": "Python 3", 180 | "language": "python", 181 | "name": "python3" 182 | }, 183 | "language_info": { 184 | "codemirror_mode": { 185 | "name": "ipython", 186 | "version": 3 187 | }, 188 | "file_extension": ".py", 189 | "mimetype": "text/x-python", 190 | "name": "python", 191 | "nbconvert_exporter": "python", 192 | "pygments_lexer": "ipython3", 193 | "version": "3.6.13" 194 | } 195 | }, 196 | "nbformat": 4, 197 | "nbformat_minor": 4 198 | } 199 | -------------------------------------------------------------------------------- /exercises/load_text/load_text_solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | Load Array from Text File 3 | ------------------------- 4 | 5 | 0. From the IPython prompt, type:: 6 | 7 | In [1]: loadtxt? 8 | 9 | to see the options on how to use the loadtxt command. 10 | 11 | 12 | 1. Use loadtxt to load in a 2D array of floating point values from 13 | 'float_data.txt'. The data in the file looks like:: 14 | 15 | 1 2 3 4 16 | 5 6 7 8 17 | 18 | The resulting data should be a 2x4 array of floating point values. 19 | 20 | 2. In the second example, the file 'float_data_with_header.txt' has 21 | strings as column names in the first row:: 22 | 23 | c1 c2 c3 c4 24 | 1 2 3 4 25 | 5 6 7 8 26 | 27 | Ignore these column names, and read the remainder of the data into 28 | a 2D array. 29 | 30 | Later on, we'll learn how to create a "structured array" using 31 | these column names to create fields within an array. 32 | 33 | Bonus 34 | ~~~~~ 35 | 36 | 3. A third example is more involved. It contains comments in multiple 37 | locations, uses multiple formats, and includes a useless column to 38 | skip:: 39 | 40 | -- THIS IS THE BEGINNING OF THE FILE -- 41 | % This is a more complex file to read! 42 | 43 | % Day, Month, Year, Useless Col, Avg Power 44 | 01, 01, 2000, ad766, 30 45 | 02, 01, 2000, t873, 41 46 | % we don't have Jan 03rd! 47 | 04, 01, 2000, r441, 55 48 | 05, 01, 2000, s345, 78 49 | 06, 01, 2000, x273, 134 % that day was crazy 50 | 07, 01, 2000, x355, 42 51 | 52 | %-- THIS IS THE END OF THE FILE -- 53 | """ 54 | 55 | from numpy import loadtxt 56 | 57 | ############################################################################# 58 | # 1. Simple example loading a 2x4 array of floats from a file. 59 | ############################################################################# 60 | ary1 = loadtxt('float_data.txt') 61 | 62 | print('example 1:') 63 | print(ary1) 64 | 65 | 66 | ############################################################################# 67 | # 2. Same example, but skipping the first row of column headers 68 | ############################################################################# 69 | ary2 = loadtxt('float_data_with_header.txt', skiprows=1) 70 | 71 | print('example 2:') 72 | print(ary2) 73 | 74 | ############################################################################# 75 | # 3. More complex example with comments and columns to skip 76 | ############################################################################# 77 | ary3 = loadtxt("complex_data_file.txt", delimiter=",", comments="%", 78 | usecols=(0, 1, 2, 4), dtype=int, skiprows=1) 79 | 80 | print('example 3:') 81 | print(ary3) 82 | -------------------------------------------------------------------------------- /exercises/sinc_function/sinc_function.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Since Function\n", 8 | "Topics: Broadcasting, Fancy Indexing\n", 9 | "\n", 10 | "The [sinc function](https://en.wikipedia.org/wiki/Sinc_function) is defined by: \n", 11 | "\n", 12 | "$$ \\large sinc(r) = \\frac{sin(r)}{r} $$\n", 13 | "\n", 14 | "\n", 15 | "1. Create a Cartesian x,y grid by defining `x` and `y` as arrays of evenly spaced numbers over the interval `[-15, 15]`. Each of these arrays should have `101` numbers.\n", 16 | "\n", 17 | "2. Reshape the `y` array into a \"column\" vector by increasing the dimensionality using array slicing and `newaxis`.\n", 18 | "\n", 19 | "3. Create an array of `r` that corresponds to the following equation:\n", 20 | "\n", 21 | "$$ \\large r = \\sqrt{x^2 + y^2} $$\n", 22 | "\n", 23 | "\n", 24 | "4. Calculate the sinc function of `r`.\n", 25 | "\n", 26 | "5. Replace any location in `sinc` where `r` is `0` with `1.0`.\n", 27 | "\n", 28 | "\n", 29 | "## Bonus\n", 30 | "6. Plot the result of the sinc function using `matplotlib`'s `plt.imshow` function." 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "%matplotlib inline\n", 40 | "from numpy import linspace, sin, sqrt, newaxis\n", 41 | "import matplotlib.pyplot as plt" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "1. Create a Cartesian x,y grid by defining `x` and `y` as arrays of evenly spaced numbers over the interval `[-15, 15]`. Each of these arrays should have `101` numbers." 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "# Your code goes here" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "2. Reshape the y array into a \"column\" vector by increasing the dimensionality using array slicing and newaxis." 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": null, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "y = # Your code goes here" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "3. Create an array of `r` that corresponds to the following equation:\n", 81 | "\n", 82 | "$$ \\large r = \\sqrt{x^2 + y^2} $$" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": null, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "# Your code goes here" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "4. Calculate the sinc function of `r`" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "sinc = # Your code goes here" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "5. Replace any location in `sinc` where `r` is `0` with `1.0`." 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [ 123 | "# Your code goes here" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "## Bonus\n", 131 | "6. Plot the result of the sinc function using `matplotlib`'s `plt.imshow` function." 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "# Your code goes here" 141 | ] 142 | } 143 | ], 144 | "metadata": { 145 | "kernelspec": { 146 | "display_name": "Python 3", 147 | "language": "python", 148 | "name": "python3" 149 | }, 150 | "language_info": { 151 | "codemirror_mode": { 152 | "name": "ipython", 153 | "version": 3 154 | }, 155 | "file_extension": ".py", 156 | "mimetype": "text/x-python", 157 | "name": "python", 158 | "nbconvert_exporter": "python", 159 | "pygments_lexer": "ipython3", 160 | "version": "3.6.13" 161 | } 162 | }, 163 | "nbformat": 4, 164 | "nbformat_minor": 4 165 | } 166 | -------------------------------------------------------------------------------- /exercises/sinc_function/sinc_function.py: -------------------------------------------------------------------------------- 1 | """ 2 | Sinc Function 3 | ------------- 4 | 5 | Topics: Broadcasting, Fancy Indexing 6 | 7 | Calculate the sinc function: sin(r)/r. Use a Cartesian x,y grid 8 | and calculate ``r = sqrt(x**2+y**2)`` with 0 in the center of the grid. 9 | Calculate the function for -15,15 for both x and y. 10 | 11 | See :ref:`sinc-function-solution`. 12 | """ 13 | 14 | from numpy import linspace, sin, sqrt, newaxis 15 | import matplotlib.pyplot as plt 16 | -------------------------------------------------------------------------------- /exercises/sinc_function/sinc_function_solution.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Since Function Solution\n", 8 | "Topics: Broadcasting, Fancy Indexing\n", 9 | "\n", 10 | "The [sinc function](https://en.wikipedia.org/wiki/Sinc_function) is defined by: \n", 11 | "\n", 12 | "$$ \\large sinc(r) = \\frac{sin(r)}{r} $$\n", 13 | "\n", 14 | "\n", 15 | "1. Create a Cartesian x,y grid by defining `x` and `y` as arrays of evenly spaced numbers over the interval `[-15, 15]`. Each of these arrays should have `101` numbers.\n", 16 | "\n", 17 | "2. Reshape the `y` array into a \"column\" vector by increasing the dimensionality using array slicing and `newaxis`.\n", 18 | "\n", 19 | "3. Create an array of `r` that corresponds to the following equation:\n", 20 | "\n", 21 | "$$ \\large r = \\sqrt{x^2 + y^2} $$\n", 22 | "\n", 23 | "\n", 24 | "4. Calculate the sinc function of `r`.\n", 25 | "\n", 26 | "5. Replace any location in `sinc` where `r` is `0` with `1.0`.\n", 27 | "\n", 28 | "\n", 29 | "## Bonus\n", 30 | "6. Plot the result of the sinc function using `matplotlib`'s `plt.imshow` function." 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "%matplotlib inline\n", 40 | "from numpy import linspace, sin, sqrt, newaxis\n", 41 | "import matplotlib.pyplot as plt" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "1. Create a Cartesian x,y grid by defining `x` and `y` as arrays of evenly spaced numbers over the interval `[-15, 15]`. Each of these arrays should have `101` numbers." 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "x = linspace(-15, 15, 101)\n", 58 | "y = linspace(-15, 15, 101)" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "2. Reshape the y array into a \"column\" vector by increasing the dimensionality using array slicing and newaxis." 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": null, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "y = y[:, newaxis]" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "3. Create an array of `r` that corresponds to the following equation:\n", 82 | "\n", 83 | "$$ \\large r = \\sqrt{x^2 + y^2} $$" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": null, 89 | "metadata": {}, 90 | "outputs": [], 91 | "source": [ 92 | "r = sqrt(x**2 + y**2)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "4. Calculate the sinc function of `r`" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": null, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [ 108 | "sinc = sin(r)/r" 109 | ] 110 | }, 111 | { 112 | "cell_type": "markdown", 113 | "metadata": {}, 114 | "source": [ 115 | "5. Replace any location in `sinc` where `r` is `0` with `1.0`." 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "sinc[r==0] = 1.0" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "## Bonus\n", 132 | "6. Plot the result of the sinc function using `matplotlib`'s `plt.imshow` function." 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": null, 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "plt.imshow(sinc)\n", 142 | "plt.colorbar();" 143 | ] 144 | } 145 | ], 146 | "metadata": { 147 | "kernelspec": { 148 | "display_name": "Python 3", 149 | "language": "python", 150 | "name": "python3" 151 | }, 152 | "language_info": { 153 | "codemirror_mode": { 154 | "name": "ipython", 155 | "version": 3 156 | }, 157 | "file_extension": ".py", 158 | "mimetype": "text/x-python", 159 | "name": "python", 160 | "nbconvert_exporter": "python", 161 | "pygments_lexer": "ipython3", 162 | "version": "3.6.13" 163 | } 164 | }, 165 | "nbformat": 4, 166 | "nbformat_minor": 4 167 | } 168 | -------------------------------------------------------------------------------- /exercises/sinc_function/sinc_function_solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | Topics: Broadcasting, Fancy Indexing 3 | 4 | Calculate the sinc function: sin(r)/r. Use a Cartesian x,y grid 5 | and calculate ``r = sqrt(x**2+y**2)`` with 0 in the center of the grid. 6 | Calculate the function for -15,15 for both x and y. 7 | """ 8 | 9 | from numpy import linspace, sin, sqrt, newaxis 10 | import matplotlib.pyplot as plt 11 | 12 | x = linspace(-15,15,101) 13 | # flip y up so that it is a "column" vector. 14 | y = linspace(-15,15,101)[:,newaxis] 15 | 16 | # because of broadcasting rules, r is 2D. 17 | r = sqrt(x**2+y**2) 18 | 19 | # calculate our function. 20 | sinc = sin(r)/r 21 | 22 | # replace any location where r is 0 with 1.0 23 | sinc[r==0] = 1.0 24 | 25 | plt.imshow(sinc, extent=[-15,15,-15,15]) 26 | plt.gray() 27 | plt.show() 28 | -------------------------------------------------------------------------------- /exercises/wind_statistics/wind.desc: -------------------------------------------------------------------------------- 1 | wind daily average wind speeds for 1961-1978 at 12 synoptic meteorological 2 | stations in the Republic of Ireland (Haslett and raftery 1989). 3 | 4 | These data were analyzed in detail in the following article: 5 | Haslett, J. and Raftery, A. E. (1989). Space-time Modelling with 6 | Long-memory Dependence: Assessing Ireland's Wind Power Resource 7 | (with Discussion). Applied Statistics 38, 1-50. 8 | 9 | Each line corresponds to one day of data in the following format: 10 | year, month, day, average wind speed at each of the stations in the order given 11 | in Fig.4 of Haslett and Raftery : 12 | RPT, VAL, ROS, KIL, SHA, BIR, DUB, CLA, MUL, CLO, BEL, MAL 13 | 14 | Fortan format : ( i2, 2i3, 12f6.2) 15 | 16 | The data are in knots, not in m/s. 17 | 18 | Permission granted for unlimited distribution. 19 | 20 | Please report all anomalies to fraley@stat.washington.edu 21 | 22 | Be aware that the dataset is 532494 bytes long (thats over half a 23 | Megabyte). Please be sure you want the data before you request it. 24 | -------------------------------------------------------------------------------- /exercises/wind_statistics/wind_statistics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Wind Statistics\n", 8 | "\n", 9 | "Topics: Using array methods over different axes, fancy indexing.\n", 10 | "\n", 11 | "1. The data in 'wind.data' has the following format:\n", 12 | "\n", 13 | "```\n", 14 | "61 1 1 15.04 14.96 13.17 9.29 13.96 9.87 13.67 10.25 10.83 12.58 18.50 15.04\n", 15 | "61 1 2 14.71 16.88 10.83 6.50 12.62 7.67 11.50 10.04 9.79 9.67 17.54 13.83\n", 16 | "61 1 3 18.50 16.88 12.33 10.13 11.17 6.17 11.25 8.04 8.50 7.67 12.75 12.71\n", 17 | "```\n", 18 | "\n", 19 | "The first three columns are year, month and day. The\n", 20 | "remaining 12 columns are average windspeeds in knots at 12\n", 21 | "locations in Ireland on that day. \n", 22 | "\n", 23 | "You should be able to solve questions 2–7 without using a for loop or other looping construct.\n", 24 | "\n", 25 | "Start by using the 'loadtxt' function from numpy to read the data into an array. " 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "from numpy import loadtxt" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": null, 40 | "metadata": {}, 41 | "outputs": [], 42 | "source": [] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "2. Calculate the min, max and mean windspeeds and standard deviation of the windspeeds over all the locations and all the times (a single set of numbers for the entire dataset)." 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "3. Calculate the min, max and mean windspeeds and standard deviations of the windspeeds at each location over all the days (a different set of numbers for each location)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "4. Calculate the min, max and mean windspeed and standard deviations of the windspeeds across all the locations at each day (a different set of numbers for each day)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "5. Find the location which has the greatest windspeed on each day (an integer column number for each day)." 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": null, 96 | "metadata": {}, 97 | "outputs": [], 98 | "source": [] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "6. Find the year, month and day on which the greatest windspeed was recorded." 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "7. Find the average windspeed in January for each location." 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "metadata": {}, 125 | "outputs": [], 126 | "source": [] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "## Bonus\n", 133 | "\n", 134 | "\n", 135 | "1. Calculate the mean windspeed for each month in the dataset. Treat\n", 136 | " January 1961 and January 1962 as *different* months. (hint: first find a\n", 137 | " way to create an identifier unique for each month. The second step might\n", 138 | " require a for loop.)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": null, 144 | "metadata": {}, 145 | "outputs": [], 146 | "source": [] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": {}, 151 | "source": [ 152 | "2. Calculate the min, max and mean windspeeds and standard deviations of the\n", 153 | " windspeeds across all locations for each week (assume that the first week\n", 154 | " starts on January 1 1961) for the first 52 weeks. This can be done without\n", 155 | " any for loop." 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": null, 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "metadata": {}, 168 | "source": [ 169 | "## Bonus Bonus\n", 170 | "\n", 171 | "Calculate the mean windspeed for each month without using a for loop.\n", 172 | "(Hint: look at `searchsorted` and `add.reduceat`.)" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": null, 178 | "metadata": {}, 179 | "outputs": [], 180 | "source": [] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "metadata": {}, 185 | "source": [ 186 | "## Notes\n", 187 | "\n", 188 | "These data were analyzed in detail in the following article:\n", 189 | "\n", 190 | " Haslett, J. and Raftery, A. E. (1989). Space-time Modelling with\n", 191 | " Long-memory Dependence: Assessing Ireland's Wind Power Resource\n", 192 | " (with Discussion). Applied Statistics 38, 1-50." 193 | ] 194 | } 195 | ], 196 | "metadata": { 197 | "kernelspec": { 198 | "display_name": "Python 3", 199 | "language": "python", 200 | "name": "python3" 201 | }, 202 | "language_info": { 203 | "codemirror_mode": { 204 | "name": "ipython", 205 | "version": 3 206 | }, 207 | "file_extension": ".py", 208 | "mimetype": "text/x-python", 209 | "name": "python", 210 | "nbconvert_exporter": "python", 211 | "pygments_lexer": "ipython3", 212 | "version": "3.6.10" 213 | } 214 | }, 215 | "nbformat": 4, 216 | "nbformat_minor": 2 217 | } 218 | -------------------------------------------------------------------------------- /exercises/wind_statistics/wind_statistics.py: -------------------------------------------------------------------------------- 1 | """ 2 | Wind Statistics 3 | ---------------- 4 | 5 | Topics: Using array methods over different axes, fancy indexing. 6 | 7 | 1. The data in 'wind.data' has the following format:: 8 | 9 | 61 1 1 15.04 14.96 13.17 9.29 13.96 9.87 13.67 10.25 10.83 12.58 18.50 15.04 10 | 61 1 2 14.71 16.88 10.83 6.50 12.62 7.67 11.50 10.04 9.79 9.67 17.54 13.83 11 | 61 1 3 18.50 16.88 12.33 10.13 11.17 6.17 11.25 8.04 8.50 7.67 12.75 12.71 12 | 13 | The first three columns are year, month and day. The 14 | remaining 12 columns are average windspeeds in knots at 12 15 | locations in Ireland on that day. 16 | 17 | Use the 'loadtxt' function from numpy to read the data into 18 | an array. 19 | 20 | 2. Calculate the min, max and mean windspeeds and standard deviation of the 21 | windspeeds over all the locations and all the times (a single set of numbers 22 | for the entire dataset). 23 | 24 | 3. Calculate the min, max and mean windspeeds and standard deviations of the 25 | windspeeds at each location over all the days (a different set of numbers 26 | for each location) 27 | 28 | 4. Calculate the min, max and mean windspeed and standard deviations of the 29 | windspeeds across all the locations at each day (a different set of numbers 30 | for each day) 31 | 32 | 5. Find the location which has the greatest windspeed on each day (an integer 33 | column number for each day). 34 | 35 | 6. Find the year, month and day on which the greatest windspeed was recorded. 36 | 37 | 7. Find the average windspeed in January for each location. 38 | 39 | You should be able to perform all of these operations without using a for 40 | loop or other looping construct. 41 | 42 | Bonus 43 | ~~~~~ 44 | 45 | 1. Calculate the mean windspeed for each month in the dataset. Treat 46 | January 1961 and January 1962 as *different* months. (hint: first find a 47 | way to create an identifier unique for each month. The second step might 48 | require a for loop.) 49 | 50 | 2. Calculate the min, max and mean windspeeds and standard deviations of the 51 | windspeeds across all locations for each week (assume that the first week 52 | starts on January 1 1961) for the first 52 weeks. This can be done without 53 | any for loop. 54 | 55 | Bonus Bonus 56 | ~~~~~~~~~~~ 57 | 58 | Calculate the mean windspeed for each month without using a for loop. 59 | (Hint: look at `searchsorted` and `add.reduceat`.) 60 | 61 | Notes 62 | ~~~~~ 63 | 64 | These data were analyzed in detail in the following article: 65 | 66 | Haslett, J. and Raftery, A. E. (1989). Space-time Modelling with 67 | Long-memory Dependence: Assessing Ireland's Wind Power Resource 68 | (with Discussion). Applied Statistics 38, 1-50. 69 | 70 | 71 | See :ref:`wind-statistics-solution`. 72 | """ 73 | 74 | from numpy import loadtxt 75 | 76 | -------------------------------------------------------------------------------- /exercises/wind_statistics/wind_statistics_solution.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Wind Statistics\n", 8 | "\n", 9 | "Topics: Using array methods over different axes, fancy indexing.\n", 10 | "\n", 11 | "1. The data in 'wind.data' has the following format:\n", 12 | "\n", 13 | "```\n", 14 | "61 1 1 15.04 14.96 13.17 9.29 13.96 9.87 13.67 10.25 10.83 12.58 18.50 15.04\n", 15 | "61 1 2 14.71 16.88 10.83 6.50 12.62 7.67 11.50 10.04 9.79 9.67 17.54 13.83\n", 16 | "61 1 3 18.50 16.88 12.33 10.13 11.17 6.17 11.25 8.04 8.50 7.67 12.75 12.71\n", 17 | "```\n", 18 | "\n", 19 | "The first three columns are year, month and day. The\n", 20 | "remaining 12 columns are average windspeeds in knots at 12\n", 21 | "locations in Ireland on that day. \n", 22 | "\n", 23 | "You should be able to solve questions 2–7 without using a for loop or other looping construct.\n", 24 | "\n", 25 | "Start by using the 'loadtxt' function from numpy to read the data into an array. " 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "from numpy import (loadtxt, arange, searchsorted, add, zeros, unravel_index,\n", 35 | " where)\n", 36 | "\n", 37 | "wind_data = loadtxt('wind.data')\n", 38 | "\n", 39 | "data = wind_data[:, 3:]" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "2. Calculate the min, max and mean windspeeds and standard deviation of the\n", 47 | " windspeeds over all the locations and all the times (a single set of numbers\n", 48 | " for the entire dataset)." 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "print('2. Statistics over all values')\n", 58 | "print(' min:', data.min())\n", 59 | "print(' max:', data.max())\n", 60 | "print(' mean:', data.mean())\n", 61 | "print(' standard deviation:', data.std())" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "3. Calculate the min, max and mean windspeeds and standard deviations of the\n", 69 | " windspeeds at each location over all the days (a different set of numbers\n", 70 | " for each location)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "print('3. Statistics over all days at each location')\n", 80 | "print(' min:', data.min(axis=0))\n", 81 | "print(' max:', data.max(axis=0))\n", 82 | "print(' mean:', data.mean(axis=0))\n", 83 | "print(' standard deviation:', data.std(axis=0))" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "4. Calculate the min, max and mean windspeed and standard deviations of the\n", 91 | " windspeeds across all the locations at each day (a different set of numbers\n", 92 | " for each day)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": null, 98 | "metadata": {}, 99 | "outputs": [], 100 | "source": [ 101 | "print('4. Statistics over all locations for each day')\n", 102 | "print(' min:', data.min(axis=1))\n", 103 | "print(' max:', data.max(axis=1))\n", 104 | "print(' mean:', data.mean(axis=1))\n", 105 | "print(' standard deviation:', data.std(axis=1))" 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": {}, 111 | "source": [ 112 | "5. Find the location which has the greatest windspeed on each day (an integer\n", 113 | " column number for each day)." 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": null, 119 | "metadata": {}, 120 | "outputs": [], 121 | "source": [ 122 | "print('5. Location of daily maximum')\n", 123 | "print(' daily max location:', data.argmax(axis=1))" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "6. Find the year, month and day on which the greatest windspeed was recorded." 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "daily_max = data.max(axis=1)\n", 140 | "max_row = daily_max.argmax()\n", 141 | "# Note: Another way to do this would be to use the unravel_index function\n", 142 | "# which takes a linear index and convert it to a location given the shape\n", 143 | "# of the array:\n", 144 | "max_row, max_col = unravel_index(data.argmax(), data.shape)\n", 145 | "# Or you could use \"where\", which identifies *all* the places where the max\n", 146 | "# occurs, rather than just the first. Note that \"where\" returns two arrays in\n", 147 | "# this case, instead of two integers.\n", 148 | "max_row, max_col = where(data == data.max())\n", 149 | "\n", 150 | "print('6. Day of maximum reading')\n", 151 | "print(' Year:', int(wind_data[max_row, 0]))\n", 152 | "print(' Month:', int(wind_data[max_row, 1]))\n", 153 | "print(' Day:', int(wind_data[max_row, 2]))" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "7. Find the average windspeed in January for each location.\n" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": null, 166 | "metadata": {}, 167 | "outputs": [], 168 | "source": [ 169 | "january_indices = wind_data[:, 1] == 1\n", 170 | "january_data = data[january_indices]\n", 171 | "\n", 172 | "print('7. Statistics for January')\n", 173 | "print(' mean:', january_data.mean(axis=0))" 174 | ] 175 | }, 176 | { 177 | "cell_type": "markdown", 178 | "metadata": {}, 179 | "source": [ 180 | "## Bonus\n", 181 | "\n", 182 | "1. Calculate the mean windspeed for each month in the dataset. Treat\n", 183 | " January 1961 and January 1962 as *different* months." 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": null, 189 | "metadata": {}, 190 | "outputs": [], 191 | "source": [ 192 | "# compute the month number for each day in the dataset\n", 193 | "months = (wind_data[:, 0] - 61) * 12 + wind_data[:, 1] - 1\n", 194 | "\n", 195 | "# we're going to use the month values as indices, so we need\n", 196 | "# them to be integers\n", 197 | "months = months.astype(int)\n", 198 | "\n", 199 | "# get set of unique months\n", 200 | "month_values = set(months)\n", 201 | "\n", 202 | "# initialize an array to hold the result\n", 203 | "monthly_means = zeros(len(month_values))\n", 204 | "\n", 205 | "for month in month_values:\n", 206 | " # find the rows that correspond to the current month\n", 207 | " day_indices = (months == month)\n", 208 | "\n", 209 | " # extract the data for the current month using fancy indexing\n", 210 | " month_data = data[day_indices]\n", 211 | "\n", 212 | " # find the mean\n", 213 | " monthly_means[month] = month_data.mean()\n", 214 | "\n", 215 | " # Note: experts might do this all-in one\n", 216 | " # monthly_means[month] = data[months==month].mean()\n", 217 | "\n", 218 | "# In fact the whole for loop could reduce to the following one-liner\n", 219 | "# monthly_means = array([data[months==month].mean() for month in month_values])\n", 220 | "\n", 221 | "print(\"Bonus 1.\")\n", 222 | "print(\" mean:\", monthly_means)" 223 | ] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "metadata": {}, 228 | "source": [ 229 | "\n", 230 | "2. Calculate the min, max and mean windspeeds and standard deviations of the\n", 231 | " windspeeds across all locations for each week (assume that the first week\n", 232 | " starts on January 1 1961) for the first 52 weeks." 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": null, 238 | "metadata": {}, 239 | "outputs": [], 240 | "source": [ 241 | "# Bonus 2.\n", 242 | "# Extract the data for the first 52 weeks. Then reshape the array to put\n", 243 | "# on the same line 7 days worth of data for all locations. Let Numpy\n", 244 | "# figure out the number of lines needed to do so\n", 245 | "weekly_data = data[:52 * 7].reshape(-1, 7 * 12)\n", 246 | "\n", 247 | "print('Bonus 2. Weekly statistics over all locations')\n", 248 | "print(' min:', weekly_data.min(axis=1))\n", 249 | "print(' max:', weekly_data.max(axis=1))\n", 250 | "print(' mean:', weekly_data.mean(axis=1))\n", 251 | "print(' standard deviation:', weekly_data.std(axis=1))" 252 | ] 253 | }, 254 | { 255 | "cell_type": "markdown", 256 | "metadata": {}, 257 | "source": [ 258 | "## Bonus Bonus: this is really tricky...\n", 259 | "\n", 260 | "Calculate the mean windspeed for each month without using a for loop.\n", 261 | "(Hint: look at `searchsorted` and `add.reduceat`.)" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": null, 267 | "metadata": {}, 268 | "outputs": [], 269 | "source": [ 270 | "# compute the month number for each day in the dataset\n", 271 | "months = (wind_data[:, 0] - 61) * 12 + wind_data[:, 1] - 1\n", 272 | "\n", 273 | "# find the indices for the start of each month\n", 274 | "# this is a useful trick - we use range from 0 to the\n", 275 | "# number of months + 1 and searchsorted to find the insertion\n", 276 | "# points for each.\n", 277 | "month_indices = searchsorted(months, arange(months[-1] + 2))\n", 278 | "\n", 279 | "# now use add.reduceat to get the sum at each location\n", 280 | "monthly_loc_totals = add.reduceat(data, month_indices[:-1])\n", 281 | "\n", 282 | "# now use add to find the sum across all locations for each month\n", 283 | "monthly_totals = monthly_loc_totals.sum(axis=1)\n", 284 | "\n", 285 | "# now find total number of measurements for each month\n", 286 | "month_days = month_indices[1:] - month_indices[:-1]\n", 287 | "measurement_count = month_days * 12\n", 288 | "\n", 289 | "# compute the mean\n", 290 | "monthly_means = monthly_totals / measurement_count\n", 291 | "\n", 292 | "print(\"Bonus Bonus\")\n", 293 | "print(\" mean:\", monthly_means)\n", 294 | "\n", 295 | "# Notes: this method relies on the fact that the months are contiguous in the\n", 296 | "# data set - the method used in the bonus section works for non-contiguous\n", 297 | "# days." 298 | ] 299 | }, 300 | { 301 | "cell_type": "markdown", 302 | "metadata": {}, 303 | "source": [ 304 | "## Notes\n", 305 | "\n", 306 | "These data were analyzed in detail in the following article:\n", 307 | "\n", 308 | " Haslett, J. and Raftery, A. E. (1989). Space-time Modelling with\n", 309 | " Long-memory Dependence: Assessing Ireland's Wind Power Resource\n", 310 | " (with Discussion). Applied Statistics 38, 1-50." 311 | ] 312 | } 313 | ], 314 | "metadata": { 315 | "kernelspec": { 316 | "display_name": "Python 3", 317 | "language": "python", 318 | "name": "python3" 319 | }, 320 | "language_info": { 321 | "codemirror_mode": { 322 | "name": "ipython", 323 | "version": 3 324 | }, 325 | "file_extension": ".py", 326 | "mimetype": "text/x-python", 327 | "name": "python", 328 | "nbconvert_exporter": "python", 329 | "pygments_lexer": "ipython3", 330 | "version": "3.6.10" 331 | } 332 | }, 333 | "nbformat": 4, 334 | "nbformat_minor": 2 335 | } 336 | -------------------------------------------------------------------------------- /exercises/wind_statistics/wind_statistics_solution.py: -------------------------------------------------------------------------------- 1 | """ 2 | Wind Statistics 3 | ---------------- 4 | 5 | Topics: Using array methods over different axes, fancy indexing. 6 | 7 | 1. The data in 'wind.data' has the following format:: 8 | 9 | 61 1 1 15.04 14.96 13.17 9.29 13.96 9.87 13.67 10.25 10.83 12.58 18.50 15.04 10 | 61 1 2 14.71 16.88 10.83 6.50 12.62 7.67 11.50 10.04 9.79 9.67 17.54 13.83 11 | 61 1 3 18.50 16.88 12.33 10.13 11.17 6.17 11.25 8.04 8.50 7.67 12.75 12.71 12 | 13 | The first three columns are year, month and day. The 14 | remaining 12 columns are average windspeeds in knots at 12 15 | locations in Ireland on that day. 16 | 17 | Use the 'loadtxt' function from numpy to read the data into 18 | an array. 19 | 20 | 2. Calculate the min, max and mean windspeeds and standard deviation of the 21 | windspeeds over all the locations and all the times (a single set of numbers 22 | for the entire dataset). 23 | 24 | 3. Calculate the min, max and mean windspeeds and standard deviations of the 25 | windspeeds at each location over all the days (a different set of numbers 26 | for each location) 27 | 28 | 4. Calculate the min, max and mean windspeed and standard deviations of the 29 | windspeeds across all the locations at each day (a different set of numbers 30 | for each day) 31 | 32 | 5. Find the location which has the greatest windspeed on each day (an integer 33 | column number for each day). 34 | 35 | 6. Find the year, month and day on which the greatest windspeed was recorded. 36 | 37 | 7. Find the average windspeed in January for each location. 38 | 39 | You should be able to perform all of these operations without using a for 40 | loop or other looping construct. 41 | 42 | Bonus 43 | ~~~~~ 44 | 45 | 1. Calculate the mean windspeed for each month in the dataset. Treat 46 | January 1961 and January 1962 as *different* months. 47 | 48 | 2. Calculate the min, max and mean windspeeds and standard deviations of the 49 | windspeeds across all locations for each week (assume that the first week 50 | starts on January 1 1961) for the first 52 weeks. 51 | 52 | Bonus Bonus 53 | ~~~~~~~~~~~ 54 | 55 | Calculate the mean windspeed for each month without using a for loop. 56 | (Hint: look at `searchsorted` and `add.reduceat`.) 57 | 58 | Notes 59 | ~~~~~ 60 | 61 | These data were analyzed in detail in the following article: 62 | 63 | Haslett, J. and Raftery, A. E. (1989). Space-time Modelling with 64 | Long-memory Dependence: Assessing Ireland's Wind Power Resource 65 | (with Discussion). Applied Statistics 38, 1-50. 66 | 67 | """ 68 | from __future__ import print_function 69 | from numpy import (loadtxt, arange, searchsorted, add, zeros, unravel_index, 70 | where) 71 | 72 | wind_data = loadtxt('wind.data') 73 | 74 | data = wind_data[:, 3:] 75 | 76 | print('2. Statistics over all values') 77 | print(' min:', data.min()) 78 | print(' max:', data.max()) 79 | print(' mean:', data.mean()) 80 | print(' standard deviation:', data.std()) 81 | print() 82 | 83 | print('3. Statistics over all days at each location') 84 | print(' min:', data.min(axis=0)) 85 | print(' max:', data.max(axis=0)) 86 | print(' mean:', data.mean(axis=0)) 87 | print(' standard deviation:', data.std(axis=0)) 88 | print() 89 | 90 | print('4. Statistics over all locations for each day') 91 | print(' min:', data.min(axis=1)) 92 | print(' max:', data.max(axis=1)) 93 | print(' mean:', data.mean(axis=1)) 94 | print(' standard deviation:', data.std(axis=1)) 95 | print() 96 | 97 | print('5. Location of daily maximum') 98 | print(' daily max location:', data.argmax(axis=1)) 99 | print() 100 | 101 | daily_max = data.max(axis=1) 102 | max_row = daily_max.argmax() 103 | # Note: Another way to do this would be to use the unravel_index function 104 | # which takes a linear index and convert it to a location given the shape 105 | # of the array: 106 | max_row, max_col = unravel_index(data.argmax(), data.shape) 107 | # Or you could use "where", which identifies *all* the places where the max 108 | # occurs, rather than just the first. Note that "where" returns two arrays in 109 | # this case, instead of two integers. 110 | max_row, max_col = where(data == data.max()) 111 | 112 | 113 | print('6. Day of maximum reading') 114 | print(' Year:', int(wind_data[max_row, 0])) 115 | print(' Month:', int(wind_data[max_row, 1])) 116 | print(' Day:', int(wind_data[max_row, 2])) 117 | print() 118 | 119 | january_indices = wind_data[:, 1] == 1 120 | january_data = data[january_indices] 121 | 122 | print('7. Statistics for January') 123 | print(' mean:', january_data.mean(axis=0)) 124 | print() 125 | 126 | # Bonus 127 | 128 | # compute the month number for each day in the dataset 129 | months = (wind_data[:, 0] - 61) * 12 + wind_data[:, 1] - 1 130 | 131 | # we're going to use the month values as indices, so we need 132 | # them to be integers 133 | months = months.astype(int) 134 | 135 | # get set of unique months 136 | month_values = set(months) 137 | 138 | # initialize an array to hold the result 139 | monthly_means = zeros(len(month_values)) 140 | 141 | for month in month_values: 142 | # find the rows that correspond to the current month 143 | day_indices = (months == month) 144 | 145 | # extract the data for the current month using fancy indexing 146 | month_data = data[day_indices] 147 | 148 | # find the mean 149 | monthly_means[month] = month_data.mean() 150 | 151 | # Note: experts might do this all-in one 152 | # monthly_means[month] = data[months==month].mean() 153 | 154 | # In fact the whole for loop could reduce to the following one-liner 155 | # monthly_means = array([data[months==month].mean() for month in month_values]) 156 | 157 | 158 | print("Bonus 1.") 159 | print(" mean:", monthly_means) 160 | print() 161 | 162 | # Bonus 2. 163 | # Extract the data for the first 52 weeks. Then reshape the array to put 164 | # on the same line 7 days worth of data for all locations. Let Numpy 165 | # figure out the number of lines needed to do so 166 | weekly_data = data[:52 * 7].reshape(-1, 7 * 12) 167 | 168 | print('Bonus 2. Weekly statistics over all locations') 169 | print(' min:', weekly_data.min(axis=1)) 170 | print(' max:', weekly_data.max(axis=1)) 171 | print(' mean:', weekly_data.mean(axis=1)) 172 | print(' standard deviation:', weekly_data.std(axis=1)) 173 | print() 174 | 175 | # Bonus Bonus : this is really tricky... 176 | 177 | # compute the month number for each day in the dataset 178 | months = (wind_data[:, 0] - 61) * 12 + wind_data[:, 1] - 1 179 | 180 | # find the indices for the start of each month 181 | # this is a useful trick - we use range from 0 to the 182 | # number of months + 1 and searchsorted to find the insertion 183 | # points for each. 184 | month_indices = searchsorted(months, arange(months[-1] + 2)) 185 | 186 | # now use add.reduceat to get the sum at each location 187 | monthly_loc_totals = add.reduceat(data, month_indices[:-1]) 188 | 189 | # now use add to find the sum across all locations for each month 190 | monthly_totals = monthly_loc_totals.sum(axis=1) 191 | 192 | # now find total number of measurements for each month 193 | month_days = month_indices[1:] - month_indices[:-1] 194 | measurement_count = month_days * 12 195 | 196 | # compute the mean 197 | monthly_means = monthly_totals / measurement_count 198 | 199 | print("Bonus Bonus") 200 | print(" mean:", monthly_means) 201 | 202 | # Notes: this method relies on the fact that the months are contiguous in the 203 | # data set - the method used in the bonus section works for non-contiguous 204 | # days. 205 | -------------------------------------------------------------------------------- /history/session1.txt: -------------------------------------------------------------------------------- 1 | SciPy 2021 Tutorial: Introduction to Numerical Computing With NumPy 2 | Presented by: Logan Thomas, Enthought, Inc. 3 | Monday, July 12th 2021. 4 | 5 | # Session 1 6 | 7 | a = [1, 2, 3, 4, 5] 8 | b = [2, 4, 6, 8, 10] 9 | a + b 10 | result = [] 11 | for a_val, b_val in zip(a,b): 12 | result.append(a_val + b_val) 13 | result 14 | import numpy as np 15 | np_a = np.array([1,2,3,4,5]) 16 | np_b = np.array([2,4,6,8,10]) 17 | np_a + np+b 18 | np_a + np_b 19 | clear 20 | np_a 21 | np_.dtype 22 | np_a.dtype 23 | np_a 24 | np_a[0] = 10.5 25 | np_a 26 | # universal function (ufunc( 27 | # universal function (ufunc) 28 | np.sin(np_a) 29 | np.cos(np_a): 30 | type(np_a) 31 | a = np.array([[1,2,3],[4,5,6]]) 32 | a 33 | a[1,1] 34 | l = a.tolist() 35 | l 36 | l[1,1] 37 | l[1] 38 | l[1][1] 39 | clear 40 | np.array([0, 1, 2, 3]) 41 | np.array(range(5)) 42 | a = np.array([0,1,2,3]) 43 | a 44 | print(a) 45 | type(a) 46 | a.dtype 47 | a = np.array([1,2,3], dtype='int32') 48 | a 49 | a.dtype 50 | a 51 | a.astype('int64') 52 | b = a.astype('int64') 53 | b.dtype 54 | a 55 | b 56 | a.ndim 57 | a 58 | a = np.array([[1,2,3], [4,5,6]]) 59 | a 60 | a.ndim 61 | a.shape 62 | a.itemsize 63 | a 64 | a.size 65 | a.shape 66 | a.size 67 | a.dtype 68 | # 8 bits = 1 byes -> 8 bytes in a 64 bit int 69 | a 70 | a.itemsize 71 | a.nbytes 72 | 6*8 73 | a = np.array([1, 2, 3, 4,]) 74 | b = np.array([2, 3, 4, 5]) 75 | a + b 76 | a * b 77 | a ** b 78 | np.array(range(5)) 79 | np.arange(5) 80 | x = np.arange(11.0) 81 | x 82 | x.dtype 83 | c = np.arange(11) 84 | c 85 | c.dtype 86 | x 87 | x * 5 88 | np.pi 89 | np.e 90 | x = (2.0 * np.pi) / 10.0 91 | x 92 | x = (2.0 * np.pi) / 10.0 93 | c = (2.0 * np.pi) / 10.0 94 | c 95 | x = np.arange(11.0) 96 | x 97 | c * x 98 | x = c * x 99 | x 100 | x *= c # x = c * x 101 | x = np.arange(11.0) 102 | x 103 | np.sin(x) 104 | x 105 | x = np.arange(11.0) 106 | x 107 | x[0] 108 | a = np.array([0,1,2,3]) 109 | a 110 | a[0] 111 | a[0] = 10 112 | a 113 | a.dtype 114 | a[0] = 10.6 115 | type(10.6) 116 | a 117 | a = np.array([0, 1, 2, 3], dtype='float64') 118 | a 119 | a[0] = 10.6 120 | a 121 | a = np.array([0,1,2,3]) 122 | a 123 | a.dtype 124 | a.fill(5) 125 | a 126 | a.fill(-4.8) 127 | a 128 | a.dtype 129 | clear 130 | 131 | # Break #1 (5 min) 132 | %history -f session1.txt 133 | -------------------------------------------------------------------------------- /history/session2.txt: -------------------------------------------------------------------------------- 1 | SciPy 2021 Tutorial: Introduction to Numerical Computing With NumPy 2 | Presented by: Logan Thomas, Enthought, Inc. 3 | Monday, July 12th 2021. 4 | 5 | # Session 2 6 | 7 | a = np.arange(6) + np.arange(60, step=10).reshape(6,1) 8 | a 9 | a[0] 10 | a[0, 3:5] 11 | a[4:] 12 | a[4:, 4:] 13 | a[:, 2] 14 | a[2::2, :] 15 | a[2::2, 0::2] 16 | a[2::2, ::2] 17 | a = np.array([0,1,2,3,4]) 18 | a 19 | a[-2:] = 99 20 | a 21 | a[-2:] = [55, 33] 22 | a 23 | a[-2:] = [55, 33, 77] 24 | clear 25 | a = np.arange(25) 26 | a 27 | a = a.reshape(5,5) 28 | a 29 | a[4] 30 | a[4, :] 31 | a[-1] 32 | a[-1, :] 33 | yellow = a[-1, :] 34 | yellow 35 | a[:,::2] 36 | a[:, 1::2] 37 | red = a[:, 1::2] 38 | red 39 | a[1::2 , ] 40 | a[1::2, ::2] 41 | a[1::2, :3:2] 42 | blue = a[1::2, :3:2] 43 | blue 44 | # yellow = a[-1, :] red = a[:, 1::2] blue = a[1::2, :3:2] 45 | a = np.array([0,1,2,3,4]) 46 | a 47 | b = a[2:4] 48 | b 49 | b[0] = 10 50 | b 51 | a 52 | np.shares_memory(a,b) 53 | b = a[2:4].copy() 54 | a 55 | clear 56 | a = np.array([0,1,2,3,4]) 57 | a 58 | b = a[2:4].copy() 59 | b[0] = 10 60 | b 61 | a 62 | 63 | # Calc return exercise (15 min) 64 | pwd 65 | cd desktop 66 | 67 | import numpy as np 68 | a = np.arange(0,80,10) 69 | a 70 | a[[1,2-3]] 71 | a[[1,2,-3]] 72 | a[[1,2,5]] 73 | a[[1,2,5,5,5,5,5]] 74 | a 75 | a[[1,2,5]] = 999 76 | a 77 | a 78 | a[[1,2,3]] 79 | a[np.array([1,2,5])] 80 | a[1,2,3] 81 | a = np.arange(0,80,10) 82 | a 83 | y = a[[1,2,5]] 84 | y 85 | np.shares_memory(a,y) 86 | y[0] = -777 87 | y 88 | a 89 | a 90 | mask = np.array([0,1,1,0,0,1,0,0], dtype=bool) 91 | mask 92 | a[mask] 93 | a > 30 94 | a[a > 30] 95 | mask = a > 30 96 | a[mask] 97 | 98 | # Break 2 (15 minutes) 99 | %history -f session2.txt 100 | -------------------------------------------------------------------------------- /history/session3.txt: -------------------------------------------------------------------------------- 1 | SciPy 2021 Tutorial: Introduction to Numerical Computing With NumPy 2 | Presented by: Logan Thomas, Enthought, Inc. 3 | Monday, July 12th 2021. 4 | 5 | # Session 3 6 | 7 | import numpy as np 8 | a = np.arange(6) + np.arange(60, step=10).reshape(6,1) 9 | a 10 | np.diagonal(a) 11 | np.diagonal(a, 1) 12 | rows = [0, 1, 2, 3, 4] 13 | cols = [1, 2, 3, 4, 5] 14 | a[rows, cols] 15 | list(zip(rows, cols)) 16 | 5%2 17 | a 18 | # Blue (left to right - [10,6,3,19] ) 19 | a = np.arange(25).reshape(5,5) 20 | a 21 | a[ [2,1,0,3], [0,1,3,4] ] 22 | # Blue (top to bottom - [3, 6, 10, 19]) 23 | rows = [0, 1, 2, 3] 24 | cols = [3, 1, 0, 4] 25 | a[rows, cols] 26 | a 27 | a % 3 28 | a % 3 == 0 29 | mask = a%3 == 0 30 | mask 31 | a[mask] 32 | a[a%3 == 0] 33 | type(1) 34 | type(1.0) 35 | type(1.) 36 | np.linspace(0,1,5) 37 | np.linspace(0,1) 38 | np.linspace(0,1).shape 39 | np.logspcae(0,1,5) 40 | np.logspace(0,1,5) 41 | 10**1 42 | 10**0 43 | np.logspace(0,1,5, base=2) 44 | clear 45 | # Rule 1 46 | a = np.array([1,2,3]) 47 | b = np.array([5,6]) 48 | a + b 49 | b = np.array([5]) 50 | a + b 51 | a + 5 52 | b = np.array([5,5,5]) 53 | a + b 54 | a + 2.0 55 | a + 5.0 56 | # Rule 2 57 | a = np.array([1,1,1]) 58 | b = np.array([2,3,4]) 59 | a * b 60 | a+b 61 | a/b 62 | a-b 63 | np.dot(a,b) 64 | np.matmul(a,b) 65 | a@b 66 | # Rule 3 67 | a = np.arange(6).reshape(2,3) 68 | a 69 | np.dot? 70 | a 71 | a.mean() 72 | 0+1+2+3+4+5 73 | 15/6 74 | a.mean(axis=0) 75 | a.shape 76 | a.mean(axis=1) 77 | # Rule 4 78 | np.nan 79 | 5 + np.nan 80 | 5 * np.nan 81 | np.array([1,2,3]) * np.nan 82 | np.array([1,2,3]) + np.nan 83 | a = np.array([1, 2, np.nan, 3)] 84 | a = np.array([1, 2, np.nan, 3]) 85 | a 86 | a.mean() 87 | np.nanmean(a) 88 | 1+2+3 89 | 6/3 90 | 6/4 91 | 92 | # Break 3 (5 min) 93 | %history -f session3.txt 94 | -------------------------------------------------------------------------------- /history/session4.txt: -------------------------------------------------------------------------------- 1 | SciPy 2021 Tutorial: Introduction to Numerical Computing With NumPy 2 | Presented by: Logan Thomas, Enthought, Inc. 3 | Monday, July 12th 2021. 4 | 5 | # Session 4 6 | 7 | # slide 28 8 | a = np.array([[2,3], [0,1]]) 9 | a 10 | np.min() 11 | a.min() 12 | np.min(a) 13 | a.max(axis=0) 14 | a.max(axis=1) 15 | a = np.aragne(6).reshape(2,3) 16 | a = np.arange(6).reshape(2,3) 17 | a 18 | a.mean(axis=0).shape 19 | a.mean(axis=1).shape 20 | a = np.array([[2,3], [0,1]]) 21 | a 22 | a.argmax() 23 | a 24 | np.argmin(a) 25 | a.shape 26 | np.unravel_index(1, (2,2)) 27 | np.unravel_index(3, (2,2)) 28 | np.unravel_index(a.argmax(), a.shape) 29 | a 30 | np.unravel_index(a.argmin(), a.shape) 31 | a = np.arange(-2,2) ** 2 32 | a 33 | a % 2 == 0 34 | a[a%2==0] 35 | np.where(a%2==0) 36 | positives = np.aragne(1,5) 37 | positives = np.arange(1,5) 38 | negatives = -positives 39 | positives 40 | negatives 41 | a 42 | mask = a%2==0 43 | mask 44 | np.where(mask, positives, negatives) 45 | a 46 | b = np.arange(25).reshape(5,5) 47 | b 48 | np.where(b%2==0, b, np.nan) 49 | np.where(b%2==0, b, 1) 50 | np.where(b%2==0, b, 0) 51 | a = np.arange(-15,15).reshape(5,6) ** 2 52 | a 53 | a.max() 54 | a.max(axis=1) 55 | a.mean(axis=0) 56 | np.argmin(a) 57 | a.argmin() 58 | np.unravel_index(a.argmin(), a.shape) 59 | a.ndim 60 | a.shape 61 | np.where(a=0) 62 | np.where(a==0) 63 | np.where(a == a.min()) 64 | b = np.array([1,2,5,5]) 65 | b.argmax() 66 | np.where(b == b.max()) 67 | clear 68 | a = np.arange(6) + np.arange(60, step=10).reshape(6,1) 69 | a 70 | np.arange(6) 71 | np.arange(60, step=10).reshape(6,1) 72 | clear 73 | np.arange(6) 74 | np.arange(60, step=10).reshape(6,1) 75 | a = np.arange(6) + np.arange(60, step=10).reshape(6,1) 76 | a 77 | a.shape 78 | %history -f session4.txt 79 | -------------------------------------------------------------------------------- /introduction_to_numerical_computing_with_numpy_manual.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enthought/Numpy-Tutorial-SciPyConf-2021/6ae55cdf01eb0a0c6df64a179d74ad8ad6de78b8/introduction_to_numerical_computing_with_numpy_manual.pdf --------------------------------------------------------------------------------