{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 4.6. Using stride tricks with NumPy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Every array has a number of dimensions, a shape, a data type, and strides. Strides are integer numbers describing, for each dimension, the byte step in the contiguous block of memory. The address of an item in the array is a linear combination of its indices: the coefficients are the strides." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "id = lambda x: x.__array_interface__['data'][0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.zeros(10); x.strides" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This vector contains float64 (8 bytes) items: one needs to go 8 bytes forward to go from one item to the next." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "y = np.zeros((10, 10)); y.strides" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the first dimension (vertical), one needs to go 80 bytes (10 float64 items) forward to go from one item to the next, because the items are internally stored in row-major order. In the second dimension (horizontal), one needs to go 8 bytes forward to go from one item to the next." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Broadcasting revisited" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We create a new array pointing to the same memory block as `a`, but with a different shape. The strides are such that this array looks like it is a vertically tiled version of `a`. NumPy is *tricked*: it thinks `b` is a 2D `n * n` array with `n^2` elements, whereas the data buffer really contains only `n` elements." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "n = 1000; a = np.arange(n)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b = np.lib.stride_tricks.as_strided(a, (n, n), (0, 4))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b.size, b.shape, b.nbytes" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%timeit b * b.T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This first version does not involve any copy, as `b` and `b.T` are arrays pointing to the same data buffer in memory, but with different strides." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%timeit np.tile(a, (n, 1)) * np.tile(a[:, np.newaxis], (1, n))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n", "\n", "> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }