Skip to content

Instantly share code, notes, and snippets.

@esc
Created May 28, 2014 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esc/1badb29b962417b6489b to your computer and use it in GitHub Desktop.
Save esc/1badb29b962417b6489b to your computer and use it in GitHub Desktop.
Numpy 100
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"signature": "sha256:4fd95f758aabd9296e34d4fe53d49997546d4446e5b43c4750077c2037d4b8ff"
},
"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",
"\n",
"::\n"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"#. Find indices of non-zero elements from [1,2,0,0,4,0]\n",
"\n",
" .. code-block:: 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](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:\n",
"[https://github.com/rougier/numpy-100](https://github.com/rougier/numpy-100)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Contents**\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* \n",
"\n",
"* \n",
"\n",
"* \n",
"\n",
"* \n",
"\n",
"* \n",
"\n",
"* \n",
"\n",
"* \n",
"\n",
"* \n",
"\n",
"* \n",
"\n",
"* \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.0\n",
"atlas_threads_info:\n",
" NOT AVAILABLE\n",
"blas_opt_info:\n",
" libraries = ['f77blas', 'cblas', 'atlas']\n",
" library_dirs = ['/home/esc/anaconda/lib']\n",
" define_macros = [('ATLAS_INFO', '\"\\\\\"3.8.4\\\\\"\"')]\n",
" language = c\n",
"atlas_blas_threads_info:\n",
" NOT AVAILABLE\n",
"openblas_info:\n",
" NOT AVAILABLE\n",
"lapack_opt_info:\n",
" libraries = ['lapack', 'f77blas', 'cblas', 'atlas']\n",
" library_dirs = ['/home/esc/anaconda/lib']\n",
" define_macros = [('ATLAS_INFO', '\"\\\\\"3.8.4\\\\\"\"')]\n",
" language = f77\n",
"atlas_info:\n",
" libraries = ['lapack', 'f77blas', 'cblas', 'atlas']\n",
" library_dirs = ['/home/esc/anaconda/lib']\n",
" define_macros = [('ATLAS_INFO', '\"\\\\\"3.8.4\\\\\"\"')]\n",
" language = f77\n",
"lapack_mkl_info:\n",
" NOT AVAILABLE\n",
"blas_mkl_info:\n",
" NOT AVAILABLE\n",
"atlas_blas_info:\n",
" libraries = ['f77blas', 'cblas', 'atlas']\n",
" library_dirs = ['/home/esc/anaconda/lib']\n",
" define_macros = [('ATLAS_INFO', '\"\\\\\"3.8.4\\\\\"\"')]\n",
" language = c\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)"
],
"language": "python",
"metadata": {},
"outputs": [],
"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"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a vector with values ranging from 10 to 99\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Z = np.arange(10,100)"
],
"language": "python",
"metadata": {},
"outputs": [],
"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)"
],
"language": "python",
"metadata": {},
"outputs": [],
"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])"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a 3x3 identity matrix\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Z = np.eye(3)"
],
"language": "python",
"metadata": {},
"outputs": [],
"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)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a 10x10x10 array with random values\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Z = np.random.random((10,10,10))"
],
"language": "python",
"metadata": {},
"outputs": [],
"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))\n",
"Z[1::2,::2] = 1\n",
"Z[::2,1::2] = 1"
],
"language": "python",
"metadata": {},
"outputs": [],
"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()"
],
"language": "python",
"metadata": {},
"outputs": [],
"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))"
],
"language": "python",
"metadata": {},
"outputs": [],
"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)"
],
"language": "python",
"metadata": {},
"outputs": [],
"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)))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a 10x10 matrix with row values ranging from 0 to 9\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Z = np.zeros((10,10))\n",
"Z += np.arange(10)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a vector of size 1000 with values ranging from 0 to 1, both\n",
"excluded\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Z = np.random.linspace(0,1,1002,endpoint=True)[1:-1]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'module' object has no attribute 'linspace'",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-17-e5d39b8d39cb>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mZ\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlinspace\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1002\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mendpoint\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m: 'module' object has no attribute 'linspace'"
]
}
],
"prompt_number": 17
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a random vector of size 100 and sort it\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Z = np.random.random(100)\n",
"Z.sort()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Consider two random matrices A anb B, check if they are equal.\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"A = np.random.randint(0,2,(2,2))\n",
"B = np.random.randint(0,2,(2,2))\n",
"equal = np.allclose(A,B)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 19
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a random vector of size 1000 and find the mean value\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Z = np.random.random(1000)\n",
"m = Z.mean()"
],
"language": "python",
"metadata": {},
"outputs": [],
"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"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 21
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Consider a random 100x2 matrix representing cartesian coordinates,\n",
"convert them to polar coordinates\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Z = np.random.random((100,2))\n",
"X,Y = Z[:,0], Z[:,1]\n",
"R = np.sqrt(X**2+Y**2)\n",
"T = np.arctan2(Y,X)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 22
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create random vector of size 100 and replace the maximum value by 0\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Z = np.random.random(100)\n",
"Z[Z.argmax()] = 0"
],
"language": "python",
"metadata": {},
"outputs": [],
"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))"
],
"language": "python",
"metadata": {},
"outputs": [],
"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)])])"
],
"language": "python",
"metadata": {},
"outputs": [],
"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",
"\n",
"# Much faster with scipy\n",
"Z = np.random.random((10,2))\n",
"D = scipy.spatial.distance.cdist(Z,Z)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'scipy' is not defined",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-27-bc6bf442f025>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;31m# Much faster with scipy\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0mZ\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mD\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mscipy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mspatial\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdistance\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcdist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mZ\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mZ\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mNameError\u001b[0m: name 'scipy' is not defined"
]
}
],
"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,100), np.linspace(-1,1,100))\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 ) ) )"
],
"language": "python",
"metadata": {},
"outputs": [],
"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"
],
"language": "python",
"metadata": {},
"outputs": [],
"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.flat[np.abs(Z - z).argmin()]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'z' is not defined",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-30-1fd90557d1cc>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mZ\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mflat\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mabs\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mZ\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mz\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0margmin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mNameError\u001b[0m: name 'z' is not defined"
]
}
],
"prompt_number": 30
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Journeyman"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Consider the following [file:](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 = genfromtxt(\"missing.dat\", delimiter=\",\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'genfromtxt' is not defined",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-31-ec7cc23ee08d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mZ\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgenfromtxt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"missing.dat\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdelimiter\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\",\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mNameError\u001b[0m: name 'genfromtxt' is not defined"
]
}
],
"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)"
],
"language": "python",
"metadata": {},
"outputs": [],
"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))"
],
"language": "python",
"metadata": {},
"outputs": [],
"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)"
],
"language": "python",
"metadata": {},
"outputs": [],
"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",
"\n",
"np.unique(I)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 35,
"text": [
"array([0, 1], dtype=uint8)"
]
}
],
"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)"
],
"language": "python",
"metadata": {},
"outputs": [],
"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"
],
"language": "python",
"metadata": {},
"outputs": [],
"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",
"\n",
"def rolling(a, window):\n",
" shape = (a.size - window + 1, window)\n",
" strides = (a.itemsize, a.itemsize)\n",
" return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)\n",
"\n",
"Z = rolling(np.arange(100), 3)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 38
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Consider a set of 100 triplets describing 100 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,(100,3))\n",
"\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)"
],
"language": "python",
"metadata": {},
"outputs": [],
"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)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 40
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Artisan"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Considering a 100x3 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,(100,3))\n",
"E = np.logical_and.reduce(Z[:,1:] == Z[:,:-1], axis=1)\n",
"U = Z[~E]"
],
"language": "python",
"metadata": {},
"outputs": [],
"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",
"B = 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",
"np.unpackbits(I[:, np.newaxis], axis=1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 42,
"text": [
"array([[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]], dtype=uint8)"
]
}
],
"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": "code",
"collapsed": false,
"input": [
"# Author: Nicolas Rougier\n",
"\n",
"Z = np.random.random((25,25))\n",
"shape = (3,3)\n",
"fill = 0\n",
"position = (0,0)\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]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 43
},
{
"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)\n",
"R = as_strided(Z,(11,4),(4,4))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'as_strided' is not defined",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-44-372239c684a6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mZ\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m15\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mR\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mas_strided\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mZ\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m11\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mNameError\u001b[0m: name 'as_strided' is not defined"
]
}
],
"prompt_number": 44
},
{
"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]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 45
},
{
"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)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'stride_tricks' is not defined",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-46-be634ecf5686>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mi\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mZ\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0mj\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mZ\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mC\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mstride_tricks\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mas_strided\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mZ\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mshape\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mn\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstrides\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mZ\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstrides\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mZ\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstrides\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mNameError\u001b[0m: name 'stride_tricks' is not defined"
]
}
],
"prompt_number": 46
},
{
"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": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-47-e55f32d39787>, line 13)",
"output_type": "pyerr",
"traceback": [
"\u001b[1;36m File \u001b[1;32m\"<ipython-input-47-e55f32d39787>\"\u001b[1;36m, line \u001b[1;32m13\u001b[0m\n\u001b[1;33m S[2,3] = 42\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
]
}
],
"prompt_number": 47
},
{
"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,6))\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]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 48
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Archmaster"
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment