{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Introduction to NumPy\n", "\n", "Tamás Gál (tamas.gal@fau.de)\n", "\n", "The latest version of this notebook is available at [https://github.com/escape2020/school2021](https://github.com/escape2020/school2021)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python version: 3.9.4 | packaged by conda-forge | (default, May 10 2021, 22:10:52) \n", "[Clang 11.1.0 ]\n", "NumPy version: 1.20.3\n" ] } ], "source": [ "import numpy as np\n", "import sys\n", "\n", "print(f\"Python version: {sys.version}\\n\"\n", " f\"NumPy version: {np.__version__}\")\n", "\n", "rng = np.random.default_rng(42) # initialise our random number generator" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "def describe(np_obj):\n", " \"\"\"Print some information about a NumPy object\"\"\"\n", " print(\"object type: {0}\\n\"\n", " \"size: {o.size}\\n\"\n", " \"ndim: {o.ndim}\\n\"\n", " \"shape: {o.shape}\\n\"\n", " \"dtype: {o.dtype}\"\n", " .format(type(np_obj), o=np_obj))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "from IPython.core.magic import register_line_magic\n", "\n", "@register_line_magic\n", "def shorterr(line):\n", " \"\"\"Show only the exception message if one is raised.\"\"\"\n", " try:\n", " output = eval(line)\n", " except Exception as e:\n", " print(\"\\x1b[31m\\x1b[1m{e.__class__.__name__}: {e}\\x1b[0m\".format(e=e))\n", " else:\n", " return output\n", " \n", "del shorterr" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## The basic datastructure in NumPy: `ndarray`" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([1, 2, 3, 4, 5, 6])\n", "a" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Array properties" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.size # number of elements" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.ndim" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(6,)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.shape" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "dtype('int64')" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.dtype" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Multi-Dimensional Arrays" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 4, 5],\n", " [ 6, 7, 8, 9, 10]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])\n", "b" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "object type: \n", "size: 10\n", "ndim: 2\n", "shape: (2, 5)\n", "dtype: int64\n" ] } ], "source": [ "describe(b)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Array Methods" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(1, 6, 3.5, 21)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.min(), a.max(), a.mean(), a.sum()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 4, 5],\n", " [ 6, 7, 8, 9, 10]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "55" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.sum()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 7, 9, 11, 13, 15])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.sum(axis=0)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([15, 40])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.sum(axis=1)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Operations with Arrays" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([-41, -40, -39, -38, -37, -36])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a - 42" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([13.36901522, 26.73803044, 40.10704566, 53.47606088, 66.8450761 ,\n", " 80.21409132])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a * 42 / np.pi" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(array([ 1. , 6.58088599, 19.81299075, 43.30806043,\n", " 79.43235917, 130.38703324]),\n", " array([ 2.71828183, 7.3890561 , 20.08553692, 54.59815003,\n", " 148.4131591 , 403.42879349]))" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a**np.e, np.e**a" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 1, 4, 9, 16, 25, 36])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a * a # element-wise" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "91" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a @ a # use np.dot(a, a) if you are using < Python 3.5" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ True, True, False, False, False, False])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a < 3" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([False, False, False, True, False, False])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a == 4" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([False, False, False, True, False, False])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(a > 3) & (a < 5) # bitwise AND" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ True, True, True, False, False, False])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a < np.array([2, 3, 5, 2, 1, 5])" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sum(a > 2)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Basic Indexing and Slicing" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0] # indexing starts at 0" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[-1] # -1 refers to the last element" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([3, 6])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[2:6:3] # just like in Python: [start:end:step]" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([6, 5, 4, 3, 2, 1])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[::-1] # reversing an array" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 6, 7, 8, 9, 10],\n", " [ 1, 2, 3, 4, 5]])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[::-1] # reverses axis 0" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Indixing and Slicing in Multiple Dimensions" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 4, 5],\n", " [ 6, 7, 8, 9, 10]])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[0, 2]" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([2, 3, 4])" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[0, 1:4]" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[2, 3, 4],\n", " [7, 8, 9]])" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[:, 1:4] # the `:` selects the whole axis" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 3, 5],\n", " [ 8, 10]])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[:, 2:5:2]" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[10, 9, 8, 7, 6],\n", " [ 5, 4, 3, 2, 1]])" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[::-1, ::-1] # reverses both axes" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Advanced Indexing" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "d = np.array([4, 3, 2, 5, 4, 5, 4, 4])" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ True, False, False, True, False, False, True, True])" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mask = np.array([True, False, False, True, False, False, True, True])\n", "mask" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([4, 5, 4, 4])" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[mask]" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([3, 5, 3, 4])" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[[1, 3, 1, 6]]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "#### Be careful with boolean indexing, the mask has to be a boolean array or a list of booleans." ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([4, 3, 2, 5, 4, 5, 4, 4])" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([3, 4, 4])" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[[False, True, False, False, True, False, False, True]]" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([4, 3, 4, 4, 3, 4, 4, 3])" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[[0, 1, 0, 0, 1, 0, 0, 1]] # although we know that True==1 and False==0" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([3, 4, 4])" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[np.array([0, 1, 0, 0, 1, 0, 0, 1], dtype=bool)] " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## The `dtype`" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "numpy.dtype" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dtype" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(array([1, 2, 3, 4, 5, 6]), dtype('int64'))" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, a.dtype" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(array([13.36901522, 26.73803044, 40.10704566, 53.47606088, 66.8450761 ,\n", " 80.21409132]),\n", " dtype('float64'))" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "e = a * 42 / np.pi # NumPy will choose the \"right\" `dtype` automatically\n", "e, e.dtype" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Some Basic `dtype`s" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "dtype('float32')" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dtype('f')" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "dtype('float64')" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dtype('f8')" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "dtype('int32')" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dtype('i')" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "dtype('int16')" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dtype('i2')" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "dtype('complex128')" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dtype('c16')" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "dtype('S8')" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dtype('S8') # String with a fixed length of 8" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Properties of `dtype`s" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "dt = np.dtype('>i4')" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'>'" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.byteorder # endinanness: " ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.itemsize" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'int32'" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.name" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Structured `dtypes`" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "dt = np.dtype([('x', 'f8'), ('y', 'f8'), ('E', 'i4')])" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.itemsize" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "dtype('float64')" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt['x']" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "dtype([('f0', '\n", "size: 20\n", "ndim: 2\n", "shape: (4, 5)\n", "dtype: int64\n" ] } ], "source": [ "describe(i)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 4, 12, 20],\n", " [ 0, 6, 14, 32, 45],\n", " [ 0, 11, 24, 52, 70],\n", " [ 0, 16, 34, 72, 95]])" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i * np.array([0, 1, 2, 4, 5])" ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "j = np.array([0, 10, 20, 30])\n", "k = np.array([7, 8, 9])" ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[31m\u001b[1mValueError: operands could not be broadcast together with shapes (4,) (3,) \u001b[0m\n" ] } ], "source": [ "%shorterr j+k" ] }, { "cell_type": "code", "execution_count": 92, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0],\n", " [10],\n", " [20],\n", " [30]])" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "j[:, np.newaxis] # inserts a new axis, making it two dimensional" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 7, 8, 9],\n", " [17, 18, 19],\n", " [27, 28, 29],\n", " [37, 38, 39]])" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "j[:, np.newaxis] + k" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Universal Functions (`ufunc`)\n", "\n", "#### A `ufunc` is a \"vectorized\" wrapper for a function that takes a fixed number of scalar inputs and produces a fixed number of scalar outputs.\n", "\n", "NumPy provides a bunch of `ufunc`s:\n", "- Math operations (`add()`, `subtract()`, `square()`, `log10()`, ...)\n", "- Trigonometric functions (`sin()`, `cos()`, `tan()`, `deg2rad()`, ...)\n", "- Bit-twiddling functions (`bitwise_and()`, `right_shift()`, ...)\n", "- Comparison functions (`greater()`, `less_equal()`, `fmax()`, ...)\n", "- Floating functions (`isnan()`, `isinf()`, `floor()`, ...)\n", " \n", "They all are subclasses of `np.ufunc`" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "numpy.ufunc" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(np.cos) # they all are subclasses of np.ufunc" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Create your own `ufunc` with `np.frompyfunc(func, nin, nout)`" ] }, { "cell_type": "code", "execution_count": 95, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([62, 70, 9, 31, 76, 83, 43, 80, 84, 38, 89, 28, 23, 68, 63, 13, 83])" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = rng.integers(0, 100, 17)\n", "m" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def step_23(x):\n", " return 1 if x > 23 else 0" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[31m\u001b[1mValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()\u001b[0m\n" ] } ], "source": [ "%shorterr step_23(m)" ] }, { "cell_type": "code", "execution_count": 98, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "ustep_23 = np.frompyfunc(step_23, 1, 1)" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ustep_23(42)" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ustep_23(5)" ] }, { "cell_type": "code", "execution_count": 101, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1], dtype=object)" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ustep_23(m)" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[0, 1, 0, 1],\n", " [1, 1, 1, 1],\n", " [1, 1, 1, 1]],\n", "\n", " [[1, 1, 1, 0],\n", " [0, 1, 0, 1],\n", " [1, 1, 1, 1]]], dtype=object)" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ustep_23(rng.integers(0, 100, (2, 3, 4)))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Views and Copies" ] }, { "cell_type": "code", "execution_count": 103, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "original = np.arange(10)\n", "original" ] }, { "cell_type": "code", "execution_count": 104, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 99, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ref_to_original = original # will point to `original`\n", "ref_to_original[2] = 99\n", "original # changing `ref_to_original` has changed `original`" ] }, { "cell_type": "code", "execution_count": 105, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "single_value = original[5] # single element access returns a copy\n", "single_value" ] }, { "cell_type": "code", "execution_count": 106, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 99, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "single_value = 9999\n", "original # not affected when `single_value` is changed" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Slices return (memory) views" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "original = np.arange(10)\n", "original" ] }, { "cell_type": "code", "execution_count": 108, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([2, 3])" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_slice = original[2:4] # slices return (memory) views\n", "a_slice" ] }, { "cell_type": "code", "execution_count": 109, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 2, 1000, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_slice[1] = 1000 # changing elements of `original` are actual changes to `a_slice`\n", "original" ] }, { "cell_type": "code", "execution_count": 110, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 2, 101, 102, 103, 6, 7, 8, 9])" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "original[3:6] = [101, 102, 103] # changing multiple elements at once\n", "original" ] } ], "metadata": { "@webio": { "lastCommId": "8dcdfb2dba1843138432b9a471109ba2", "lastKernelId": "40c3d3f4-4dc0-4ef0-9f84-ad757b8911a8" }, "celltoolbar": "Slideshow", "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.9.4" } }, "nbformat": 4, "nbformat_minor": 4 }