{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# EuroSciPy 2018: NumPy tutorial" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Let's do some slicing" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mylist = list(range(10))\n", "print(mylist)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use slicing to produce the following outputs:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[2, 3, 4, 5]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[0, 1, 2, 3, 4]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[6, 7, 8, 9]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[0, 2, 4, 6, 8]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[7, 5, 3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matrices and lists of lists" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "matrix = [[0, 1, 2],\n", " [3, 4, 5],\n", " [6, 7, 8]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get the second row by slicing twice" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try to get the second column by slicing. Do not use a list comprehension!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting started" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import the NumPy package" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create an array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.lookfor('create array')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(np.array)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The variable `matrix` contains a list of lists. Turn it into an `ndarray` and assign it to the variable `myarray`. Verify that its type is correct." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For practicing purposes, arrays can conveniently be created with the `arange` method." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "myarray1 = np.arange(6)\n", "myarray1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def array_attributes(a):\n", " for attr in ('ndim', 'size', 'itemsize', 'dtype', 'shape', 'strides'):\n", " print('{:8s}: {}'.format(attr, getattr(a, attr)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "array_attributes(myarray1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use `np.array()` to create arrays containing\n", " * floats\n", " * complex numbers\n", " * booleans\n", " * strings\n", " \n", "and check the `dtype` attribute." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Do you understand what is happening in the following statement?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.arange(1, 160, 10, dtype=np.int8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strides" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "myarray2 = myarray1.reshape(2, 3)\n", "myarray2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "array_attributes(myarray2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "myarray3 = myarray1.reshape(3, 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "array_attributes(myarray3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Views" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set the first entry of `myarray1` to a new value, e.g. 42." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What happened to `myarray2`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What happens when a matrix is transposed?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(9).reshape(3, 3)\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check the strides!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.strides" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.T.strides" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## View versus copy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "identical object" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(4)\n", "b = a\n", "id(a), id(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "view: a different object working on the same data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b = a[:]\n", "id(a), id(b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[0] = 42\n", "a, b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "an independent copy" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(4)\n", "b = np.copy(a)\n", "id(a), id(b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[0] = 42\n", "a, b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Some array creation routines" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### numerical ranges" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`arange(`*start*, *stop*, *step*`)`, *stop* is not included in the array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.arange(5, 30, 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`arange` resembles `range`, but also works for floats\n", "\n", "Create the array [1, 1.1, 1.2, 1.3, 1.4, 1.5]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`linspace(`*start*, *stop*, *num*`)` determines the step to produce *num* equally spaced values, *stop* is included by default\n", "\n", "Create the array [1., 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For equally spaced values on a logarithmic scale, use `logspace`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.logspace(-2, 2, 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.logspace(0, 4, 9, base=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Application" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = np.linspace(0, 10, 100)\n", "y = np.cos(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Homogeneous data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.zeros((4, 4))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a 4x4 array with integer zeros" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.ones((2, 3, 3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a 3x3 array filled with tens" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Diagonal elements" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.diag([1, 2, 3, 4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`diag` has an optional argument `k`. Try to find out what its effect is." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Replace the 1d array by a 2d array. What does `diag` do?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "np.info(np.eye)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the 3x3 array\n", "\n", "```[[2, 1, 0],\n", " [1, 2, 1],\n", " [0, 1, 2]]\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Random numbers" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.random.rand(5, 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.random.seed(1234)\n", "np.random.rand(5, 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = np.random.rand(20, 20)\n", "plt.imshow(data, cmap=plt.cm.hot, interpolation='none')\n", "plt.colorbar()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "casts = np.random.randint(1, 7, (100, 3))\n", "plt.hist(casts, np.linspace(0.5, 6.5, 7))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Indexing and slicing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1d arrays" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the array [7, 8, 9]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the array [2, 4, 6, 8]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the array [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Higher dimensions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fancy indexing ‒ Boolean mask" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(40).reshape(5, 8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a %3 == 0" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a[a %3 == 0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Application: sieve of Eratosthenes" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "nmax = 50\n", "integers = np.arange(nmax)\n", "is_prime = np.ones(nmax, dtype=bool)\n", "is_prime[:2] = False\n", "for j in range(2, int(np.sqrt(nmax))+1):\n", " if is_prime[j]:\n", " print(integers[is_prime])\n", " is_prime[j*j::j] = False\n", "print(integers[is_prime])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Axes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create an array and calculate the sum over all elements" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now calculate the sum along axis 0 ..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and now along axis 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Identify the axis in the following array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(24).reshape(2, 3, 4)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Axes in more than two dimensions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a three-dimensional array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Produce a two-dimensional array by cutting along axis 0 ..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and axis 1 ..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and axis 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What do you get by simply using the index `[0]`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What do you get by using `[..., 0]`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exploring numerical operations" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(4)\n", "b = np.arange(4, 8)\n", "a, b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a+b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a*b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Operations are elementwise. Check this by multiplying two 2d array..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... and now do a real matrix multiplication" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Application: Random walk" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "length_of_walk = 10000\n", "realizations = 5\n", "angles = 2*np.pi*np.random.rand(length_of_walk, realizations)\n", "x = np.cumsum(np.cos(angles), axis=0)\n", "y = np.cumsum(np.sin(angles), axis=0)\n", "plt.plot(x, y)\n", "plt.axis('scaled')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(np.hypot(x, y))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(np.mean(x**2+y**2, axis=1))\n", "plt.axis('scaled')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Let's check the speed" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%timeit a = np.arange(1000000)\n", "a**2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "%%timeit xvals = range(1000000)\n", "[xval**2 for xval in xvals]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%timeit a = np.arange(100000)\n", "np.sin(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import math" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%timeit xvals = range(100000)\n", "[math.sin(xval) for xval in xvals]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Broadcasting" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(12).reshape(3, 4)\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a+1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a+np.arange(4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a+np.arange(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.arange(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.arange(3).reshape(3, 1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a+np.arange(3).reshape(3, 1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%timeit a = np.arange(10000).reshape(100, 100); b = np.ones((100, 100))\n", "a+b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%timeit a = np.arange(10000).reshape(100, 100)\n", "a+1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a multiplication table for the numbers from 1 to 10 starting from two appropriately chosen 1d arrays." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As an alternative to `reshape` one can add additional axes with `newaxes`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(5)\n", "b = a[:, np.newaxis]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check the shapes." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions of two variables" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = np.linspace(-40, 40, 200)\n", "y = x[:, np.newaxis]\n", "z = np.sin(np.hypot(x-10, y))+np.sin(np.hypot(x+10, y))\n", "plt.imshow(z, cmap='viridis')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x, y = np.mgrid[-10:10:0.1, -10:10:0.1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.imshow(np.sin(x*y))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x, y = np.mgrid[-10:10:50j, -10:10:50j]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.imshow(np.arctan2(x, y))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is natural to use broadcasting. Check out what happens when you replace `mgrid` by `ogrid`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Application: Mandelbrot set" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#\n", "# put code here to create a Boolean array which contains True if a point\n", "# belongs to the Mandelbrot set\n", "#\n", "# reasonable values: 50 iterations, threshold at 100, and a 300x300 grid\n", "# but feel free to choose other values\n", "#\n", "plt.imshow(imdata, cmap='gray')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Application: π from random numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create an array of random numbers and determine the fraction of points with distance from the origin smaller than one. Determine an approximation for π." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Linear Algebra in NumPy" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy.linalg as LA" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.arange(4).reshape(2, 2)\n", "eigenvalues, eigenvectors = LA.eig(a)\n", "eigenvalues" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "eigenvectors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Explore whether the eigenvectors are the rows or the columns." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try out `eigvals` and other methods offered by `linalg` which your are interested in" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Determine the eigenvalue larger than one appearing in the Fibonacci problem. Verify the result by calculating the ratio of successive Fibonacci numbers. Do you recognize the result?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Application: Brownian motion" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Simulate several trajectories for a one-dimensional\n", "Brownian motion \n", "Hint: `np.random.choice`\n", "2. Plot the mean distance from the origin as a function\n", "of time\n", "3. Plot the variance of the trajectories as a function of\n", "time\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Application: identify entry closest to ½" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a 2d array containing random numbers and generate a vector containing for each row the entry closest to one-half." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Polynomials" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from numpy.polynomial import polynomial as P" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Powers increase from left to right (index corresponds to power)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "p1 = P.Polynomial([1, 2])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p1.degree()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p1.roots()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "p4 = P.Polynomial([24, -50, 35, -10, 1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p4.degree()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p4.roots()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p4.deriv()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p4.integ()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "P.polydiv(p4.coef, p1.coef)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Application: polynomial fit" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Application: image manipulation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from scipy import misc\n", "face = misc.face(gray=True)\n", "face" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.imshow(face, cmap=plt.cm.gray)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Modify this image, e.g. convert it to a black and white image, put a black frame, change the contrast, ..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }