diff --git a/.gitignore b/.gitignore index 24198a7..abd74d0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ .ipynb_checkpoints my_*.png my_*.mp4 +my_*.csv +my_*.npy +my_*.npz diff --git a/tools_numpy.ipynb b/tools_numpy.ipynb index a94ed77..515d02f 100644 --- a/tools_numpy.ipynb +++ b/tools_numpy.ipynb @@ -22,6 +22,13 @@ "import numpy as np" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `np.zeros`" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -62,7 +69,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Some vocabulary:\n", + "### Some vocabulary\n", "\n", "* In NumPy, each dimension is called an **axis**.\n", "* The number of axes is called the **rank**.\n", @@ -93,6 +100,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "### N-dimensional arrays\n", "You can also create an N-dimensional array of arbitrary rank. For example, here's a 3D array (rank=3), with shape `(2,3,4)`:" ] }, @@ -111,7 +119,8 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Let's look at the type of these arrays:" + "### Array type\n", + "NumPy arrays have the type `ndarray`s:" ] }, { @@ -129,6 +138,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "### `np.ones`\n", "Many other NumPy functions create `ndarrays`.\n", "\n", "Here's a 3x4 matrix full of ones:" @@ -149,12 +159,32 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "An uninitialized 2x3 array (its content is not predictable, as it is whatever is in memory at that point):" + "### `np.full`\n", + "Creates an array of the given shape initialized with the given value. Here's a 3x4 matrix full of `π`." ] }, { "cell_type": "code", "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print np.full((3,4), np.pi)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `np.empty`\n", + "An uninitialized 2x3 array (its content is not predictable, as it is whatever is in memory at that point):" + ] + }, + { + "cell_type": "code", + "execution_count": 9, "metadata": { "collapsed": false, "scrolled": true @@ -168,32 +198,34 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Of course you can initialize an `ndarray` using a regular python array (or any iterable). Just call the `array` function:" + "### np.array\n", + "Of course you can initialize an `ndarray` using a regular python array. Just call the `array` function:" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "a = np.array([[1,2,3,4], [10, 20, 30, 40]])\n", - "print type(a)\n", - "print a" + "b = np.array([[1,2,3,4], [10, 20, 30, 40]])\n", + "print type(b)\n", + "print b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ + "### `np.arange`\n", "You can create an `ndarray` using NumPy's `range` function, which is similar to python's built-in `range` function:" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 11, "metadata": { "collapsed": false, "scrolled": true @@ -212,7 +244,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 12, "metadata": { "collapsed": false }, @@ -230,7 +262,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "metadata": { "collapsed": false }, @@ -248,7 +280,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 14, "metadata": { "collapsed": false }, @@ -263,12 +295,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "### `np.linspace`\n", "For this reason, it is generally preferable to use the `linspace` function instead of `arange` when working with floats. The `linspace` function returns an array containing a specific number of points evenly distributed between two values (note that the maximum value is *included*, contrary to `arange`):" ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 15, "metadata": { "collapsed": false }, @@ -281,13 +314,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "### `np.rand` and `np.randn`\n", "A number of functions are available in NumPy's `random` module to create `ndarray`s initialized with random values.\n", "For example, here is a 3x4 matrix initialized with random floats between 0 and 1 (uniform distribution):" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 16, "metadata": { "collapsed": false }, @@ -305,7 +339,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 17, "metadata": { "collapsed": false }, @@ -323,7 +357,7 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": 153, "metadata": { "collapsed": false, "scrolled": false @@ -346,12 +380,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "### np.fromfunction\n", "You can also initialize an `ndarray` using a function:" ] }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 19, "metadata": { "collapsed": false }, @@ -367,7 +402,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "NumPy first creates three `ndarrays` (one per dimension), each of shape `(3, 2, 10)`. Each array has values equal to the coordinate along a specific axis. For example, all elements in the `z` array are equal to the z-coordinate:\n", + "NumPy first creates three `ndarrays` (one per dimension), each of shape `(3, 2, 10)`. Each array has values equal to the coordinate along a specific axis. For example, all elements in the `z` array are equal to their z-coordinate:\n", "\n", " [[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]\n", @@ -378,7 +413,7 @@ " [[ 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.]\n", " [ 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.]]]\n", "\n", - "This means that `my_function` is only called once, so the initialization is very efficient." + "So the terms x, y and z in the expression `x * y + z` above are in fact `ndarray`s (we will discuss arithmetic operations on arrays below). The point is that the function `my_function` is only called *once*, instead of once per element. This makes initialization very efficient." ] }, { @@ -386,24 +421,25 @@ "metadata": {}, "source": [ "## Array data\n", - "NumPy's `ndarray`s are very efficient in part because all their elements must have the same type (usually numbers).\n", + "### `dtype`\n", + "NumPy's `ndarray`s are also efficient in part because all their elements must have the same type (usually numbers).\n", "You can check what the data type is by looking at the `dtype` attribute:" ] }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 20, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ - "a = np.arange(1, 5)\n", - "print a.dtype, a\n", + "c = np.arange(1, 5)\n", + "print c.dtype, c\n", "\n", - "b = np.arange(1.0, 5.0)\n", - "print b.dtype, b" + "c = np.arange(1.0, 5.0)\n", + "print c.dtype, c" ] }, { @@ -413,27 +449,6 @@ "Instead of letting NumPy guess what data type to use, you can set it explicitly when creating an array by setting the `dtype` parameter:" ] }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "a = np.arange(1, 5, dtype=np.complex64)\n", - "print a.dtype, a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Available data types include int8, int16, int32, int64, uint8/16/32/64, float16/32/64 and complex64/128. Check out [the documentation](http://docs.scipy.org/doc/numpy-1.10.1/user/basics.types.html) for the full list.\n", - "\n", - "The `itemsize` attribute returns the size (in bytes) of each item:" - ] - }, { "cell_type": "code", "execution_count": 21, @@ -442,31 +457,54 @@ }, "outputs": [], "source": [ - "a = np.arange(1, 5, dtype=np.complex64)\n", - "print a.itemsize" + "d = np.arange(1, 5, dtype=np.complex64)\n", + "print d.dtype, d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "An array's data is actually stored in memory as a flat (one dimensional) byte buffer. It is available *via* the `data` attribute (you will rarely need it, though)." + "Available data types include `int8`, `int16`, `int32`, `int64`, `uint8`/`16`/`32`/`64`, `float16`/`32`/`64` and `complex64`/`128`. Check out [the documentation](http://docs.scipy.org/doc/numpy-1.10.1/user/basics.types.html) for the full list.\n", + "\n", + "### `itemsize`\n", + "The `itemsize` attribute returns the size (in bytes) of each item:" ] }, { "cell_type": "code", "execution_count": 22, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "e = np.arange(1, 5, dtype=np.complex64)\n", + "print e.itemsize" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `data` buffer\n", + "An array's data is actually stored in memory as a flat (one dimensional) byte buffer. It is available *via* the `data` attribute (you will rarely need it, though)." + ] + }, + { + "cell_type": "code", + "execution_count": 23, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [], "source": [ - "a = np.array([[1,2],[1000, 2000]], dtype=np.int32)\n", + "f = np.array([[1,2],[1000, 2000]], dtype=np.int32)\n", "print \"Array:\"\n", - "print a\n", + "print f\n", "print \"Raw data:\"\n", - "print [ord(c) for c in a.data]" + "print [ord(item) for item in f.data]" ] }, { @@ -481,22 +519,10 @@ "metadata": {}, "source": [ "## Reshaping an array\n", + "### In place\n", "Changing the shape of an `ndarray` is as simple as setting its `shape` attribute. However, the array's size must remain the same." ] }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "a = np.arange(24)\n", - "print a\n", - "print \"Rank:\", a.ndim" - ] - }, { "cell_type": "code", "execution_count": 24, @@ -505,30 +531,22 @@ }, "outputs": [], "source": [ - "a.shape = (6, 4)\n", - "print a\n", - "print \"Rank:\", a.ndim" + "g = np.arange(24)\n", + "print g\n", + "print \"Rank:\", g.ndim" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { - "collapsed": false, - "scrolled": true + "collapsed": false }, "outputs": [], "source": [ - "a.shape = (2, 3, 4)\n", - "print a\n", - "print \"Rank:\", a.ndim" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The `reshape` function returns a new `ndarray` object pointing to the *same* data. This means that modifying one array will also modify the other." + "g.shape = (6, 4)\n", + "print g\n", + "print \"Rank:\", g.ndim" ] }, { @@ -540,9 +558,31 @@ }, "outputs": [], "source": [ - "a2 = a.reshape(4,6)\n", - "print a2\n", - "print \"Rank:\", a2.ndim" + "g.shape = (2, 3, 4)\n", + "print g\n", + "print \"Rank:\", g.ndim" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `reshape`\n", + "The `reshape` function returns a new `ndarray` object pointing at the *same* data. This means that modifying one array will also modify the other." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [], + "source": [ + "g2 = g.reshape(4,6)\n", + "print g2\n", + "print \"Rank:\", g2.ndim" ] }, { @@ -552,25 +592,6 @@ "Set item at row 1, col 2 to 999 (more about indexing below)." ] }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "a2[1, 2] = 999\n", - "print a2" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The corresponding element in a has been modified." - ] - }, { "cell_type": "code", "execution_count": 28, @@ -579,14 +600,15 @@ }, "outputs": [], "source": [ - "print a" + "g2[1, 2] = 999\n", + "print g2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Finally, the `ravel` function returns a new one-dimensional `ndarray` that also points to the same data:" + "The corresponding element in `g` has been modified." ] }, { @@ -597,16 +619,15 @@ }, "outputs": [], "source": [ - "a3 = a.ravel()\n", - "print a3" + "print g" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Array indexing\n", - "One-dimensional NumPy arrays can be accessed more or less like regular python arrays:" + "### `ravel`\n", + "Finally, the `ravel` function returns a new one-dimensional `ndarray` that also points to the same data:" ] }, { @@ -617,637 +638,8 @@ }, "outputs": [], "source": [ - "a = np.array([1, 5, 3, 19, 13, 7, 3])\n", - "print a[3]" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print a[2:5]" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print a[2:-1]" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print a[:2]" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print a[2::2]" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print a[::-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Of course, you can modify elements:" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "a[3]=999\n", - "print a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You can also modify an `ndarray` slice:" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "a[2:5] = [997, 998, 999]\n", - "print a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And if you assign a single value, it is copied across the whole slice (this is called *broadcasting*, more on this below):" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "a[2:5] = -1\n", - "print a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Contrary to regular python arrays, you cannot grow or shrink `ndarray`s this way:" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false, - "scrolled": false - }, - "outputs": [], - "source": [ - "try:\n", - " a[2:5] = [1,2,3,4,5,6] # too long\n", - "except ValueError, e:\n", - " print e" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You cannot delete elements either:" - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "try:\n", - " del a[2:5]\n", - "except ValueError, e:\n", - " print e" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Multi-dimensional arrays can be accessed in a similar way by providing an index or slice for each axis, separated by commas:" - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "b = np.arange(48).reshape(4, 12)\n", - "print b" - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print b[1, 2] # row 1, col 2" - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print b[1, :] # row 1, all columns" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print b[:, 1] # all rows, column 1" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [], - "source": [ - "print b[(0,2), 2:5] # rows 0 and 2, columns 2 to 4 (5-1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You can also provide an `ndarray` of boolean values to specify the indices that you want to access. This will come in handy later:" - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "bools = np.array([True, False, True, False])\n", - "print b[bools, :] # Rows 0 and 2, all columns. Equivalent to b[(1, 3), :]" - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print b[b % 3 == 1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Note the subtle difference between these two expressions: " - ] - }, - { - "cell_type": "code", - "execution_count": 48, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print b[1, :]\n", - "print b[1:2, :]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The first expression returns row 1 as a 1D array of shape `(12,)`, while the second returns that same row as a 2D array of shape `(1, 12)`." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Everything works just as well with higher dimension arrays:" - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "c = b.reshape(4,2,6)\n", - "print c" - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print c[2, 1, 4] # matrix 2, row 1, col 4" - ] - }, - { - "cell_type": "code", - "execution_count": 51, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print c[2, :, 3] # matrix 2, all rows, col 3" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you omit coordinates for some axes, then all elements in these axes are returned:" - ] - }, - { - "cell_type": "code", - "execution_count": 52, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print c[2, 1] # Return matrix 2, row 1, all columns. This is equivalent to c[2, 1, :]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You may also write an ellipsis (`...`) to specify that all non-specified axes must be entirely included." - ] - }, - { - "cell_type": "code", - "execution_count": 53, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print c[2, ...] # matrix 2, all rows, all columns. This is equivalent to c[2, :, :]" - ] - }, - { - "cell_type": "code", - "execution_count": 54, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print c[2, 1, ...] # matrix 2, row 1, all columns. This is equivalent to c[2, 1, :]" - ] - }, - { - "cell_type": "code", - "execution_count": 55, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print c[2, ..., 3] # matrix 2, all rows, column 3. This is equivalent to c[2, :, 3]" - ] - }, - { - "cell_type": "code", - "execution_count": 56, - "metadata": { - "collapsed": false, - "scrolled": false - }, - "outputs": [], - "source": [ - "print c[..., 3] # all matrices, all rows, column 3. This is equivalent to c[:, :, 3]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Broadcasting" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As we discussed above, assigning to an `ndarray` slice requires an `ndarray` of the same shape as the slice. In general, when NumPy expects arrays of the same shape but finds that this is not the case, it applies the so-called *broadcasting* rules:\n", - "\n", - "**First rule**: if the arrays do not have the same rank, then a 1 will be prepended to the smaller ranking arrays until their ranks match.\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 57, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "a = np.arange(10).reshape(1, 1, 10)\n", - "print a" - ] - }, - { - "cell_type": "code", - "execution_count": 58, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print \"Slice:\", a[..., 2:4]\n", - "print \"Shape:\", a[..., 2:4].shape" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's try to assign a 1D array of shape `(2,)` to this 3D array of shape `(1,1,2)`. Applying the first rule of broadcasting!" - ] - }, - { - "cell_type": "code", - "execution_count": 59, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "a[..., 2:4] = [55, 66] # acts as [[[55, 56]]]\n", - "print a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Second rule**: arrays with a 1 along a particular dimension act as if they had the size of the array with the largest shape along that dimension. The value of the array element is repeated along that dimension." - ] - }, - { - "cell_type": "code", - "execution_count": 60, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "b = np.arange(10).reshape(2, 5)\n", - "print b" - ] - }, - { - "cell_type": "code", - "execution_count": 61, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print \"Slice:\"\n", - "print b[..., 1:4]\n", - "print \"Shape:\", b[..., 1:4].shape" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try to assign a 2D array of shape `(2,1)` to this slice of shape `(2, 3)`. NumPy will apply the second rule of broadcasting:" - ] - }, - { - "cell_type": "code", - "execution_count": 62, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "b[..., 1:4] = [[44], [55]] # acts as [[44, 44, 44], [55, 55, 55]]\n", - "print b" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Combining rules 1 & 2, we can do this:" - ] - }, - { - "cell_type": "code", - "execution_count": 63, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "b[..., 1:4] = [66, 77, 88] # after rule 1: [[66, 77, 88]], and after rule 2: [[66, 77, 88], [66, 77, 88]]\n", - "print b" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And also, very simply:" - ] - }, - { - "cell_type": "code", - "execution_count": 64, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "b[..., 1:4] = 99 # after rule 1: [[99]], and after rule 2: [[99, 99, 99], [99, 99, 99]]\n", - "print b" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Third rule**: after rules 1 & 2, the sizes of all arrays must match." - ] - }, - { - "cell_type": "code", - "execution_count": 65, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "try:\n", - " b[..., 1:4] = [33, 44]\n", - "except ValueError, e:\n", - " print e" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Broadcasting rules are used in many NumPy operations, not just assignment, as we will see below.\n", - "For more details about broadcasting, check out [the documentation](https://docs.scipy.org/doc/numpy-dev/user/basics.broadcasting.html)." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Iterating\n", - "Iterating over `ndarray`s is very similar to iterating over regular python arrays. Note that iterating over multidimensional arrays is done with respect to the first axis." - ] - }, - { - "cell_type": "code", - "execution_count": 66, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "c = np.arange(24).reshape(2, 2, 6) # A 3D array (composed of two 2x6 matrices)\n", - "print c" - ] - }, - { - "cell_type": "code", - "execution_count": 67, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "for m in c:\n", - " print \"Item:\"\n", - " print m" - ] - }, - { - "cell_type": "code", - "execution_count": 68, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "for i in range(len(c)): # Note that len(c) == c.shape[0]\n", - " print \"Item:\"\n", - " print c[i]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you want to iterate on *all* elements in the `ndarray`, simply iterate over the `flat` attribute:" - ] - }, - { - "cell_type": "code", - "execution_count": 69, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "for i in c.flat:\n", - " print \"Item:\", i" + "g3 = g.ravel()\n", + "print g3" ] }, { @@ -1255,12 +647,12 @@ "metadata": {}, "source": [ "## Arithmetic operations\n", - "All the usual arithmetic operators (`+`, `-`, `*`, `/`, `**`, etc.) can be used with `ndarray`s. They apply elementwise:" + "All the usual arithmetic operators (`+`, `-`, `*`, `/`, `**`, etc.) can be used with `ndarray`s. They apply *elementwise*:" ] }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 31, "metadata": { "collapsed": false, "scrolled": false @@ -1283,18 +675,216 @@ "source": [ "Note that the multiplication is *not* a matrix multiplication. We will discuss matrix operations below.\n", "\n", - "The arrays must have the same shape. If they do not, NumPy will apply the broadcasting rules, as discussed above." + "The arrays must have the same shape. If they do not, NumPy will apply the *broadcasting rules*." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Broadcasting" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In general, when NumPy expects arrays of the same shape but finds that this is not the case, it applies the so-called *broadcasting* rules:\n", + "\n", + "### First rule\n", + "*If the arrays do not have the same rank, then a 1 will be prepended to the smaller ranking arrays until their ranks match.*" ] }, { "cell_type": "code", - "execution_count": 71, + "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print a * 3 # thanks to broadcasting, this is equivalent to: a * [3, 3, 3, 3]" + "h = np.arange(5).reshape(1, 1, 5)\n", + "print h" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's try to add a 1D array of shape `(5,)` to this 3D array of shape `(1,1,5)`. Applying the first rule of broadcasting!" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print h + [10, 20, 30, 40, 50] # same as: h + [[[10, 20, 30, 40, 50]]]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Second rule\n", + "*Arrays with a 1 along a particular dimension act as if they had the size of the array with the largest shape along that dimension. The value of the array element is repeated along that dimension.*" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "k = np.arange(6).reshape(2, 3)\n", + "print k" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's try to add a 2D array of shape `(2,1)` to this 2D `ndarray` of shape `(2, 3)`. NumPy will apply the second rule of broadcasting:" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print k + [[100], [200]] # same as: k + [[100, 100, 100], [200, 200, 200]]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Combining rules 1 & 2, we can do this:" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print k + [100, 200, 300] # after rule 1: [[100, 200, 300]], and after rule 2: [[100, 200, 300], [100, 200, 300]]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And also, very simply:" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print k + 1000 # same as: k + [[1000, 1000, 1000], [1000, 1000, 1000]]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Third rule\n", + "*After rules 1 & 2, the sizes of all arrays must match.*" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "try:\n", + " print k + [33, 44]\n", + "except ValueError, e:\n", + " print e" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Broadcasting rules are used in many NumPy operations, not just arithmetic operations, as we will see below.\n", + "For more details about broadcasting, check out [the documentation](https://docs.scipy.org/doc/numpy-dev/user/basics.broadcasting.html)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Upcasting\n", + "When trying to combine arrays with different `dtype`s, NumPy will *upcast* to a type capable of handling all possible values (regardless of what the *actual* values are)." + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "k1 = np.arange(0, 5, dtype=np.uint8)\n", + "print k1.dtype, k1" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "k2 = k1 + np.array([5, 6, 7, 8, 9], dtype=np.int8)\n", + "print k2.dtype, k2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Note that `int16` is required to represent all *possible* `int8` and `uint8` values (from -128 to 255), even though in this case a uint8 would have sufficed." + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "k3 = k1 + 1.5\n", + "print k3.dtype, k3" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Conditional operators" ] }, { @@ -1306,13 +896,14 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print a < [15, 16, 35, 36]" + "m = np.array([20, -5, 30, 40])\n", + "print m < [15, 16, 35, 36]" ] }, { @@ -1324,20 +915,510 @@ }, { "cell_type": "code", - "execution_count": 73, + "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print a < 25 # equivalent to a < [25, 25, 25, 25]" + "print m < 25 # equivalent to m < [25, 25, 25, 25]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "This is most useful in conjunction with boolean indexing:" + "This is most useful in conjunction with boolean indexing (discussed below)." + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print m[m < 25]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Mathematical and statistical functions" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Many mathematical and statistical functions are available for `ndarray`s.\n", + "\n", + "### `ndarray` methods\n", + "Some functions are simply `ndarray` methods, for example:" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a = np.array([[-2.5, 3.1, 7], [10, 11, 12]])\n", + "print a\n", + "print \"mean =\", a.mean()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Note that this computes the mean of all elements in the `ndarray`, regardless of its shape.\n", + "\n", + "Here are a few more useful `ndarray` methods:" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "for func in (a.min, a.max, a.sum, a.prod, a.std, a.var):\n", + " print func.__name__, \"=\", func()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "These functions accept an optional argument `axis` which lets you ask for the operation to be performed on elements along the given axis. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "c=np.arange(24).reshape(2,3,4)\n", + "print c" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print c.sum(axis=0) # sum across matrices" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print c.sum(axis=1) # sum across rows" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can also sum over multiple axes:" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print c.sum(axis=(0,2)) # sum across matrices and columns\n", + "print 0+1+2+3 + 12+13+14+15, 4+5+6+7 + 16+17+18+19, 8+9+10+11 + 20+21+22+23" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Universal functions\n", + "NumPy also provides fast elementwise functions called *universal functions*, or **ufunc**. They are vectorized wrappers of simple functions. For example `square` returns a new `ndarray` which is a copy of the original `ndarray` except that each element is squared:" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a = np.array([[-2.5, 3.1, 7], [10, 11, 12]])\n", + "print np.square(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here are a few more useful unary ufuncs:" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print \"Original ndarray\"\n", + "print a\n", + "for func in (np.abs, np.sqrt, np.exp, np.log, np.sign, np.ceil, np.modf, np.isnan, np.cos):\n", + " print \"\\n\", func.__name__\n", + " print func(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Binary ufuncs\n", + "There are also many binary ufuncs, that apply elementwise on two `ndarray`s. Broadcasting rules are applied if the arrays do not have the same shape:" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a = np.array([1, -2, 3, 4])\n", + "b = np.array([2, 8, -1, 7])\n", + "print np.add(a, b) # equivalent to a + b" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print np.greater(a, b) # equivalent to a > b" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print np.maximum(a, b)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print np.copysign(a, b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Array indexing\n", + "### One-dimensional arrays\n", + "One-dimensional NumPy arrays can be accessed more or less like regular python arrays:" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a = np.array([1, 5, 3, 19, 13, 7, 3])\n", + "print a[3]" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print a[2:5]" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print a[2:-1]" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print a[:2]" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print a[2::2]" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print a[::-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Of course, you can modify elements:" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a[3]=999\n", + "print a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can also modify an `ndarray` slice:" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a[2:5] = [997, 998, 999]\n", + "print a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Differences with regular python arrays\n", + "Contrary to regular python arrays, if you assign a single value to an `ndarray` slice, it is copied across the whole slice, thanks to broadcasting rules discussed above." + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a[2:5] = -1\n", + "print a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Also, you cannot grow or shrink `ndarray`s this way:" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": { + "collapsed": false, + "scrolled": false + }, + "outputs": [], + "source": [ + "try:\n", + " a[2:5] = [1,2,3,4,5,6] # too long\n", + "except ValueError, e:\n", + " print e" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You cannot delete elements either:" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "try:\n", + " del a[2:5]\n", + "except ValueError, e:\n", + " print e" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Last but not least, `ndarray` **slices are actually *views*** on the same data buffer. This means that if you create a slice and modify it, you are actually going to modify the original `ndarray` as well!" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a_slice = a[2:6]\n", + "a_slice[1] = 1000\n", + "print a # the original array was modified!" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a[3] = 2000\n", + "print a_slice # similarly, modifying the original array modifies the slice!" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you want a copy of the data, you need to use the `copy` method:" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "another_slice = a[2:6].copy()\n", + "another_slice[1] = 3000\n", + "print a # the original array is untouched" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a[3] = 4000\n", + "print another_slice # similary, modifying the original array does not affect the slice copy" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Multi-dimensional arrays\n", + "Multi-dimensional arrays can be accessed in a similar way by providing an index or slice for each axis, separated by commas:" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "b = np.arange(48).reshape(4, 12)\n", + "print b" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print b[1, 2] # row 1, col 2" ] }, { @@ -1348,14 +1429,7 @@ }, "outputs": [], "source": [ - "print a[a < 25]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Note that all matching elements are returned as a 1D array, no matter the original array's shape:" + "print b[1, :] # row 1, all columns" ] }, { @@ -1366,45 +1440,1434 @@ }, "outputs": [], "source": [ - "p = np.fromfunction(lambda row, col: row*col, (3,6))\n", - "print p" + "print b[:, 1] # all rows, column 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Caution**: note the subtle difference between these two expressions: " ] }, { "cell_type": "code", "execution_count": 76, "metadata": { - "collapsed": false + "collapsed": false, + "scrolled": true }, "outputs": [], "source": [ - "print p[p%3 == 1]" + "print b[1, :]\n", + "print b[1:2, :]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "It is possible (and quite convenient) to use boolean indexing and assignment jointly:" + "The first expression returns row 1 as a 1D array of shape `(12,)`, while the second returns that same row as a 2D array of shape `(1, 12)`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Fancy indexing\n", + "You may also specify a list of indices that you are interested in. This is referred to as *fancy indexing*." ] }, { "cell_type": "code", "execution_count": 77, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [], + "source": [ + "print b[(0,2), 2:5] # rows 0 and 2, columns 2 to 4 (5-1)" + ] + }, + { + "cell_type": "code", + "execution_count": 78, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "p[p%3 == 1] = 99\n", - "print p" + "print b[:, (-1, 2, -1)] # all rows, columns -1 (last), 2 and -1 (again, and in this order)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## To be continued..." + "If you provide multiple index arrays, you get a 1D `ndarray` containing the values of the elements at the specified coordinates." + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print b[(-1, 2, -1, 2), (5, 9, 1, 9)] # returns a 1D array with b[-1, 5], b[2, 9], b[-1, 1] and b[2, 9] (again)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Higher dimensions\n", + "Everything works just as well with higher dimensional arrays, but it's useful to look at a few examples:" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "c = b.reshape(4,2,6)\n", + "print c" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print c[2, 1, 4] # matrix 2, row 1, col 4" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print c[2, :, 3] # matrix 2, all rows, col 3" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you omit coordinates for some axes, then all elements in these axes are returned:" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print c[2, 1] # Return matrix 2, row 1, all columns. This is equivalent to c[2, 1, :]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Ellipsis (`...`)\n", + "You may also write an ellipsis (`...`) to ask that all non-specified axes be entirely included." + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print c[2, ...] # matrix 2, all rows, all columns. This is equivalent to c[2, :, :]" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print c[2, 1, ...] # matrix 2, row 1, all columns. This is equivalent to c[2, 1, :]" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print c[2, ..., 3] # matrix 2, all rows, column 3. This is equivalent to c[2, :, 3]" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "metadata": { + "collapsed": false, + "scrolled": false + }, + "outputs": [], + "source": [ + "print c[..., 3] # all matrices, all rows, column 3. This is equivalent to c[:, :, 3]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Boolean indexing\n", + "You can also provide an `ndarray` of boolean values on one axis to specify the indices that you want to access." + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "b = np.arange(48).reshape(4, 12)\n", + "print b" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "rows_on = np.array([True, False, True, False])\n", + "print b[rows_on, :] # Rows 0 and 2, all columns. Equivalent to b[(0, 2), :]" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "cols_on = np.array([False, True, False] * 4)\n", + "print b[:, cols_on] # All rows, columns 1, 4, 7 and 10" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `np.ix_`\n", + "You cannot use boolean indexing this way on multiple axes, but you can work around this by using the `ix_` function:" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print b[np.ix_(rows_on, cols_on)]" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print np.ix_(rows_on, cols_on)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you use a boolean array that has the same shape as the `ndarray`, then you get in return a 1D array containing all the values that have `True` at their coordinate. This is generally used along with conditional operators:" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print b[b % 3 == 1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Iterating\n", + "Iterating over `ndarray`s is very similar to iterating over regular python arrays. Note that iterating over multidimensional arrays is done with respect to the first axis." + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "c = np.arange(24).reshape(2, 3, 4) # A 3D array (composed of two 3x4 matrices)\n", + "print c" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "for m in c:\n", + " print \"Item:\"\n", + " print m" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "for i in range(len(c)): # Note that len(c) == c.shape[0]\n", + " print \"Item:\"\n", + " print c[i]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you want to iterate on *all* elements in the `ndarray`, simply iterate over the `flat` attribute:" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "for i in c.flat:\n", + " print \"Item:\", i" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Stacking arrays\n", + "It is often useful to stack together different arrays. NumPy offers several functions to do just that. Let's start by creating a few arrays." + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "q1 = np.full((3,4), 1.0)\n", + "q2 = np.full((4,4), 2.0)\n", + "q3 = np.full((3,4), 3.0)\n", + "print \"q1\"\n", + "print q1\n", + "print \"q2\"\n", + "print q2\n", + "print \"q3\"\n", + "print q3" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `vstack`\n", + "Now let's stack them vertically using `vstack`:" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "q4 = np.vstack((q1, q2, q3))\n", + "print q4\n", + "print \"shape =\", q4.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This was possible because q1, q2 and q3 all have the same shape (except for the vertical axis, but that's ok since we are stacking on that axis).\n", + "\n", + "### `hstack`\n", + "We can also stack arrays horizontally using `hstack`:" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "q5 = np.hstack((q1, q3))\n", + "print q5\n", + "print \"shape =\", q5.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is possible because q1 and q3 both have 3 rows. But since q2 has 4 rows, it cannot be stacked horizontally with q1 and q3:" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "try:\n", + " q5 = np.hstack((q1, q2, q3))\n", + "except ValueError, e:\n", + " print e" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `concatenate`\n", + "The `concatenate` function stacks arrays along any given existing axis." + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "q7 = np.concatenate((q1, q2, q3), axis=0) # Equivalent to vstack\n", + "print q7\n", + "print \"shape =\", q7.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As you might guess, `hstack` is equivalent to calling `concatenate` with `axis=1`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `stack`\n", + "The `stack` function stacks arrays along a new axis. All arrays have to have the same shape." + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "q8 = np.stack((q1, q3))\n", + "print q8\n", + "print \"shape =\", q8.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Splitting arrays\n", + "Splitting is the opposite of stacking. For example, let's use the `vsplit` function to split a matrix vertically.\n", + "\n", + "First let's create a 6x4 matrix:" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "r = np.arange(24).reshape(6,4)\n", + "print r" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's split it in three equal parts, vertically:" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "r1, r2, r3 = np.vsplit(r, 3)\n", + "print \"r1\"\n", + "print r1\n", + "print \"r2\"\n", + "print r2\n", + "print \"r3\"\n", + "print r3" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "There is also a `split` function which splits an array along any given axis. Calling `vsplit` is equivalent to calling `split` with `axis=0`. There is also an `hsplit` function, equivalent to calling `split` with `axis=1`:" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "r4, r5 = np.hsplit(r, 2)\n", + "print \"r4\"\n", + "print r4\n", + "print \"r5\"\n", + "print r5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Transposing arrays\n", + "The `transpose` method creates a new view on an `ndarray`'s data, with axes permuted in the given order.\n", + "\n", + "For example, let's create a 3D array:" + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "t = np.arange(24).reshape(4,2,3)\n", + "print t" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's create an `ndarray` such that the axes `0, 1, 2` (depth, height, width) are re-ordered to `1, 2, 0` (depth→width, height→depth, width→height):" + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "t1 = t.transpose((1,2,0))\n", + "print t1\n", + "print \"shape =\", t1.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "By default, `transpose` reverses the order of the dimensions:" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "t2 = t.transpose() # equivalent to t.transpose((2, 1, 0))\n", + "print t2\n", + "print \"shape =\", t2.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "NumPy provides a convenience function `swapaxes` to swap two axes. For example, let's create a new view of `t` with depth and height swapped:" + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "t3 = t.swapaxes(0,1) # equivalent to t.transpose((1, 0, 2))\n", + "print t3\n", + "print \"shape =\", t3.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Linear algebra\n", + "NumPy 2D arrays can be used to represent matrices efficiently in python. Let's go through some of the main matrix operations available.\n", + "\n", + "### Matrix transpose\n", + "The `T` attribute is equivalent to calling `transpose()` when the rank is ≥2:" + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "m1 = np.arange(10).reshape(2,5)\n", + "print m1" + ] + }, + { + "cell_type": "code", + "execution_count": 112, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print m1.T" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The `T` attribute has no effect on rank 0 (empty) or rank 1 arrays:" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [], + "source": [ + "m2 = np.arange(5)\n", + "print m2" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [], + "source": [ + "print m2.T" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can get the desired transposition by first reshaping the 1D array to a single-row matrix (2D):" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "m2r = m2.reshape(1,5)\n", + "print m2r" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print m2r.T" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Matrix dot product\n", + "Let's create two matrices and execute a matrix dot product using the `dot` method." + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "n1 = np.arange(10).reshape(2, 5)\n", + "print n1" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "n2 = np.arange(15).reshape(5,3)\n", + "print n2" + ] + }, + { + "cell_type": "code", + "execution_count": 119, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print n1.dot(n2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Caution**: as mentionned previously, `n1*n2` is *not* a dot product, it is an elementwise product." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Matrix inverse and pseudo-inverse\n", + "Many of the linear algebra functions are available in the `numpy.linalg` module, in particular the `inv` function to compute a square matrix's inverse:" + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "import numpy.linalg as linalg\n", + "\n", + "m3 = np.array([[1,2,3],[5,7,11],[21,29,31]])\n", + "print m3" + ] + }, + { + "cell_type": "code", + "execution_count": 121, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print linalg.inv(m3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can also compute the [pseudoinverse](https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_pseudoinverse) using `pinv`:" + ] + }, + { + "cell_type": "code", + "execution_count": 122, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print linalg.pinv(m3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Identity matrix\n", + "The product of a matrix by its inverse returns the identiy matrix (with small floating point errors):" + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print m3.dot(linalg.inv(m3))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can create an identity matrix of size NxN by calling `eye`:" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print np.eye(3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### QR decomposition\n", + "The `qr` function computes the [QR decomposition](https://en.wikipedia.org/wiki/QR_decomposition) of a matrix:" + ] + }, + { + "cell_type": "code", + "execution_count": 125, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "q, r = linalg.qr(m3)\n", + "print \"q\"\n", + "print q\n", + "print \"r\"\n", + "print r\n", + "print \"q.r (equal to m3)\"\n", + "print q.dot(r)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Determinant\n", + "The `det` function computes the [matrix determinant](https://en.wikipedia.org/wiki/Determinant):" + ] + }, + { + "cell_type": "code", + "execution_count": 126, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print linalg.det(m3) # Computes the matrix determinant" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Eigenvalues and eigenvectors\n", + "The `eig` function computes the [eigenvalues and eigenvectors](https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors) of a square matrix:" + ] + }, + { + "cell_type": "code", + "execution_count": 127, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "eigenvalues, eigenvectors = linalg.eig(m3)\n", + "print \"Eigen values (λ)\"\n", + "print eigenvalues\n", + "print \"Eigen vectors (v)\"\n", + "print eigenvectors\n", + "print \"m3.v - λ*v (equals 0)\"\n", + "print m3.dot(eigenvectors) - eigenvalues * eigenvectors" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Singular Value Decomposition\n", + "The `svd` function takes a matrix and returns its [singular value decomposition](https://en.wikipedia.org/wiki/Singular_value_decomposition):" + ] + }, + { + "cell_type": "code", + "execution_count": 128, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "m4 = np.array([[1,0,0,0,2], [0,0,3,0,0], [0,0,0,0,0], [0,2,0,0,0]])\n", + "print m4" + ] + }, + { + "cell_type": "code", + "execution_count": 129, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "U, S_diag, V = linalg.svd(m4)\n", + "S = np.zeros((4, 5))\n", + "S[np.diag_indices(4)] = S_diag\n", + "print \"U\"\n", + "print U\n", + "print \"Σ\"\n", + "print S\n", + "print \"V\"\n", + "print V\n", + "print \"U.Σ.V (equals m4)\"\n", + "print U.dot(S).dot(V) # equal to m4" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Diagonal and trace" + ] + }, + { + "cell_type": "code", + "execution_count": 130, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print np.diag(m3) # the values in the diagonal of m3 (top left to bottom right)" + ] + }, + { + "cell_type": "code", + "execution_count": 131, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print np.trace(m3) # equivalent to np.diag(m3).sum()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Solving a system of linear scalar equations" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The `solve` function solves a system of linear scalar equations, such as:\n", + "\n", + "* $2x + 6y = 6$\n", + "* $5x + 3y = -9$" + ] + }, + { + "cell_type": "code", + "execution_count": 132, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "coeffs = np.array([[2, 6], [5, 3]])\n", + "depvars = np.array([6, -9])\n", + "solution = linalg.solve(coeffs, depvars)\n", + "print solution" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's check the solution:" + ] + }, + { + "cell_type": "code", + "execution_count": 133, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print coeffs.dot(solution)\n", + "print depvars" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Looks good! Another way to check the solution:" + ] + }, + { + "cell_type": "code", + "execution_count": 134, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [], + "source": [ + "print np.allclose(coeffs.dot(solution), depvars)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Vectorization\n", + "Instead of executing operations on individual array items, one at a time, your code is much more efficient if you try to stick to array operations. This is called *vectorization*. This way, you can benefit from NumPy's many optimizations.\n", + "\n", + "For example, let's say we want to generate a 768x1024 array based on the formula $sin(xy/40)$. A **bad** option would be to do the math in python using nested loops:" + ] + }, + { + "cell_type": "code", + "execution_count": 135, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "import math\n", + "data = np.empty((768, 1024))\n", + "for y in xrange(768):\n", + " for x in xrange(1024):\n", + " data[y, x] = math.sin(x*y/40.5) # BAD! Very inefficient." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Sure, this works, but it's terribly inefficient since the loops are taking place in pure python. Let's vectorize this algorithm. First, we will use NumPy's `meshgrid` function which generates coordinate matrices from coordinate vectors." + ] + }, + { + "cell_type": "code", + "execution_count": 136, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "x_coords = np.arange(0, 1024) # [0, 1, 2, ..., 1023]\n", + "y_coords = np.arange(0, 768) # [0, 1, 2, ..., 767]\n", + "X, Y = np.meshgrid(x_coords, y_coords)\n", + "print \"X\"\n", + "print X\n", + "print\n", + "print \"Y\"\n", + "print Y" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As you can see, both `X` and `Y` are 768x1024 arrays, and all values in `X` correspond to the horizontal coordinate, while all values in `Y` correspond to the the vertical coordinate.\n", + "\n", + "Now we can simply compute the result using array operations:" + ] + }, + { + "cell_type": "code", + "execution_count": 137, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "data = np.sin(X*Y/40.5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we can plot this data using matplotlib's `imshow` function (see the [matplotlib tutorial](tools_matplotlib.ipynb))." + ] + }, + { + "cell_type": "code", + "execution_count": 138, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "import matplotlib.pyplot as plt\n", + "import matplotlib.cm as cm\n", + "fig = plt.figure(1, figsize=(7, 6))\n", + "plt.imshow(data, cmap=cm.hot, interpolation=\"bicubic\")\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Saving and loading\n", + "NumPy makes it easy to save and load `ndarray`s in binary or text format.\n", + "\n", + "### Binary `.npy` format\n", + "Let's create a random array and save it." + ] + }, + { + "cell_type": "code", + "execution_count": 139, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [], + "source": [ + "a = np.random.rand(2,3)\n", + "print a" + ] + }, + { + "cell_type": "code", + "execution_count": 140, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "np.save(\"my_array\", a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Done! Since the file name contains no file extension was provided, NumPy automatically added `.npy`. Let's take a peek at the file content:" + ] + }, + { + "cell_type": "code", + "execution_count": 141, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "with open(\"my_array.npy\", \"rb\") as f:\n", + " content = f.read()\n", + " print repr(content)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To load this file into a NumPy array, simply call `load`:" + ] + }, + { + "cell_type": "code", + "execution_count": 142, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a_loaded = np.load(\"my_array.npy\")\n", + "print a_loaded" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Text format\n", + "Let's try saving the array in text format:" + ] + }, + { + "cell_type": "code", + "execution_count": 143, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "np.savetxt(\"my_array.csv\", a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's look at the file content:" + ] + }, + { + "cell_type": "code", + "execution_count": 144, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "with open(\"my_array.csv\", \"rt\") as f:\n", + " content = f.read()\n", + " print content" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is a CSV file with tabs as delimiters. You can set a different delimiter:" + ] + }, + { + "cell_type": "code", + "execution_count": 145, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "np.savetxt(\"my_array.csv\", a, delimiter=\",\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To load this file, just use `loadtxt`:" + ] + }, + { + "cell_type": "code", + "execution_count": 146, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "a_loaded = np.loadtxt(\"my_array.csv\", delimiter=\",\")\n", + "print a_loaded" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Zipped `.npz` format\n", + "It is also possible to save multiple arrays in one zipped file:" + ] + }, + { + "cell_type": "code", + "execution_count": 147, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "b = np.arange(24, dtype=np.uint8).reshape(2, 3, 4)\n", + "print b" + ] + }, + { + "cell_type": "code", + "execution_count": 148, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "np.savez(\"my_arrays\", my_a=a, my_b=b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Again, let's take a peek at the file content. Note that the `.npz` file extension was automatically added." + ] + }, + { + "cell_type": "code", + "execution_count": 149, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "with open(\"my_arrays.npz\", \"rb\") as f:\n", + " content = f.read()\n", + " print repr(content)[:200], \"[...]\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You then load this file like so:" + ] + }, + { + "cell_type": "code", + "execution_count": 150, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "my_arrays = np.load(\"my_arrays.npz\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is a dict-like object which loads the arrays lazily:" + ] + }, + { + "cell_type": "code", + "execution_count": 151, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print my_arrays.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 152, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print my_arrays[\"my_a\"]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## What next?\n", + "Now you know all the fundamentals of NumPy, but there are many more options available. The best way to learn more is to experiment with NumPy, and go through the excellent [reference documentation](http://docs.scipy.org/doc/numpy/reference/index.html) to find more functions and features you may be interested in." ] } ],