Skip to content
Snippets Groups Projects
python-data-3-numpy.ipynb 38.1 KiB
Newer Older
ignat's avatar
ignat committed
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Notebook 3 - NumPy\n",
    "[NumPy](http://numpy.org) short for Numerical Python, has long been a cornerstone of numerical computing on Python. It provides the data structures, algorithms and the glue needed for most scientific applications involving numerical data in Python. All computation is done in vectorised form - using vectors of several values at once instead of singular values at a time. NumPy contains, among other thigs:\n",
ignat's avatar
ignat committed
    "* A fast and efficient multidimensional array object `ndarray`.\n",
    "* Mathematical functions for performing element-wise computations with arrays or mathematical operations between arrays.\n",
    "* Tools for reading and manipulating large array data to disk and working with memory-mapped files.\n",
    "* Linear algebra, random number generation and Fourier transform capabilities.\n",
    "\n",
    "For the rest of the course, whenever array is mentioned it refers to the NumPy ndarray.\n",
    "<br>\n",
    "\n",
    "## Table of contents\n",
    "- [The ndarray](#ndarray)\n",
    "    - [Creating arrays](#creating)\n",
    "    - [Data Types](#data)\n",
    "    - [Arithmetic Operations](#arithmetic)\n",
    "    - [Indexing and Slicing](#indexing)\n",
    "    - [Transposing and Swapping Axis](#transposing)\n",
ignat's avatar
ignat committed
    "- [Universal Functinos](#universal)\n",
    "- [Other useful operations](#other)\n",
    "- [File IO](#file)\n",
    "- [Liear algebra](#linear)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Why NumPy?\n",
    "Is the first question that anybody asks when they find out about it. \n",
    "\n",
    "Some people might say: *I don't care about speed, I want to spend my time researching how to cure cancer, not optimise coputer code!*\n",
    "\n",
    "That's perfectly reasonable, but are you willing to wait a lot longer for your experiment to finish? I definitely don't want to do that. Let's see how much faster NumPy really is!\n",
    "\n",
    "to show that we'll be using the magic command `%timeit` which you can read more about [here](https://ipython.readthedocs.io/en/stable/interactive/magics.html) and don't worry about the details now, they will clear up later.\n",
    "\n",
    "Let's have a look at generating a vector of 10M random values and then summing them all up using the Python way and using the NumPy way!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "x = np.random.randn(10000000) # generate random numbers\n",
    "\n",
    "print(\"Running normal python sum()\")\n",
    "%timeit sum(x)\n",
    "\n",
    "print(\"Running numpy sum()\")\n",
    "%timeit np.sum(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**WOW** that was a difference of more than a **100 times** and that was just for a single summing operation. Imagine if you had several of those running all the time!\n",
    "\n",
    "Are you onboard with Numpy then? Let's proceed..."
ignat's avatar
ignat committed
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The ndarray <a name=\"ndarray\"></a>\n",
    "The ndarray is a backbone on Numpy. It's a fast and flexible container for N-dimensional array objects, usually used for large datasets in Python. Arrays enable you to perform mathematical operations on whole blocks of data using similar syntax to the equivalent operations between scalar elements.\n",
ignat's avatar
ignat committed
    "\n",
    "Here is a quick example of its capabilities:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
ignat's avatar
ignat committed
   "metadata": {},
ignat's avatar
ignat committed
   "source": [
    "import numpy as np\n",
    "\n",
    "# create a 2x3 array of random values\n",
    "data = np.random.randn(2,3)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
ignat's avatar
ignat committed
   "metadata": {},
ignat's avatar
ignat committed
   "source": [
    "data * 10 #multiply all numbers by 10"
ignat's avatar
ignat committed
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
ignat's avatar
ignat committed
   "metadata": {},
ignat's avatar
ignat committed
   "source": [
    "data + data #element-wise addition"
ignat's avatar
ignat committed
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Every array has a shape, a tuple indicating the size of each dimnesion and a dtype. You can obtain these via the respective methods:"
ignat's avatar
ignat committed
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
ignat's avatar
ignat committed
   "metadata": {},
ignat's avatar
ignat committed
   "source": [
    "# number of dimensions of the array\n",
    "data.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
ignat's avatar
ignat committed
   "metadata": {},
ignat's avatar
ignat committed
   "source": [
    "# the size of the array\n",
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
ignat's avatar
ignat committed
   "metadata": {},
ignat's avatar
ignat committed
   "source": [
    "# the type of values store in the array\n",
    "data.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating arrays <a name=\"creating\"></a>\n",
    "The easiest and quickest way to create an array is from a normal Python list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = [1.2, 5.2, 5, 7.8, 0.3]\n",
    "arr = np.array(data)\n",
    "\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is also possible to create multidimensional arrays in a similar fashion. An example would be:\n",
    "```python\n",
    "data = [[1.2, 5.2, 5, 7.8, 0.3],\n",
    "        [4.1, 7.2, 4.8, 0.1, 7.7]]\n",
    "```\n",
    "Try creating an multidimensional array below and verify its number of dimensions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ident = [[1, 0], [0, 1]]\n",
    "idarray = np.array(ident)\n",
    "\n",
    "idarray.ndim"
   ]
ignat's avatar
ignat committed
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also create an array filled with zeros"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.zeros(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Again, it is also possible to create a multidimensional array by passing a tuple as an argument"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.zeros((4,6))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Another option is to use the `empty()` function which creates an array filled with garbage values. It is used in a similar way to `zeros()`.\n",
    "\n",
    "Try using it below and see what it creates!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.empty((10, 10))"
   ]
ignat's avatar
ignat committed
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "NumPy also has an equivalent to the built-in Python function `range()`"
ignat's avatar
ignat committed
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.asarray([1, 1, 1])"
ignat's avatar
ignat committed
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here are most of the possible ways of creating an array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| Function | Description |\n",
    "|----|:--|\n",
    "| array  | Convert input data to an ndarray either by inferring a dtype<br>or explicitly specifying a dtype; copies the input data by default. |\n",
    "| asarray | Convert input to ndarray, but do not copy if the input is already an ndarray. |\n",
    "| arange | Similar to the built-in `range` function but returns an ndarray. |\n",
    "| ones | Produces an array of all 1s with the given shape and dtype. |\n",
    "| ones_like | Similar to `ones` but takes another array and produces a ones array<br>of the same shape and dtype |\n",
    "| zeros, zeros_like | Similar to `ones` but produces an array of 0s. |\n",
    "| empty, emtpy_like | Create new array by allocating memory but without populating any values. |\n",
    "| full, full_like | Produce an array of a given shape and dtype with all values set to the indicated \"fill value\". |\n",
    "| eye | Create a square NxN identity matrix (1s on the diagonal and 0s elsewhere). |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Data Types <a name=\"data\"></a>\n",
    "The data type or `dtype` is a special object containing the information the array needs to interpret a chunk of memory. We can specify it during the creation of an array "
ignat's avatar
ignat committed
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array([1, 2, 3], dtype=np.float64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "dtypes are a source of NumPy's flexibility. Here is a table of all of them.\n",
    "There is no need to remember all of dtypes.\n",
    "\n",
    "| Type | Type code | Description |\n",
    "|----|---|---|\n",
    "| int8, uint8 | i1, u1 | Signed and unsigned 8bit integer |\n",
    "| int16, uint16 | i2, u2 | Signed and unsigned 16bit integer |\n",
    "| int32, uint32 | i4, u4 | Signed and unsigned 32bit integer |\n",
    "| int64, uint64 | i8, u8 | Signed and unsigned 64bit integer |\n",
    "| float16 | f2 | Half-precision floating point |\n",
    "| float32 | f4 or f | Standard single-precision floating point |\n",
    "| float64 | f8 or d | Standard double-precision floating point |\n",
    "| float128 | f16 or g | Extended-precision floating point |\n",
    "| complex64<br>complex128<br>complex256 | c8, c16, c32 | Complex numbers represented by two 32, 64 or 128 floats, respectively |\n",
    "| bool | ? | Boolean type storing True or False |\n",
    "| object | O | Python object type, a value can be any Python object |\n",
    "| string_ | S | Fixed-length ASCII string type.<br>For example use `S10` to create a string dtype with length 10 |\n",
    "| unicode_ | U | Fixed-length Unicode type |\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Similar to normal Python, you can cast(convert) an array from one dtype to another using the `astype` method:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array([1, 2, 3])\n",
    "arr.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "float_arr = arr.astype(np.float64)\n",
    "float_arr.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The normal limitations to casting apply here as well. You can try creating a `float64` array and then converting it to an `int64` array below "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 1\n",
    "Create a 5x5 [identity matrix](https://en.wikipedia.org/wiki/Identity_matrix). Then convert it to float64 dtype. At the end confirm its properties using the appropriate attributes."
ignat's avatar
ignat committed
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Arithmetic operations <a name=\"arithmetic\"></a>\n",
    "You have already gotten a taste of this in the examples above but let's try to extend that.\n",
    "\n",
    "Arrays are important because they enable you to express batch operations on data without having to write for loops - this is called **vectorisation**.\n",
    "\n",
    "Any arithmetic operations between equal-size arrays applies the operation element-wise:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A * A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A - A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Arithmetic operations with scalars propogate the scalar argument to each element in the array:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A * 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A ** 0.5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Comparisons between arrays of the same size yield boolean arrays:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "B = np.array([[1, 7, 4],[4, 12, 2]])\n",
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A > B"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Arithmetic operations with differently sized arrays is called **broadcasting** but will not be covered in this course due to the limited time."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 2\n",
    "Generate a vector of size 10 with values ranging from 0 to 1, both included."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Indexing and slicing <a name=\"indexing\"></a>\n",
    "NumPy offers many options for indexing and slicing. Coincidentally, they are very similar to Python.\n",
    "\n",
    "Let's see how this is done in 1D:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.arange(10)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A[5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A[5:8]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A[5:8] = 0\n",
    "A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Important:** Unlike vanilla Python, NumPy array slices are views on the original array. This means that the data is not copied, and any modifications to the view will be reflected in the source array."
ignat's avatar
ignat committed
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A_slice = A[5:8]\n",
    "A_slice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A_slice[:] = [12, 17, 24]\n",
    "A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we used the [:] operator which assings to all values in the array.\n",
    "Let's now have a look at higher dimensional arrays:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "C = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "C"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that we have 2 dimensions, we need to input 2 indices to get a specific element of the array. Alternatively, if we input only one index, then we obtain the whole row of the array:"
ignat's avatar
ignat committed
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "C[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "C[2][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "C[2, 1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here is a picture to better explain indexing in 2D:\n",
    "<img src=\"img/ndarray.png\" alt=\"drawing\" width=\"300\"/>"
ignat's avatar
ignat committed
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The same concepts and techniques are extended into multidimensional arrays:\n",
    "if you omit later indices, the returned object will be a lower dimensional ndarray consisting of all data along the higher dimensions."
ignat's avatar
ignat committed
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's look into **slicing**. You already saw above that slicing in 1D is done the same way as in standard Python data structures. So how do we do that in 2D? Well, it is fairly intuitive:"
ignat's avatar
ignat committed
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "C = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "C[:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This can be read as *select the first 2 rows of C*"
ignat's avatar
ignat committed
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "C[1, :2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "C[:, :1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here is some visual aid for what happened above:\n",
    "<img src=\"img/indexing.png\" alt=\"drawing\" width=\"400\"/>"
ignat's avatar
ignat committed
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is also possible to index arrays via booleans.\n",
    "\n",
    "Say we have an 1D array of 0s and 1s and then a 2D array of randomly generated data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits = np.array([\"banana\", \"orange\", \"mango\", \"banana\", \"tomato\", \"passionfruit\", \"cherry\"])\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.random.randn(7,4)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits == \"banana\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data[fruits == \"banana\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Powerful right? The only caveat is that the boolean array must be of the same length as the array axis it's indexing. Caution must be taken here as the boolean selection will not fail even if the boolean array is not the correct length!\n",
    "\n",
    "You can also mix and match boolean arrays but there is one small difference compared to Python - the typical boolean operators (`and` and `or`) do not work instead you must use `&`(and) and `|`(or)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = (fruits == \"banana\") | (fruits == \"cherry\")\n",
    "data[mask]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 3\n",
    "Create a 5x5 matrix of random values. Square all positive values of the matrix and set all else to 0. Attempt to do this in place - ie. without copying the matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 4\n",
    "Create a 2D array with 1s on the border and 0s inside"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Transposing Arrays and Swapping Axes <a name=\"transposing\"></a>\n",
    "We can use the method `reshape()` to convert the data from one shape into another. Later we can use the `T` attribute to obtain the transpose of the array."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.arange(15)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "B = A.reshape((3,5))\n",
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "B.T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also reshape 3D arrays but how would `T` work then? Luckily, we can use the `tranpose()` method which allows us to chose the axes we want to swap:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.arange(16)\n",
    "C = A.reshape((2, 2, 4))\n",
ignat's avatar
ignat committed
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "C.transpose((1, 0, 2))\n",
    "C.shape"
ignat's avatar
ignat committed
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotting\n",
    "You can easily plot and show images in Python via the package `matplotlib` which can be used to plot an array. \n",
    "\n",
    "First we have to set up our environment. Read and run the code below to do just that:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "# Import NumPy and matplotlib\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Set a greyscale colourmap (we want white for 0 and black for 1)\n",
    "plt.set_cmap('Greys')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can create an array of values and plot in a canvas. The easiest way to do this is to pick values between 0 and 1 and plot grayscale images where 1 corresponds to black and 0 corresponds to white. Let's see how we can do this by creating an array of 0s:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "canvas = np.zeros((100,50))\n",
    "plt.imshow(canvas, interpolation=\"none\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "All is white right? let's add some black to it!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "canvas[50, 25] = 1\n",
    "plt.imshow(canvas, interpolation=\"none\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
ignat's avatar
ignat committed
    "Use the canvas template above and create an image where the top right and bottom left pixels are set to black.\n",
    "\n",
    "*Note: Remember to first reset your canvas to only 0s*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
ignat's avatar
ignat committed
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
ignat's avatar
ignat committed
    "Draw a horizontal and verticle line across the canvas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
ignat's avatar
ignat committed
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
ignat's avatar
ignat committed
    "Make the top left corner of the image black.\n",
    "\n",
    "*Extra challenge: do this wihtout using numbers for indexing*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
ignat's avatar
ignat committed
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Universal Functions <a name=\"universal\"></a>\n",
    "or *ufunc* are functions that perform element-wise operations on data in ndarrays. You can think of them as fast vectorised wrappers for simply functions. Here is an example of `sqrt` and `exp`:"
ignat's avatar
ignat committed
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.arange(10)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.sqrt(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},