Corrected recipes and added notebook
+2411 −137 · 3 files changed
-
README.html+10 −25 -
README.ipynb+2233 −0 -
README.rst+168 −112
@@ -32,23 +32,8 @@ is:</p> <p class="last">The level names came from an old-game (Dungeon Master)</p> </div> <p>Repository is at: <a class="reference external" href="https://github.com/rougier/numpy-100">https://github.com/rougier/numpy-100</a></p> -<p><strong>Contents</strong></p> -<div class="contents local topic" id="contents"> -<ul class="simple"> -<li><a class="reference internal" href="#neophyte" id="id1">Neophyte</a></li> -<li><a class="reference internal" href="#novice" id="id2">Novice</a></li> -<li><a class="reference internal" href="#apprentice" id="id3">Apprentice</a></li> -<li><a class="reference internal" href="#journeyman" id="id4">Journeyman</a></li> -<li><a class="reference internal" href="#craftsman" id="id5">Craftsman</a></li> -<li><a class="reference internal" href="#artisan" id="id6">Artisan</a></li> -<li><a class="reference internal" href="#adept" id="id7">Adept</a></li> -<li><a class="reference internal" href="#expert" id="id8">Expert</a></li> -<li><a class="reference internal" href="#master" id="id9">Master</a></li> -<li><a class="reference internal" href="#archmaster" id="id10">Archmaster</a></li> -</ul> -</div> <div class="section" id="neophyte"> -<h1><a class="toc-backref" href="#id1">Neophyte</a></h1> +<h1>Neophyte</h1> <ol class="arabic"> <li><p class="first">Import the numpy package under the name <tt class="docutils literal">np</tt></p> <pre class="code python literal-block"> @@ -105,7 +90,7 @@ is:</p> </ol> </div> <div class="section" id="novice"> -<h1><a class="toc-backref" href="#id2">Novice</a></h1> +<h1>Novice</h1> <ol class="arabic"> <li><p class="first">Create a 8x8 matrix and fill it with a checkerboard pattern</p> <pre class="code python literal-block"> @@ -170,7 +155,7 @@ is:</p> </ol> </div> <div class="section" id="apprentice"> -<h1><a class="toc-backref" href="#id3">Apprentice</a></h1> +<h1>Apprentice</h1> <ol class="arabic"> <li><p class="first">Make an array immutable (read-only)</p> <pre class="code python literal-block"> @@ -260,7 +245,7 @@ consecutive zeros interleaved between each value ?</p> </ol> </div> <div class="section" id="journeyman"> -<h1><a class="toc-backref" href="#id4">Journeyman</a></h1> +<h1>Journeyman</h1> <ol class="arabic"> <li><p class="first">Consider the following file:</p> <pre class="literal-block"> @@ -336,7 +321,7 @@ using a vector S of same size describing subset indices ?</p> </ol> </div> <div class="section" id="craftsman"> -<h1><a class="toc-backref" href="#id5">Craftsman</a></h1> +<h1>Craftsman</h1> <ol class="arabic"> <li><p class="first">Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last @@ -378,7 +363,7 @@ np.bincount(A) == C ?</p> </ol> </div> <div class="section" id="artisan"> -<h1><a class="toc-backref" href="#id6">Artisan</a></h1> +<h1>Artisan</h1> <ol class="arabic"> <li><p class="first">Considering a 100x3 matrix, extract rows with unequal values (e.g. [2,2,3])</p> <pre class="code python literal-block"> @@ -406,7 +391,7 @@ np.bincount(A) == C ?</p> </ol> </div> <div class="section" id="adept"> -<h1><a class="toc-backref" href="#id7">Adept</a></h1> +<h1>Adept</h1> <ol class="arabic"> <li><p class="first">Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a <tt class="docutils literal">fill</tt> value when @@ -451,7 +436,7 @@ array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]] ?</p> </ol> </div> <div class="section" id="expert"> -<h1><a class="toc-backref" href="#id8">Expert</a></h1> +<h1>Expert</h1> <ol class="arabic"> <li><p class="first">Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A that contain elements of each row of B regardless of the order of the elements @@ -498,7 +483,7 @@ in B ?</p> </ol> </div> <div class="section" id="master"> -<h1><a class="toc-backref" href="#id9">Master</a></h1> +<h1>Master</h1> <ol class="arabic"> <li><p class="first">Given a two dimensional array, how to extract unique rows ?</p> <div class="note"> @@ -517,7 +502,7 @@ in B ?</p> </ol> </div> <div class="section" id="archmaster"> -<h1><a class="toc-backref" href="#id10">Archmaster</a></h1> +<h1>Archmaster</h1> </div> </div> </body> @@ -0,0 +1,2233 @@ +{ + "metadata": { + "signature": "sha256:c5ab63c7851c55fd5a676194657afffc9050560d49b7eea14782b955b80f4a54" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "100 numpy exercises" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The goal is both to offer a quick reference for new and old users and to\n", + "provide also a set of exercices for those who teach. If you remember\n", + "having asked or answered a (short) problem, you can send a pull request.\n", + "The format is:\n" + ] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "#. Find indices of non-zero elements from [1,2,0,0,4,0]\n", + "\n", + " .. code:: python\n", + "\n", + " # Author: Somebody\n", + "\n", + " print np.nonzero([1,2,0,0,4,0])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is what the page looks like so far:\n", + "<http://www.loria.fr/~rougier/teaching/numpy.100/index.html>\n" + ] + }, + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Note" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The level names came from an old-game (Dungeon Master)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Repository is at: <https://github.com/rougier/numpy-100>\n" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Neophyte" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Import the numpy package under the name `np`\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy as np" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Print the numpy version and the configuration.\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print np.__version__\n", + "np.__config__.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1.8.1\n", + "atlas_threads_info:\n", + " NOT AVAILABLE\n", + "blas_opt_info:\n", + " extra_link_args = ['-Wl,-framework', '-Wl,Accelerate']\n", + " extra_compile_args = ['-msse3', '-I/System/Library/Frameworks/vecLib.framework/Headers']\n", + " define_macros = [('NO_ATLAS_INFO', 3)]\n", + "atlas_blas_threads_info:\n", + " NOT AVAILABLE\n", + "openblas_info:\n", + " NOT AVAILABLE\n", + "lapack_opt_info:\n", + " extra_link_args = ['-Wl,-framework', '-Wl,Accelerate']\n", + " extra_compile_args = ['-msse3']\n", + " define_macros = [('NO_ATLAS_INFO', 3)]\n", + "atlas_info:\n", + " NOT AVAILABLE\n", + "lapack_mkl_info:\n", + " NOT AVAILABLE\n", + "blas_mkl_info:\n", + " NOT AVAILABLE\n", + "atlas_blas_info:\n", + " NOT AVAILABLE\n", + "mkl_info:\n", + " NOT AVAILABLE\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a null vector of size 10\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.zeros(10)\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a null vector of size 10 but the fifth value which is 1\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.zeros(10)\n", + "Z[4] = 1\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[ 0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a vector with values ranging from 10 to 49\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.arange(10,50)\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34\n", + " 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a 3x3 matrix with values ranging from 0 to 8\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.arange(9).reshape(3,3)\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[0 1 2]\n", + " [3 4 5]\n", + " [6 7 8]]\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Find indices of non-zero elements from [1,2,0,0,4,0]\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "nz = np.nonzero([1,2,0,0,4,0])\n", + "print nz" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(array([0, 1, 4]),)\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a 3x3 identity matrix\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.eye(3)\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[ 1. 0. 0.]\n", + " [ 0. 1. 0.]\n", + " [ 0. 0. 1.]]\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a 5x5 matrix with values 1,2,3,4 just below the diagonal\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.diag(1+np.arange(4),k=-1)\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[0 0 0 0 0]\n", + " [1 0 0 0 0]\n", + " [0 2 0 0 0]\n", + " [0 0 3 0 0]\n", + " [0 0 0 4 0]]\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a 3x3x3 array with random values\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.random.random((3,3,3))\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[[ 0.70021694 0.95083496 0.30437049]\n", + " [ 0.21431207 0.31105127 0.5068161 ]\n", + " [ 0.58181837 0.99693352 0.09652223]]\n", + "\n", + " [[ 0.29377124 0.79645988 0.90955378]\n", + " [ 0.92912313 0.80872229 0.79051782]\n", + " [ 0.62249093 0.83439699 0.95995313]]\n", + "\n", + " [[ 0.40897092 0.65822449 0.65887148]\n", + " [ 0.99839327 0.41591443 0.10637997]\n", + " [ 0.84581123 0.82366357 0.16914503]]]\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Novice" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a 8x8 matrix and fill it with a checkerboard pattern\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.zeros((8,8),dtype=int)\n", + "Z[1::2,::2] = 1\n", + "Z[::2,1::2] = 1\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]\n", + " [0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]\n", + " [0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]\n", + " [0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]]\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a 10x10 array with random values and find the minimum and maximum\n", + "values\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.random.random((10,10))\n", + "Zmin, Zmax = Z.min(), Z.max()\n", + "print Zmin, Zmax" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0.00129611420124 0.99762192206\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a checkerboard 8x8 matrix using the tile function\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.tile( np.array([[0,1],[1,0]]), (4,4))\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]\n", + " [0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]\n", + " [0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]\n", + " [0 1 0 1 0 1 0 1]\n", + " [1 0 1 0 1 0 1 0]]\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Normalize a 5x5 random matrix (between 0 and 1)\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.random.random((5,5))\n", + "Zmax,Zmin = Z.max(), Z.min()\n", + "Z = (Z - Zmin)/(Zmax - Zmin)\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[ 0.82414054 0.53347021 0.27229421 0.82030074 0.63632098]\n", + " [ 0.47403754 0.78566293 0. 0.07140557 0.59491348]\n", + " [ 0.61019479 0.65576641 0.27786659 0.68939527 0.16168705]\n", + " [ 0.43904126 0.90618438 0.06668649 0.14611578 1. ]\n", + " [ 0.49405041 0.9111222 0.73685086 0.17489323 0.95239388]]\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Multiply a 5x3 matrix by a 3x2 matrix (real matrix product)\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.dot(np.ones((5,3)), np.ones((3,2)))\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[ 3. 3.]\n", + " [ 3. 3.]\n", + " [ 3. 3.]\n", + " [ 3. 3.]\n", + " [ 3. 3.]]\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a 5x5 matrix with row values ranging from 0 to 4\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.zeros((5,5))\n", + "Z += np.arange(5)\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[ 0. 1. 2. 3. 4.]\n", + " [ 0. 1. 2. 3. 4.]\n", + " [ 0. 1. 2. 3. 4.]\n", + " [ 0. 1. 2. 3. 4.]\n", + " [ 0. 1. 2. 3. 4.]]\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a vector of size 10 with values ranging from 0 to 1, both\n", + "excluded\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.linspace(0,1,12,endpoint=True)[1:-1]\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[ 0.09090909 0.18181818 0.27272727 0.36363636 0.45454545 0.54545455\n", + " 0.63636364 0.72727273 0.81818182 0.90909091]\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a random vector of size 10 and sort it\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.random.random(10)\n", + "Z.sort()\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[ 0.06704956 0.08438841 0.08667572 0.30537151 0.35700968 0.66220797\n", + " 0.88027037 0.88374795 0.9779146 0.98195841]\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Consider two random array A anb B, check if they are equal.\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "A = np.random.randint(0,2,5)\n", + "B = np.random.randint(0,2,5)\n", + "equal = np.allclose(A,B)\n", + "print equal" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "False\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a random vector of size 30 and find the mean value\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.random.random(30)\n", + "m = Z.mean()\n", + "print m" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0.453360234095\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Apprentice" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Make an array immutable (read-only)\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.zeros(10)\n", + "Z.flags.writeable = False\n", + "Z[0] = 1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "assignment destination is read-only", + "output_type": "pyerr", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m<ipython-input-21-6fd4c6570dd1>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mZ\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mzeros\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mZ\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mflags\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwriteable\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mFalse\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mZ\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mValueError\u001b[0m: assignment destination is read-only" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Consider a random 10x2 matrix representing cartesian coordinates,\n", + "convert them to polar coordinates\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.random.random((10,2))\n", + "X,Y = Z[:,0], Z[:,1]\n", + "R = np.sqrt(X**2+Y**2)\n", + "T = np.arctan2(Y,X)\n", + "print R\n", + "print T" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[ 0.52257189 0.38213937 1.12648582 0.72972401 0.96450131 0.59947076\n", + " 1.20904375 0.63290855 0.88362902 0.77961181]\n", + "[ 0.62281132 0.6298261 0.61482359 0.18015421 0.25797157 1.50995739\n", + " 0.96329767 0.54114677 1.47538569 0.40910505]\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create random vector of size 10 and replace the maximum value by 0\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.random.random(10)\n", + "Z[Z.argmax()] = 0\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[ 0.15052552 0.69495523 0.94540327 0.69809245 0.33030241 0.\n", + " 0.53637513 0.14122452 0.00964067 0.77279768]\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a structured array with `x` and `y` coordinates covering the\n", + "[0,1]x[0,1] area.\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.zeros((10,10), [('x',float),('y',float)])\n", + "Z['x'], Z['y'] = np.meshgrid(np.linspace(0,1,10),\n", + " np.linspace(0,1,10))\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[(0.0, 0.0) (0.1111111111111111, 0.0) (0.2222222222222222, 0.0)\n", + " (0.3333333333333333, 0.0) (0.4444444444444444, 0.0)\n", + " (0.5555555555555556, 0.0) (0.6666666666666666, 0.0)\n", + " (0.7777777777777777, 0.0) (0.8888888888888888, 0.0) (1.0, 0.0)]\n", + " [(0.0, 0.1111111111111111) (0.1111111111111111, 0.1111111111111111)\n", + " (0.2222222222222222, 0.1111111111111111)\n", + " (0.3333333333333333, 0.1111111111111111)\n", + " (0.4444444444444444, 0.1111111111111111)\n", + " (0.5555555555555556, 0.1111111111111111)\n", + " (0.6666666666666666, 0.1111111111111111)\n", + " (0.7777777777777777, 0.1111111111111111)\n", + " (0.8888888888888888, 0.1111111111111111) (1.0, 0.1111111111111111)]\n", + " [(0.0, 0.2222222222222222) (0.1111111111111111, 0.2222222222222222)\n", + " (0.2222222222222222, 0.2222222222222222)\n", + " (0.3333333333333333, 0.2222222222222222)\n", + " (0.4444444444444444, 0.2222222222222222)\n", + " (0.5555555555555556, 0.2222222222222222)\n", + " (0.6666666666666666, 0.2222222222222222)\n", + " (0.7777777777777777, 0.2222222222222222)\n", + " (0.8888888888888888, 0.2222222222222222) (1.0, 0.2222222222222222)]\n", + " [(0.0, 0.3333333333333333) (0.1111111111111111, 0.3333333333333333)\n", + " (0.2222222222222222, 0.3333333333333333)\n", + " (0.3333333333333333, 0.3333333333333333)\n", + " (0.4444444444444444, 0.3333333333333333)\n", + " (0.5555555555555556, 0.3333333333333333)\n", + " (0.6666666666666666, 0.3333333333333333)\n", + " (0.7777777777777777, 0.3333333333333333)\n", + " (0.8888888888888888, 0.3333333333333333) (1.0, 0.3333333333333333)]\n", + " [(0.0, 0.4444444444444444) (0.1111111111111111, 0.4444444444444444)\n", + " (0.2222222222222222, 0.4444444444444444)\n", + " (0.3333333333333333, 0.4444444444444444)\n", + " (0.4444444444444444, 0.4444444444444444)\n", + " (0.5555555555555556, 0.4444444444444444)\n", + " (0.6666666666666666, 0.4444444444444444)\n", + " (0.7777777777777777, 0.4444444444444444)\n", + " (0.8888888888888888, 0.4444444444444444) (1.0, 0.4444444444444444)]\n", + " [(0.0, 0.5555555555555556) (0.1111111111111111, 0.5555555555555556)\n", + " (0.2222222222222222, 0.5555555555555556)\n", + " (0.3333333333333333, 0.5555555555555556)\n", + " (0.4444444444444444, 0.5555555555555556)\n", + " (0.5555555555555556, 0.5555555555555556)\n", + " (0.6666666666666666, 0.5555555555555556)\n", + " (0.7777777777777777, 0.5555555555555556)\n", + " (0.8888888888888888, 0.5555555555555556) (1.0, 0.5555555555555556)]\n", + " [(0.0, 0.6666666666666666) (0.1111111111111111, 0.6666666666666666)\n", + " (0.2222222222222222, 0.6666666666666666)\n", + " (0.3333333333333333, 0.6666666666666666)\n", + " (0.4444444444444444, 0.6666666666666666)\n", + " (0.5555555555555556, 0.6666666666666666)\n", + " (0.6666666666666666, 0.6666666666666666)\n", + " (0.7777777777777777, 0.6666666666666666)\n", + " (0.8888888888888888, 0.6666666666666666) (1.0, 0.6666666666666666)]\n", + " [(0.0, 0.7777777777777777) (0.1111111111111111, 0.7777777777777777)\n", + " (0.2222222222222222, 0.7777777777777777)\n", + " (0.3333333333333333, 0.7777777777777777)\n", + " (0.4444444444444444, 0.7777777777777777)\n", + " (0.5555555555555556, 0.7777777777777777)\n", + " (0.6666666666666666, 0.7777777777777777)\n", + " (0.7777777777777777, 0.7777777777777777)\n", + " (0.8888888888888888, 0.7777777777777777) (1.0, 0.7777777777777777)]\n", + " [(0.0, 0.8888888888888888) (0.1111111111111111, 0.8888888888888888)\n", + " (0.2222222222222222, 0.8888888888888888)\n", + " (0.3333333333333333, 0.8888888888888888)\n", + " (0.4444444444444444, 0.8888888888888888)\n", + " (0.5555555555555556, 0.8888888888888888)\n", + " (0.6666666666666666, 0.8888888888888888)\n", + " (0.7777777777777777, 0.8888888888888888)\n", + " (0.8888888888888888, 0.8888888888888888) (1.0, 0.8888888888888888)]\n", + " [(0.0, 1.0) (0.1111111111111111, 1.0) (0.2222222222222222, 1.0)\n", + " (0.3333333333333333, 1.0) (0.4444444444444444, 1.0)\n", + " (0.5555555555555556, 1.0) (0.6666666666666666, 1.0)\n", + " (0.7777777777777777, 1.0) (0.8888888888888888, 1.0) (1.0, 1.0)]]\n" + ] + } + ], + "prompt_number": 24 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Print the minimum and maximum representable value for each numpy scalar\n", + "type\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "for dtype in [np.int8, np.int32, np.int64]:\n", + " print np.iinfo(dtype).min\n", + " print np.iinfo(dtype).max\n", + "for dtype in [np.float32, np.float64]:\n", + " print np.finfo(dtype).min\n", + " print np.finfo(dtype).max\n", + " print np.finfo(dtype).eps" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "-128\n", + "127\n", + "-2147483648\n", + "2147483647\n", + "-9223372036854775808\n", + "9223372036854775807\n", + "-3.40282e+38\n", + "3.40282e+38\n", + "1.19209e-07\n", + "-1.79769313486e+308\n", + "1.79769313486e+308\n", + "2.22044604925e-16\n" + ] + } + ], + "prompt_number": 25 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a structured array representing a position (x,y) and a color\n", + "(r,g,b)\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " Z = np.zeros(10, [ ('position', [ ('x', float, 1),\n", + " ('y', float, 1)]),\n", + " ('color', [ ('r', float, 1),\n", + " ('g', float, 1),\n", + " ('b', float, 1)])])\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0))\n", + " ((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0))\n", + " ((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0))\n", + " ((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0))\n", + " ((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0))]\n" + ] + } + ], + "prompt_number": 26 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Consider a random vector with shape (100,2) representing coordinates,\n", + "find point by point distances\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.random.random((10,2))\n", + "X,Y = np.atleast_2d(Z[:,0]), np.atleast_2d(Z[:,1])\n", + "D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2)\n", + "print D\n", + "\n", + "# Much faster with scipy\n", + "import scipy\n", + "Z = np.random.random((10,2))\n", + "D = scipy.spatial.distance.cdist(Z,Z)\n", + "print D" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[ 0. 0.16987428 0.31102149 0.26583413 0.12161091 0.37702606\n", + " 0.60918712 0.22994418 0.73836939 0.75277804]\n", + " [ 0.16987428 0. 0.45238913 0.24333364 0.23656138 0.22419316\n", + " 0.44020383 0.18661258 0.58258746 0.6961923 ]\n", + " [ 0.31102149 0.45238913 0. 0.35278432 0.21620973 0.59358334\n", + " 0.87872597 0.37145888 0.92496631 0.73163727]\n", + " [ 0.26583413 0.24333364 0.35278432 0. 0.19705441 0.26188998\n", + " 0.5682884 0.0573825 0.57244145 0.48698659]\n", + " [ 0.12161091 0.23656138 0.21620973 0.19705441 0. 0.39175971\n", + " 0.66497293 0.18703245 0.74121606 0.66903981]\n", + " [ 0.37702606 0.22419316 0.59358334 0.26188998 0.39175971 0.\n", + " 0.30859748 0.22493167 0.36151588 0.55328388]\n", + " [ 0.60918712 0.44020383 0.87872597 0.5682884 0.66497293 0.30859748\n", + " 0. 0.52524536 0.32513581 0.78334717]\n", + " [ 0.22994418 0.18661258 0.37145888 0.0573825 0.18703245 0.22493167\n", + " 0.52524536 0. 0.55794299 0.52911933]\n", + " [ 0.73836939 0.58258746 0.92496631 0.57244145 0.74121606 0.36151588\n", + " 0.32513581 0.55794299 0. 0.55390248]\n", + " [ 0.75277804 0.6961923 0.73163727 0.48698659 0.66903981 0.55328388\n", + " 0.78334717 0.52911933 0.55390248 0. ]]\n" + ] + }, + { + "ename": "AttributeError", + "evalue": "'module' object has no attribute 'spatial'", + "output_type": "pyerr", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m<ipython-input-27-0c2de30fb39a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mscipy\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0mZ\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mD\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mscipy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mspatial\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdistance\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcdist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mZ\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mZ\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0mD\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mAttributeError\u001b[0m: 'module' object has no attribute 'spatial'" + ] + } + ], + "prompt_number": 27 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Generate a generic 2D Gaussian-like array\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10))\n", + "D = np.sqrt(X*X+Y*Y)\n", + "sigma, mu = 1.0, 0.0\n", + "G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) )\n", + "print G" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[ 0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.60279818\n", + " 0.57375342 0.51979489 0.44822088 0.36787944]\n", + " [ 0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.73444367\n", + " 0.69905581 0.63331324 0.54610814 0.44822088]\n", + " [ 0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.85172308\n", + " 0.81068432 0.73444367 0.63331324 0.51979489]\n", + " [ 0.57375342 0.69905581 0.81068432 0.89483932 0.9401382 0.9401382\n", + " 0.89483932 0.81068432 0.69905581 0.57375342]\n", + " [ 0.60279818 0.73444367 0.85172308 0.9401382 0.98773022 0.98773022\n", + " 0.9401382 0.85172308 0.73444367 0.60279818]\n", + " [ 0.60279818 0.73444367 0.85172308 0.9401382 0.98773022 0.98773022\n", + " 0.9401382 0.85172308 0.73444367 0.60279818]\n", + " [ 0.57375342 0.69905581 0.81068432 0.89483932 0.9401382 0.9401382\n", + " 0.89483932 0.81068432 0.69905581 0.57375342]\n", + " [ 0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.85172308\n", + " 0.81068432 0.73444367 0.63331324 0.51979489]\n", + " [ 0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.73444367\n", + " 0.69905581 0.63331324 0.54610814 0.44822088]\n", + " [ 0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.60279818\n", + " 0.57375342 0.51979489 0.44822088 0.36787944]]\n" + ] + } + ], + "prompt_number": 28 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3\n", + "consecutive zeros interleaved between each value ?\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Author: Warren Weckesser\n", + "\n", + "Z = np.array([1,2,3,4,5])\n", + "nz = 3\n", + "Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz))\n", + "Z0[::nz+1] = Z\n", + "print Z0" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[ 1. 0. 0. 0. 2. 0. 0. 0. 3. 0. 0. 0. 4. 0. 0. 0. 5.]\n" + ] + } + ], + "prompt_number": 29 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Find the nearest value from a given value in an array\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.random.uniform(0,1,10)\n", + "z = 0.5\n", + "m = Z.flat[np.abs(Z - z).argmin()]\n", + "print m" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0.51525684594\n" + ] + } + ], + "prompt_number": 30 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Journeyman" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Consider the following file:\n" + ] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "1,2,3,4,5\n", + "6,,,7,8\n", + ",,9,10,11" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How to read it ?\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Z = np.genfromtxt(\"missing.dat\", delimiter=\",\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "IOError", + "evalue": "missing.dat not found.", + "output_type": "pyerr", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mIOError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m<ipython-input-31-a57209acdc11>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mZ\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgenfromtxt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"missing.dat\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdelimiter\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\",\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/usr/local/lib/python2.7/site-packages/numpy/lib/npyio.pyc\u001b[0m in \u001b[0;36mgenfromtxt\u001b[0;34m(fname, dtype, comments, delimiter, skiprows, skip_header, skip_footer, converters, missing, missing_values, filling_values, usecols, names, excludelist, deletechars, replace_space, autostrip, case_sensitive, defaultfmt, unpack, usemask, loose, invalid_raise)\u001b[0m\n\u001b[1;32m 1342\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbasestring\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1343\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mversion_info\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1344\u001b[0;31m \u001b[0mfhd\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0miter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlib\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_datasource\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'rbU'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1345\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1346\u001b[0m \u001b[0mfhd\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0miter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlib\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_datasource\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'rb'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python2.7/site-packages/numpy/lib/_datasource.pyc\u001b[0m in \u001b[0;36mopen\u001b[0;34m(path, mode, destpath)\u001b[0m\n\u001b[1;32m 145\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 146\u001b[0m \u001b[0mds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mDataSource\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdestpath\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 147\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mds\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmode\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 148\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 149\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python2.7/site-packages/numpy/lib/_datasource.pyc\u001b[0m in \u001b[0;36mopen\u001b[0;34m(self, path, mode)\u001b[0m\n\u001b[1;32m 494\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0m_file_openers\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mext\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfound\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmode\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 495\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 496\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mIOError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"%s not found.\"\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mpath\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 497\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 498\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mIOError\u001b[0m: missing.dat not found." + ] + } + ], + "prompt_number": 31 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Consider a generator function that generates 10 integers and use it to\n", + "build an array\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def generate():\n", + " for x in xrange(10):\n", + " yield x\n", + "Z = np.fromiter(generate(),dtype=float,count=-1)\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]\n" + ] + } + ], + "prompt_number": 32 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Consider a given vector, how to add 1 to each element indexed by a\n", + "second vector (be careful with repeated indices) ?\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Author: Brett Olsen\n", + "\n", + "Z = np.ones(10)\n", + "I = np.random.randint(0,len(Z),20)\n", + "Z += np.bincount(I, minlength=len(Z))\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[ 3. 3. 4. 3. 3. 2. 5. 1. 2. 4.]\n" + ] + } + ], + "prompt_number": 33 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "How to accumulate elements of a vector (X) to an array (F) based on an\n", + "index list (I) ?\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Author: Alan G Isaac\n", + "\n", + "X = [1,2,3,4,5,6]\n", + "I = [1,3,9,3,4,1]\n", + "F = np.bincount(I,X)\n", + "print F" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[ 0. 7. 0. 6. 5. 0. 0. 0. 0. 3.]\n" + ] + } + ], + "prompt_number": 34 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Considering a (w,h,3) image of (dtype=ubyte), compute the number of\n", + "unique colors\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Author: Nadav Horesh\n", + "\n", + "w,h = 16,16\n", + "I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte)\n", + "F = I[...,0]*256*256 + I[...,1]*256 +I[...,2]\n", + "n = len(np.unique(F))\n", + "print np.unique(I)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[0 1]\n" + ] + } + ], + "prompt_number": 35 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Considering a four dimensions array, how to get sum over the last two\n", + "axis at once ?\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "A = np.random.randint(0,10,(3,4,3,4))\n", + "sum = A.reshape(A.shape[:-2] + (-1,)).sum(axis=-1)\n", + "print" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 36 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Considering a one-dimensional vector D, how to compute means of subsets\n", + "of D using a vector S of same size describing subset indices ?\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Jaime Fern\u00e1ndez del R\u00edo\n", + "\n", + "D = np.random.uniform(0,1,100)\n", + "S = np.random.randint(0,10,100)\n", + "D_sums = np.bincount(S, weights=D)\n", + "D_counts = np.bincount(S)\n", + "D_means = D_sums / D_counts\n", + "print D_means" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[ 0.47169995 0.41982199 0.4737244 0.53018004 0.3115647 0.62880299\n", + " 0.54180417 0.38041533 0.40633414 0.34868915]\n" + ] + } + ], + "prompt_number": 37 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Craftsman" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Consider a one-dimensional array Z, build a two-dimensional array whose\n", + "first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1\n", + "(last row should be (Z[-3],Z[-2],Z[-1])\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Author: Joe Kington / Erik Rigtorp\n", + "from numpy.lib import stride_tricks\n", + "\n", + "def rolling(a, window):\n", + " shape = (a.size - window + 1, window)\n", + " strides = (a.itemsize, a.itemsize)\n", + " return stride_tricks.as_strided(a, shape=shape, strides=strides)\n", + "Z = rolling(np.arange(10), 3)\n", + "print Z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[0 1 2]\n", + " [1 2 3]\n", + " [2 3 4]\n", + " [3 4 5]\n", + " [4 5 6]\n", + " [5 6 7]\n", + " [6 7 8]\n", + " [7 8 9]]\n" + ] + } + ], + "prompt_number": 38 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Consider a set of 10 triplets describing 10 triangles (with shared\n", + "vertices), find the set of unique line segments composing all the\n", + "triangles.\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Author: Nicolas Rougier\n", + "\n", + "faces = np.random.randint(0,100,(10,3))\n", + "F = np.roll(faces.repeat(2,axis=1),-1,axis=1)\n", + "F = F.reshape(len(F)*3,2)\n", + "F = np.sort(F,axis=1)\n", + "G = F.view( dtype=[('p0',F.dtype),('p1',F.dtype)] )\n", + "G = np.unique(G)\n", + "print G" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[(4, 23) (4, 24) (4, 37) (4, 45) (6, 50) (6, 87) (8, 32) (8, 83) (12, 17)\n", + " (12, 88) (16, 21) (16, 46) (16, 61) (16, 67) (17, 88) (21, 67) (23, 45)\n", + " (24, 37) (32, 83) (36, 38) (36, 63) (38, 63) (39, 43) (39, 70) (43, 43)\n", + " (43, 44) (43, 70) (46, 61) (50, 87)]\n" + ] + } + ], + "prompt_number": 39 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Given an array C that is a bincount, how to produce an array A such that\n", + "np.bincount(A) == C ?\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Jaime Fern\u00e1ndez del R\u00edo\n", + "\n", + "C = np.bincount([1,1,2,3,4,4,6])\n", + "A = np.repeat(np.arange(len(C)), C)\n", + "print A" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[1 1 2 3 4 4 6]\n" + ] + } + ], + "prompt_number": 40 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Artisan" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Considering a 10x3 matrix, extract rows with unequal values (e.g.\n", + "[2,2,3])\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Author: Robert Kern\n", + "\n", + "Z = np.random.randint(0,5,(10,3))\n", + "E = np.logical_and.reduce(Z[:,1:] == Z[:,:-1], axis=1)\n", + "U = Z[~E]\n", + "print Z\n", + "print U" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[2 1 1]\n", + " [3 0 1]\n", + " [3 0 2]\n", + " [4 1 2]\n", + " [1 2 2]\n", + " [0 2 2]\n", + " [2 1 0]\n", + " [3 1 0]\n", + " [2 3 3]\n", + " [1 4 3]]\n", + "[[2 1 1]\n", + " [3 0 1]\n", + " [3 0 2]\n", + " [4 1 2]\n", + " [1 2 2]\n", + " [0 2 2]\n", + " [2 1 0]\n", + " [3 1 0]\n", + " [2 3 3]\n", + " [1 4 3]]\n" + ] + } + ], + "prompt_number": 41 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Convert a vector of ints into a matrix binary representation.\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Author: Warren Weckesser\n", + "\n", + "I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128])\n", + "B = ((I.reshape(-1,1) & (2**np.arange(8))) != 0).astype(int)\n", + "print B[:,::-1]\n", + "\n", + "# Author: Daniel T. McDonald\n", + "\n", + "I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128], dtype=np.uint8)\n", + "print np.unpackbits(I[:, np.newaxis], axis=1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[0 0 0 0 0 0 0 0]\n", + " [0 0 0 0 0 0 0 1]\n", + " [0 0 0 0 0 0 1 0]\n", + " [0 0 0 0 0 0 1 1]\n", + " [0 0 0 0 1 1 1 1]\n", + " [0 0 0 1 0 0 0 0]\n", + " [0 0 1 0 0 0 0 0]\n", + " [0 1 0 0 0 0 0 0]\n", + " [1 0 0 0 0 0 0 0]]\n", + "[[0 0 0 0 0 0 0 0]\n", + " [0 0 0 0 0 0 0 1]\n", + " [0 0 0 0 0 0 1 0]\n", + " [0 0 0 0 0 0 1 1]\n", + " [0 0 0 0 1 1 1 1]\n", + " [0 0 0 1 0 0 0 0]\n", + " [0 0 1 0 0 0 0 0]\n", + " [0 1 0 0 0 0 0 0]\n", + " [1 0 0 0 0 0 0 0]]\n" + ] + } + ], + "prompt_number": 42 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Adept" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Consider an arbitrary array, write a function that extract a subpart\n", + "with a fixed shape and centered on a given element (pad with a `fill`\n", + "value when necessary)\n" + ] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "# Author: Nicolas Rougier\n", + "\n", + "Z = np.random.randint(0,10,(10,10))\n", + "shape = (5,5)\n", + "fill = 0\n", + "position = (1,1)\n", + "\n", + "R = np.ones(shape, dtype=Z.dtype)*fill\n", + "P = np.array(list(position)).astype(int)\n", + "Rs = np.array(list(R.shape)).astype(int)\n", + "Zs = np.array(list(Z.shape)).astype(int)\n", + "\n", + "R_start = np.zeros((len(shape),)).astype(int)\n", + "R_stop = np.array(list(shape)).astype(int)\n", + "Z_start = (P-Rs//2)\n", + "Z_stop = (P+Rs//2)+Rs%2\n", + "\n", + "R_start = (R_start - np.minimum(Z_start,0)).tolist()\n", + "Z_start = (np.maximum(Z_start,0)).tolist()\n", + "R_stop = np.maximum(R_start, (R_stop - np.maximum(Z_stop-Zs,0))).tolist()\n", + "Z_stop = (np.minimum(Z_stop,Zs)).tolist()\n", + "\n", + "r = [slice(start,stop) for start,stop in zip(R_start,R_stop)]\n", + "z = [slice(start,stop) for start,stop in zip(Z_start,Z_stop)]\n", + "R[r] = Z[z]\n", + "print Z\n", + "print R" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to\n", + "generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ...,\n", + "[11,12,13,14]] ?\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# St\u00e9fan van der Walt\n", + "\n", + "Z = np.arange(1,15,dtype=uint32)\n", + "R = stride_tricks.as_strided(Z,(11,4),(4,4))\n", + "print R" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'uint32' is not defined", + "output_type": "pyerr", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m<ipython-input-43-ba0b6870ce88>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# St\u00e9fan van der Walt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mZ\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m15\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0muint32\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mR\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstride_tricks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mas_strided\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mZ\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m11\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0mR\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'uint32' is not defined" + ] + } + ], + "prompt_number": 43 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Expert" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Consider two arrays A and B of shape (8,3) and (2,2). How to find rows\n", + "of A that contain elements of each row of B regardless of the order of\n", + "the elements in B ?\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Author: Gabe Schwartz\n", + "\n", + "A = np.random.randint(0,5,(8,3))\n", + "B = np.random.randint(0,5,(2,2))\n", + "\n", + "C = (A[..., np.newaxis, np.newaxis] == B)\n", + "rows = (C.sum(axis=(1,2,3)) >= B.shape[1]).nonzero()[0]\n", + "print rows" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[0 1 3 5 6 7]\n" + ] + } + ], + "prompt_number": 44 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Extract all the contiguous 3x3 blocks from a random 10x10 matrix.\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Chris Barker\n", + "\n", + "Z = np.random.randint(0,5,(10,10))\n", + "n = 3\n", + "i = 1 + (Z.shape[0]-3)\n", + "j = 1 + (Z.shape[1]-3)\n", + "C = stride_tricks.as_strided(Z, shape=(i, j, n, n), strides=Z.strides + Z.strides)\n", + "print C" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[[[0 4 2]\n", + " [1 0 2]\n", + " [4 4 4]]\n", + "\n", + " [[4 2 1]\n", + " [0 2 0]\n", + " [4 4 2]]\n", + "\n", + " [[2 1 1]\n", + " [2 0 4]\n", + " [4 2 1]]\n", + "\n", + " [[1 1 4]\n", + " [0 4 0]\n", + " [2 1 3]]\n", + "\n", + " [[1 4 4]\n", + " [4 0 1]\n", + " [1 3 2]]\n", + "\n", + " [[4 4 3]\n", + " [0 1 2]\n", + " [3 2 1]]\n", + "\n", + " [[4 3 3]\n", + " [1 2 2]\n", + " [2 1 4]]\n", + "\n", + " [[3 3 2]\n", + " [2 2 3]\n", + " [1 4 1]]]\n", + "\n", + "\n", + " [[[1 0 2]\n", + " [4 4 4]\n", + " [3 0 2]]\n", + "\n", + " [[0 2 0]\n", + " [4 4 2]\n", + " [0 2 2]]\n", + "\n", + " [[2 0 4]\n", + " [4 2 1]\n", + " [2 2 3]]\n", + "\n", + " [[0 4 0]\n", + " [2 1 3]\n", + " [2 3 3]]\n", + "\n", + " [[4 0 1]\n", + " [1 3 2]\n", + " [3 3 4]]\n", + "\n", + " [[0 1 2]\n", + " [3 2 1]\n", + " [3 4 3]]\n", + "\n", + " [[1 2 2]\n", + " [2 1 4]\n", + " [4 3 0]]\n", + "\n", + " [[2 2 3]\n", + " [1 4 1]\n", + " [3 0 2]]]\n", + "\n", + "\n", + " [[[4 4 4]\n", + " [3 0 2]\n", + " [0 4 3]]\n", + "\n", + " [[4 4 2]\n", + " [0 2 2]\n", + " [4 3 3]]\n", + "\n", + " [[4 2 1]\n", + " [2 2 3]\n", + " [3 3 4]]\n", + "\n", + " [[2 1 3]\n", + " [2 3 3]\n", + " [3 4 2]]\n", + "\n", + " [[1 3 2]\n", + " [3 3 4]\n", + " [4 2 1]]\n", + "\n", + " [[3 2 1]\n", + " [3 4 3]\n", + " [2 1 0]]\n", + "\n", + " [[2 1 4]\n", + " [4 3 0]\n", + " [1 0 3]]\n", + "\n", + " [[1 4 1]\n", + " [3 0 2]\n", + " [0 3 0]]]\n", + "\n", + "\n", + " [[[3 0 2]\n", + " [0 4 3]\n", + " [3 3 4]]\n", + "\n", + " [[0 2 2]\n", + " [4 3 3]\n", + " [3 4 3]]\n", + "\n", + " [[2 2 3]\n", + " [3 3 4]\n", + " [4 3 2]]\n", + "\n", + " [[2 3 3]\n", + " [3 4 2]\n", + " [3 2 4]]\n", + "\n", + " [[3 3 4]\n", + " [4 2 1]\n", + " [2 4 0]]\n", + "\n", + " [[3 4 3]\n", + " [2 1 0]\n", + " [4 0 3]]\n", + "\n", + " [[4 3 0]\n", + " [1 0 3]\n", + " [0 3 0]]\n", + "\n", + " [[3 0 2]\n", + " [0 3 0]\n", + " [3 0 4]]]\n", + "\n", + "\n", + " [[[0 4 3]\n", + " [3 3 4]\n", + " [2 1 4]]\n", + "\n", + " [[4 3 3]\n", + " [3 4 3]\n", + " [1 4 0]]\n", + "\n", + " [[3 3 4]\n", + " [4 3 2]\n", + " [4 0 1]]\n", + "\n", + " [[3 4 2]\n", + " [3 2 4]\n", + " [0 1 0]]\n", + "\n", + " [[4 2 1]\n", + " [2 4 0]\n", + " [1 0 4]]\n", + "\n", + " [[2 1 0]\n", + " [4 0 3]\n", + " [0 4 2]]\n", + "\n", + " [[1 0 3]\n", + " [0 3 0]\n", + " [4 2 4]]\n", + "\n", + " [[0 3 0]\n", + " [3 0 4]\n", + " [2 4 0]]]\n", + "\n", + "\n", + " [[[3 3 4]\n", + " [2 1 4]\n", + " [3 2 4]]\n", + "\n", + " [[3 4 3]\n", + " [1 4 0]\n", + " [2 4 4]]\n", + "\n", + " [[4 3 2]\n", + " [4 0 1]\n", + " [4 4 4]]\n", + "\n", + " [[3 2 4]\n", + " [0 1 0]\n", + " [4 4 3]]\n", + "\n", + " [[2 4 0]\n", + " [1 0 4]\n", + " [4 3 3]]\n", + "\n", + " [[4 0 3]\n", + " [0 4 2]\n", + " [3 3 1]]\n", + "\n", + " [[0 3 0]\n", + " [4 2 4]\n", + " [3 1 0]]\n", + "\n", + " [[3 0 4]\n", + " [2 4 0]\n", + " [1 0 2]]]\n", + "\n", + "\n", + " [[[2 1 4]\n", + " [3 2 4]\n", + " [0 0 4]]\n", + "\n", + " [[1 4 0]\n", + " [2 4 4]\n", + " [0 4 1]]\n", + "\n", + " [[4 0 1]\n", + " [4 4 4]\n", + " [4 1 3]]\n", + "\n", + " [[0 1 0]\n", + " [4 4 3]\n", + " [1 3 4]]\n", + "\n", + " [[1 0 4]\n", + " [4 3 3]\n", + " [3 4 1]]\n", + "\n", + " [[0 4 2]\n", + " [3 3 1]\n", + " [4 1 4]]\n", + "\n", + " [[4 2 4]\n", + " [3 1 0]\n", + " [1 4 0]]\n", + "\n", + " [[2 4 0]\n", + " [1 0 2]\n", + " [4 0 3]]]\n", + "\n", + "\n", + " [[[3 2 4]\n", + " [0 0 4]\n", + " [4 1 2]]\n", + "\n", + " [[2 4 4]\n", + " [0 4 1]\n", + " [1 2 3]]\n", + "\n", + " [[4 4 4]\n", + " [4 1 3]\n", + " [2 3 4]]\n", + "\n", + " [[4 4 3]\n", + " [1 3 4]\n", + " [3 4 1]]\n", + "\n", + " [[4 3 3]\n", + " [3 4 1]\n", + " [4 1 3]]\n", + "\n", + " [[3 3 1]\n", + " [4 1 4]\n", + " [1 3 0]]\n", + "\n", + " [[3 1 0]\n", + " [1 4 0]\n", + " [3 0 0]]\n", + "\n", + " [[1 0 2]\n", + " [4 0 3]\n", + " [0 0 3]]]]\n" + ] + } + ], + "prompt_number": 45 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a 2D array subclass such that Z[i,j] == Z[j,i]\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Eric O. Lebigot\n", + "# Note: only works for 2d array and value setting using indices\n", + "\n", + "class Symetric(np.ndarray):\n", + " def __setitem__(self, (i,j), value):\n", + " super(Symetric, self).__setitem__((i,j), value)\n", + " super(Symetric, self).__setitem__((j,i), value)\n", + "\n", + "def symetric(Z):\n", + " return np.asarray(Z + Z.T - np.diag(Z.diagonal())).view(Symetric)\n", + "\n", + "S = symetric(np.random.randint(0,10,(5,5)))\n", + "S[2,3] = 42\n", + "print S" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[ 2 14 13 9 8]\n", + " [14 3 6 0 12]\n", + " [13 6 0 42 6]\n", + " [ 9 0 42 1 12]\n", + " [ 8 12 6 12 3]]\n" + ] + } + ], + "prompt_number": 46 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Master" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Given a two dimensional array, how to extract unique rows ?\n" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Note" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "See\n", + "[stackoverflow](http://stackoverflow.com/questions/16970982/find-unique-rows-in-numpy-array/)\n", + "for explanations.\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Jaime Fern\u00e1ndez del R\u00edo\n", + "\n", + "Z = np.random.randint(0,2,(6,3))\n", + "T = np.ascontiguousarray(Z).view(np.dtype((np.void, Z.dtype.itemsize * Z.shape[1])))\n", + "_, idx = np.unique(T, return_index=True)\n", + "uZ = Z[idx]\n", + "print uZ" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[[0 0 1]\n", + " [0 1 1]\n", + " [1 0 1]\n", + " [1 1 0]]\n" + ] + } + ], + "prompt_number": 48 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Archmaster" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 47 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 47 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 47 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 47 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 47 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file @@ -8,32 +8,33 @@ A joint effort of the numpy community The goal is both to offer a quick reference for new and old users and to provide also a set of exercices for those who teach. If you remember having asked or answered a (short) problem, you can send a pull request. The format -is:: +is: - #. Find indices of non-zero elements from [1,2,0,0,4,0] +:: - .. code:: python + #. Find indices of non-zero elements from [1,2,0,0,4,0] - # Author: Somebody + .. code:: python - print np.nonzero([1,2,0,0,4,0]) + # Author: Somebody + + print np.nonzero([1,2,0,0,4,0]) Here is what the page looks like so far: http://www.loria.fr/~rougier/teaching/numpy.100/index.html - .. Note:: The level names came from an old-game (Dungeon Master) +Repository is at: https://github.com/rougier/numpy-100 The IPython notebook is +available from the github repo, thanks to the `rst2ipynb +<https://github.com/esc/rst2ipynb>`_ conversion tool by `Valentin Haenel +<http://haenel.co>`_ -Repository is at: https://github.com/rougier/numpy-100 - - -**Contents** - -.. contents:: - :local: - :depth: 1 +.. **Contents** +.. .. contents:: +.. :local: +.. :depth: 1 Neophyte @@ -41,14 +42,14 @@ Neophyte 1. Import the numpy package under the name ``np`` - .. code:: python + .. code-block:: python import numpy as np 2. Print the numpy version and the configuration. - .. code:: python + .. code-block:: python print np.__version__ np.__config__.show() @@ -56,129 +57,160 @@ Neophyte 3. Create a null vector of size 10 - .. code:: python + .. code-block:: python Z = np.zeros(10) + print Z + 4. Create a null vector of size 10 but the fifth value which is 1 - .. code:: python + .. code-block:: python Z = np.zeros(10) Z[4] = 1 + print Z -5. Create a vector with values ranging from 10 to 99 - .. code:: python +5. Create a vector with values ranging from 10 to 49 + + .. code-block:: python + + Z = np.arange(10,50) + print Z - Z = np.arange(10,100) 6. Create a 3x3 matrix with values ranging from 0 to 8 - .. code:: python + .. code-block:: python Z = np.arange(9).reshape(3,3) + print Z + 7. Find indices of non-zero elements from [1,2,0,0,4,0] - .. code:: python + .. code-block:: python nz = np.nonzero([1,2,0,0,4,0]) + print nz 8. Create a 3x3 identity matrix - .. code:: python + .. code-block:: python Z = np.eye(3) + print Z + 9. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal - .. code:: python + .. code-block:: python Z = np.diag(1+np.arange(4),k=-1) + print Z + +10. Create a 3x3x3 array with random values -10. Create a 10x10x10 array with random values + .. code-block:: python - .. code:: python + Z = np.random.random((3,3,3)) + print Z - Z = np.random.random((10,10,10)) Novice ====== 1. Create a 8x8 matrix and fill it with a checkerboard pattern - .. code:: python + .. code-block:: python - Z = np.zeros((8,8)) + Z = np.zeros((8,8),dtype=int) Z[1::2,::2] = 1 Z[::2,1::2] = 1 + print Z + 2. Create a 10x10 array with random values and find the minimum and maximum values - .. code:: python + .. code-block:: python Z = np.random.random((10,10)) Zmin, Zmax = Z.min(), Z.max() + print Zmin, Zmax + 3. Create a checkerboard 8x8 matrix using the tile function - .. code:: python + .. code-block:: python Z = np.tile( np.array([[0,1],[1,0]]), (4,4)) + print Z + 4. Normalize a 5x5 random matrix (between 0 and 1) - .. code:: python + .. code-block:: python Z = np.random.random((5,5)) Zmax,Zmin = Z.max(), Z.min() Z = (Z - Zmin)/(Zmax - Zmin) + print Z 5. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) - .. code:: python + .. code-block:: python Z = np.dot(np.ones((5,3)), np.ones((3,2))) + print Z -6. Create a 10x10 matrix with row values ranging from 0 to 9 +6. Create a 5x5 matrix with row values ranging from 0 to 4 - .. code:: python + .. code-block:: python - Z = np.zeros((10,10)) - Z += np.arange(10) + Z = np.zeros((5,5)) + Z += np.arange(5) + print Z -7. Create a vector of size 1000 with values ranging from 0 to 1, both excluded - .. code:: python +7. Create a vector of size 10 with values ranging from 0 to 1, both excluded - Z = np.random.linspace(0,1,1002,endpoint=True)[1:-1] + .. code-block:: python -8. Create a random vector of size 100 and sort it + Z = np.linspace(0,1,12,endpoint=True)[1:-1] + print Z - .. code:: python - Z = np.random.random(100) +8. Create a random vector of size 10 and sort it + + .. code-block:: python + + Z = np.random.random(10) Z.sort() + print Z -9. Consider two random matrices A anb B, check if they are equal. - .. code:: python +9. Consider two random array A anb B, check if they are equal. + + .. code-block:: python - A = np.random.randint(0,2,(2,2)) - B = np.random.randint(0,2,(2,2)) + A = np.random.randint(0,2,5) + B = np.random.randint(0,2,5) equal = np.allclose(A,B) + print equal -10. Create a random vector of size 1000 and find the mean value - .. code:: python +10. Create a random vector of size 30 and find the mean value - Z = np.random.random(1000) + .. code-block:: python + + Z = np.random.random(30) m = Z.mean() + print m @@ -188,43 +220,49 @@ Apprentice 1. Make an array immutable (read-only) - .. code:: python + .. code-block:: python Z = np.zeros(10) Z.flags.writeable = False + Z[0] = 1 -2. Consider a random 100x2 matrix representing cartesian coordinates, convert +2. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates - .. code:: python + .. code-block:: python - Z = np.random.random((100,2)) + Z = np.random.random((10,2)) X,Y = Z[:,0], Z[:,1] R = np.sqrt(X**2+Y**2) T = np.arctan2(Y,X) + print R + print T -3. Create random vector of size 100 and replace the maximum value by 0 +3. Create random vector of size 10 and replace the maximum value by 0 - .. code:: python + .. code-block:: python - Z = np.random.random(100) + Z = np.random.random(10) Z[Z.argmax()] = 0 + print Z 4. Create a structured array with ``x`` and ``y`` coordinates covering the [0,1]x[0,1] area. - .. code:: python + .. code-block:: python Z = np.zeros((10,10), [('x',float),('y',float)]) Z['x'], Z['y'] = np.meshgrid(np.linspace(0,1,10), np.linspace(0,1,10)) + print Z + 5. Print the minimum and maximum representable value for each numpy scalar type - .. code:: python + .. code-block:: python for dtype in [np.int8, np.int32, np.int64]: print np.iinfo(dtype).min @@ -237,43 +275,48 @@ Apprentice 6. Create a structured array representing a position (x,y) and a color (r,g,b) - .. code:: python + .. code-block:: python Z = np.zeros(10, [ ('position', [ ('x', float, 1), ('y', float, 1)]), ('color', [ ('r', float, 1), ('g', float, 1), ('b', float, 1)])]) + print Z 7. Consider a random vector with shape (100,2) representing coordinates, find point by point distances - .. code:: python + .. code-block:: python Z = np.random.random((10,2)) X,Y = np.atleast_2d(Z[:,0]), np.atleast_2d(Z[:,1]) D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2) + print D # Much faster with scipy + import scipy Z = np.random.random((10,2)) D = scipy.spatial.distance.cdist(Z,Z) - + print D 8. Generate a generic 2D Gaussian-like array - .. code:: python + .. code-block:: python - X, Y = np.meshgrid(np.linspace(-1,1,100), np.linspace(-1,1,100)) + X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10)) D = np.sqrt(X*X+Y*Y) sigma, mu = 1.0, 0.0 G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) ) + print G + 9. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value ? - .. code:: python + .. code-block:: python # Author: Warren Weckesser @@ -281,14 +324,17 @@ Apprentice nz = 3 Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz)) Z0[::nz+1] = Z + print Z0 10. Find the nearest value from a given value in an array - .. code:: python - - Z.flat[np.abs(Z - z).argmin()] + .. code-block:: python + Z = np.random.uniform(0,1,10) + z = 0.5 + m = Z.flat[np.abs(Z - z).argmin()] + print m Journeyman @@ -302,49 +348,53 @@ Journeyman How to read it ? - .. code:: python + .. code-block:: python - Z = genfromtxt("missing.dat", delimiter=",") + Z = np.genfromtxt("missing.dat", delimiter=",") 2. Consider a generator function that generates 10 integers and use it to build an array - .. code:: python + .. code-block:: python def generate(): for x in xrange(10): yield x Z = np.fromiter(generate(),dtype=float,count=-1) + print Z 3. Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices) ? - .. code:: python + .. code-block:: python # Author: Brett Olsen Z = np.ones(10) I = np.random.randint(0,len(Z),20) Z += np.bincount(I, minlength=len(Z)) + print Z 4. How to accumulate elements of a vector (X) to an array (F) based on an index list (I) ? - .. code:: python + .. code-block:: python # Author: Alan G Isaac X = [1,2,3,4,5,6] I = [1,3,9,3,4,1] F = np.bincount(I,X) + print F + 5. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors - .. code:: python + .. code-block:: python # Author: Nadav Horesh @@ -352,23 +402,22 @@ Journeyman I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte) F = I[...,0]*256*256 + I[...,1]*256 +I[...,2] n = len(np.unique(F)) - - np.unique(I) + print np.unique(I) 6. Considering a four dimensions array, how to get sum over the last two axis at once ? - - .. code:: python + .. code-block:: python A = np.random.randint(0,10,(3,4,3,4)) sum = A.reshape(A.shape[:-2] + (-1,)).sum(axis=-1) + print 7. Considering a one-dimensional vector D, how to compute means of subsets of D using a vector S of same size describing subset indices ? - .. code:: python + .. code-block:: python # Jaime Fernández del Río @@ -377,8 +426,7 @@ Journeyman D_sums = np.bincount(S, weights=D) D_counts = np.bincount(S) D_means = D_sums / D_counts - - + print D_means @@ -389,72 +437,77 @@ Craftsman first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1]) - .. code:: python + .. code-block:: python # Author: Joe Kington / Erik Rigtorp + from numpy.lib import stride_tricks def rolling(a, window): shape = (a.size - window + 1, window) strides = (a.itemsize, a.itemsize) - return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides) + return stride_tricks.as_strided(a, shape=shape, strides=strides) + Z = rolling(np.arange(10), 3) + print Z - Z = rolling(np.arange(100), 3) -2. Consider a set of 100 triplets describing 100 triangles (with shared +2. Consider a set of 10 triplets describing 10 triangles (with shared vertices), find the set of unique line segments composing all the triangles. - .. code:: python + .. code-block:: python # Author: Nicolas Rougier - faces = np.random.randint(0,100,(100,3)) - + faces = np.random.randint(0,100,(10,3)) F = np.roll(faces.repeat(2,axis=1),-1,axis=1) F = F.reshape(len(F)*3,2) F = np.sort(F,axis=1) G = F.view( dtype=[('p0',F.dtype),('p1',F.dtype)] ) G = np.unique(G) + print G 3. Given an array C that is a bincount, how to produce an array A such that np.bincount(A) == C ? - .. code:: python + .. code-block:: python # Jaime Fernández del Río C = np.bincount([1,1,2,3,4,4,6]) A = np.repeat(np.arange(len(C)), C) + print A Artisan ======= -1. Considering a 100x3 matrix, extract rows with unequal values (e.g. [2,2,3]) +1. Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3]) - .. code:: python + .. code-block:: python # Author: Robert Kern - Z = np.random.randint(0,5,(100,3)) + Z = np.random.randint(0,5,(10,3)) E = np.logical_and.reduce(Z[:,1:] == Z[:,:-1], axis=1) U = Z[~E] + print Z + print U 2. Convert a vector of ints into a matrix binary representation. - .. code:: python + .. code-block:: python # Author: Warren Weckesser I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128]) B = ((I.reshape(-1,1) & (2**np.arange(8))) != 0).astype(int) - B = B[:,::-1] + print B[:,::-1] # Author: Daniel T. McDonald I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128], dtype=np.uint8) - np.unpackbits(I[:, np.newaxis], axis=1) + print np.unpackbits(I[:, np.newaxis], axis=1) @@ -465,14 +518,14 @@ Adept fixed shape and centered on a given element (pad with a ``fill`` value when necessary) - .. code :: python + .. code:: python # Author: Nicolas Rougier - Z = np.random.random((25,25)) - shape = (3,3) + Z = np.random.randint(0,10,(10,10)) + shape = (5,5) fill = 0 - position = (0,0) + position = (1,1) R = np.ones(shape, dtype=Z.dtype)*fill P = np.array(list(position)).astype(int) @@ -492,20 +545,20 @@ Adept r = [slice(start,stop) for start,stop in zip(R_start,R_stop)] z = [slice(start,stop) for start,stop in zip(Z_start,Z_stop)] R[r] = Z[z] + print Z + print R 2. Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]] ? - .. code:: python + .. code-block:: python # Stéfan van der Walt - Z = np.arange(1,15) - R = as_strided(Z,(11,4),(4,4)) - - - + Z = np.arange(1,15,dtype=uint32) + R = stride_tricks.as_strided(Z,(11,4),(4,4)) + print R Expert @@ -515,7 +568,7 @@ Expert that contain elements of each row of B regardless of the order of the elements in B ? - .. code:: python + .. code-block:: python # Author: Gabe Schwartz @@ -524,11 +577,12 @@ Expert C = (A[..., np.newaxis, np.newaxis] == B) rows = (C.sum(axis=(1,2,3)) >= B.shape[1]).nonzero()[0] + print rows 2. Extract all the contiguous 3x3 blocks from a random 10x10 matrix. - .. code:: python + .. code-block:: python # Chris Barker @@ -537,11 +591,12 @@ Expert i = 1 + (Z.shape[0]-3) j = 1 + (Z.shape[1]-3) C = stride_tricks.as_strided(Z, shape=(i, j, n, n), strides=Z.strides + Z.strides) + print C 3. Create a 2D array subclass such that Z[i,j] == Z[j,i] - .. code:: python + .. code-block:: python # Eric O. Lebigot # Note: only works for 2d array and value setting using indices @@ -554,7 +609,7 @@ Expert def symetric(Z): return np.asarray(Z + Z.T - np.diag(Z.diagonal())).view(Symetric) - S = symetric(np.random.randint(0,10,(5,5)) + S = symetric(np.random.randint(0,10,(5,5))) S[2,3] = 42 print S @@ -566,14 +621,15 @@ Master .. note:: See `stackoverflow <http://stackoverflow.com/questions/16970982/find-unique-rows-in-numpy-array/>`_ for explanations. - .. code:: python + .. code-block:: python # Jaime Fernández del Río - Z = np.random.randint(0,2,(6,6)) + Z = np.random.randint(0,2,(6,3)) T = np.ascontiguousarray(Z).view(np.dtype((np.void, Z.dtype.itemsize * Z.shape[1]))) _, idx = np.unique(T, return_index=True) uZ = Z[idx] + print uZ