{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Let printing work the same in Python 2 and 3\n", "from __future__ import print_function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# NumPy\n", "The NumPy package provides the \"ndarray\" object. The NumPy array is used to contain data of uniform type with an arbitrary number of dimensions. NumPy then provides basic mathematical and array methods to lay down the foundation for the entire SciPy ecosystem. The following import statement is the generally accepted convention for NumPy." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Array Creation\n", "There are several ways to make NumPy arrays. An array has three particular attributes that can be queried: shape, size and the number of dimensions." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = np.array([1, 2, 3])\n", "print(a.shape)\n", "print(a.size)\n", "print(a.ndim)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.arange(100)\n", "print(x.shape)\n", "print(x.size)\n", "print(x.ndim)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "y = np.random.rand(5, 80)\n", "print(y.shape)\n", "print(y.size)\n", "print(y.ndim)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Array Manipulation\n", "How to change the shape of an array without a copy!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x.shape = (20, 5)\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "NumPy can even automatically figure out the size of at most one dimension for you." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "y.shape = (4, 20, -1)\n", "print(y.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Array Indexing" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Scalar Indexing\n", "print(x[2])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Slicing\n", "print(x[2:5])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Advanced slicing\n", "print(\"First 5 rows\\n\", x[:5])\n", "print(\"Row 18 to the end\\n\", x[18:])\n", "print(\"Last 5 rows\\n\", x[-5:])\n", "print(\"Reverse the rows\\n\", x[::-1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Boolean Indexing\n", "print(x[(x % 2) == 0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Fancy Indexing -- Note the use of a list, not tuple!\n", "print(x[[1, 3, 8, 9, 2]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Broadcasting\n", "Broadcasting is a very useful feature of NumPy that will let arrays with differing shapes still be used together. In most cases, broadcasting is faster, and it is more memory efficient than the equivalent full array operation." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "print(\"Shape of X:\", x.shape)\n", "print(\"Shape of Y:\", y.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, here are three identical assignments. The first one takes full advantage of broadcasting by allowing NumPy to automatically add a new dimension to the *left*. The second explicitly adds that dimension with the special NumPy alias \"np.newaxis\". These first two creates a singleton dimension without any new arrays being created. That singleton dimension is then implicitly tiled, much like the third example to match with the RHS of the addition operator. However, unlike the third example, the broadcasting merely re-uses the existing data in memory." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = x + y\n", "print(a.shape)\n", "b = x[np.newaxis, :, :] + y\n", "print(b.shape)\n", "c = np.tile(x, (4, 1, 1)) + y\n", "print(c.shape)\n", "print(\"Are a and b identical?\", np.all(a == b))\n", "print(\"Are a and c identical?\", np.all(a == c))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another example of broadcasting two 1-D arrays to make a 2-D array." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.arange(-5, 5, 0.1)\n", "y = np.arange(-8, 8, 0.25)\n", "print(x.shape, y.shape)\n", "z = x[np.newaxis, :] * y[:, np.newaxis]\n", "print(z.shape)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# More concisely\n", "y, x = np.ogrid[-8:8:0.25, -5:5:0.1]\n", "print(x.shape, y.shape)\n", "z = x * y\n", "print(z.shape)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }