{ "metadata": { "name": "", "signature": "sha256:6238f74cf5528acda37ec74b5da2a24206a7dcd8cd9baf35a0d806b9fb894adb" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to day 4" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Compatibility with Python 3\n", "from __future__ import print_function # print('me') instead of print 'me'\n", "from __future__ import division # 1/2 == 0.5, not 0" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Show figures inside the notebook\n", "%matplotlib inline" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np # the Python array package\n", "import matplotlib.pyplot as plt # the Python plotting package" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Slicing with boolean vectors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have already seen how to slice arrays using colons and integers.\n", "\n", "The colon means 'all the elements on this axis':" ] }, { "cell_type": "code", "collapsed": false, "input": [ "an_array = np.array([[0, 1, 2, 3], [4, 5, 6, 7]])\n", "an_array" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "an_array[:, 1] # All rows, only the second column" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "an_array[0, 1:] # Only the first row, all columns except the first" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have also seen how to slice using a boolean array the same shape as the original:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "is_gt_5 = an_array > 5\n", "is_gt_5" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "an_array[is_gt_5] # Select elements greater than 5 into 1D array" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also use boolean vectors to select elements on a particular axis. So, for example, let's say we want the first and last elements on the second axis. We can use a boolean vector to select these elements from a particular axis, while still using integer and colon syntax for the other axes:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "want_first_last = np.array([True, False, False, True])\n", "an_array[:, want_first_last] # All rows, columns as identified by boolean vector" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More fancy plotting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So far we have done very simple line-plots with the defaults.\n", "\n", "Let's say we are plotting some points with matplotlib. That might look like this:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Line markers" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = np.arange(0, np.pi * 2, 0.2) # The x axis values for the plot\n", "y = np.sin(x) # The y axis values\n", "plt.plot(x, y)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default matplotlib will join up the neighboring points with lines. We can also ask matplotlib to show the individual points. This is useful for scatterplots.\n", "\n", "To do this, can you specify a 'marker'. For example, let's say you wanted to use a '+' marker:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.plot(x, y, '+')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Or a circle marker\n", "plt.plot(x, y, 'o')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Subplots" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is often helpful to see images or plots side-by-side rather than in separate windows (or cells in the IPython notebook).\n", "\n", "Matplotlib uses the ``subplots`` command for this.\n", "\n", "To make subplots, you use the `subplots` command to specify your grid of plots (number of rows in your grid, number of columns). Each element of the grid has its own \"axis\" object. You can use the axis object methods to do plots and image displays on the individual axes." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Here I make a 1 row, 2 column grid\n", "fig, axes = plt.subplots(1, 2)\n", "# Axes are the individual plots, with their own - axes\n", "# matplotlib returned 2 axes for us.\n", "print('Shape of axes is', axes.shape)\n", "# Now I can use the axes objects to do the plots\n", "axes[0].plot(x, y)\n", "axes[1].plot(x, y, 'o')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you need help, try ``plt.subplots?`` in IPython. You can also tab complete on the axis objects like this:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "axis0 = axes[0]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Now you can tab complete on the axis object with:\n", "# axis0." ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating arrays of zeros, ones" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's common to need to create new arrays filled with zeros or ones, and Numpy (like MATLAB) has special commands for these:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "np.zeros(5)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "np.zeros((2, 5))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "np.ones(3)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "np.ones((3, 4))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A final reminder about axes for array functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember that Numpy, by default, will run functions across every element of the array without regard to the shape:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "an_array" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "an_array.mean()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want Numpy to run functions across a particular axis, you need the `axis` keyword:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "np.mean(an_array, axis=0)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "np.mean(an_array, axis=1)" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }