Last active
January 3, 2016 02:28
-
-
Save jaimefrio/8395341 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "What's new in numpy 1.8" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "What's new in NumPy 1.8\n=======================\n\nRecent NumPy releases\n---------------------\n* 1.5.0 - August 2010\n * 1.5.1 - February 2011 (bug fixes)\n* 1.6.0 \u2013 May 2011\n * 1.6.1, 1.6.2 \u2013 July 2011, August 2012 (bug fixes)\n* 1.7.0 \u2013 February 2013\n * 1.7.1, 1.7.2 \u2013 April 2013, December 2013 (bug fixes)\n* 1.8.0 \u2013 October 2013" | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "What numpy are you running?\n---------------------------" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "import numpy as np\nnp.__version__", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 1, | |
"text": "'1.8.0'" | |
} | |
], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "From the release notes\n----------------------\n\n**Python versions supported**\n\n* This release supports Python 2.6 - 2.7 and 3.2 \u2013 3.3.\n* Support for Python 2.4 \u2013 2.5 has been dropped.\n\n**Highlights**\n\n* New, gufuncs for linear algebra.\n* New, in-place fancy indexing with the `.at` method.\n* New, `partition` function, partial sorting via selection.\n* New, `nanmean`, `nanvar`, and `nanstd` functions.\n* New, `full` and `full_like` functions for initialized arrays.\n\n**Highlight from 1.7.0**\n\n* New, reduction ufuncs generalize `axis=` parameter.\n" | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "`full` and `full_like`\n----------------------" | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "**Before**" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "np.ones((2, 4)) * 5", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 2, | |
"text": "array([[ 5., 5., 5., 5.],\n [ 5., 5., 5., 5.]])" | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "a = np.empty((2, 4))\na.fill(5)\na", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 3, | |
"text": "array([[ 5., 5., 5., 5.],\n [ 5., 5., 5., 5.]])" | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "**After**" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "np.full((2, 4), fill_value=5)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 4, | |
"text": "array([[ 5., 5., 5., 5.],\n [ 5., 5., 5., 5.]])" | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "np.full_like(a, fill_value=5)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 5, | |
"text": "array([[ 5., 5., 5., 5.],\n [ 5., 5., 5., 5.]])" | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "`nanmean`, `nanstd` and `nanvar`\n--------------------------------" | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "**Setup**" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "a = np.arange(5, dtype=np.double)\na[2] = np.nan\na\n", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 6, | |
"text": "array([ 0., 1., nan, 3., 4.])" | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "**Before**" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "np.mean(a)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 7, | |
"text": "nan" | |
} | |
], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "np.mean(a[~np.isnan(a)])", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 8, | |
"text": "2.0" | |
} | |
], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "**After**" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "np.nanmean(a)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 9, | |
"text": "2.0" | |
} | |
], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "`.at`\n-----" | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "**Setup**" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "a = np.zeros(3)\nidx = [0, 0, 1, 2, 2 ,2]\na\n", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 10, | |
"text": "array([ 0., 0., 0.])" | |
} | |
], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "**Before**" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "a[idx] += 1\na", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 11, | |
"text": "array([ 1., 1., 1.])" | |
} | |
], | |
"prompt_number": 11 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "a[:] = 0\na += np.bincount(idx)\na", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 12, | |
"text": "array([ 2., 1., 3.])" | |
} | |
], | |
"prompt_number": 12 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "**After**" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "a[:] = 0\nnp.add.at(a, idx, 1)\na", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 13, | |
"text": "array([ 2., 1., 3.])" | |
} | |
], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "a[:] = 1\nnp.multiply.at(a, idx, 2)\na", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 14, | |
"text": "array([ 4., 2., 8.])" | |
} | |
], | |
"prompt_number": 14 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "`partition`\n-----------" | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "**Setup**" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "a = np.arange(11)\nnp.random.shuffle(a)\na", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 15, | |
"text": "array([ 7, 6, 2, 4, 3, 8, 1, 10, 5, 9, 0])" | |
} | |
], | |
"prompt_number": 15 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "**Before**\n" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "np.sort(a)[5]", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 16, | |
"text": "5" | |
} | |
], | |
"prompt_number": 16 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "**After**" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "np.partition(a, 5)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 17, | |
"text": "array([ 3, 1, 2, 0, 4, 5, 6, 7, 10, 9, 8])" | |
} | |
], | |
"prompt_number": 17 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "np.partition(a, 5)[5]", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 18, | |
"text": "5" | |
} | |
], | |
"prompt_number": 18 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "`partition` can be much faster than sorting..." | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "a = np.random.rand(1e7)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 19 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "%timeit np.sort(a)[:5]", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "1 loops, best of 3: 1.36 s per loop\n" | |
} | |
], | |
"prompt_number": 20 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "%timeit np.partition(a, np.arange(5))[:5]", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "10 loops, best of 3: 118 ms per loop\n" | |
} | |
], | |
"prompt_number": 21 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "np.allclose(np.sort(a)[:5], np.partition(a, np.arange(5))[:5])", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 22, | |
"text": "True" | |
} | |
], | |
"prompt_number": 22 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "`solve` and several other linalg gufuncs\n----------------------------------------" | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "**Setup**" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "a = np.random.rand(1e6, 2, 2)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 30 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "**Before**\n" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "%%timeit\nout = np.empty((1e6 ,2))\nfor j, mat in enumerate(a):\n out[j] = np.linalg.solve(mat, [1, 1])", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "1 loops, best of 3: 29.7 s per loop\n" | |
} | |
], | |
"prompt_number": 24 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "**After**" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "%timeit np.linalg.solve(a, [1, 1])", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "1 loops, best of 3: 417 ms per loop\n" | |
} | |
], | |
"prompt_number": 25 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "Full list of available gufuncs:\n\n* `cholesky` - Cholesky decomposition\n* `det` - determinant\n* `eig` - eigenvalues and right eigenvectors\n* `eigh` - eigenvalues and right eigenvectors (hermitian/symmetric)\n* `eigvals` - eigenvalues\n* `eigvalsh` - eigenvalues (hermitian/symmetric)\n* `inv` - multiplicative matrix inverse\n* `slogdet` - sign and logarithm of determinant\n* `solve` - solve system of linear equations\n* `svd` - singular value decomposition" | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "generalized `axis=` parameter for ufunc reduction\n-------------------------------------------------" | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "**Setup**" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "a = np.random.rand(1000, 1000, 10)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 26 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "**Before**\n" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "%timeit a.max(axis=-1).max(axis=-1)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "10 loops, best of 3: 67.1 ms per loop\n" | |
} | |
], | |
"prompt_number": 27 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "%timeit a.max(axis=-2).max(axis=-1)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "10 loops, best of 3: 33.2 ms per loop\n" | |
} | |
], | |
"prompt_number": 28 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "**After**" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "%timeit a.max(axis=(-1, -2))", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "100 loops, best of 3: 10.2 ms per loop\n" | |
} | |
], | |
"prompt_number": 29 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment