{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Data Analysis with Jupyter Notebooks.\n", "\n", "# Tutorial 3\n", "\n", "Benjamin J. Morgan, University of Bath." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Contents\n", "\n", "- [Data Types](#data_types)\n", " - [Integers and Floats](#numbers)\n", " - [Scientific Notation](#scientific_notation)\n", " - [Strings](#strings)\n", " - [Lists](#lists)\n", "- [Numpy and arrays](#numpy)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data types\n", "\n", "So far we have talked about “data” and “results”, but what are the pieces of information that we want to manipulate? Typically numbers (or groups of numbers) or text (or lists of text). Different kinds of data can be used for different things: numbers can be combined in mathematical expressions, text can be printed, searched, or reorganised; numbers can be arranged by magnitude, names can be arranged by alphabetical order. In Python, these differences are represented by different **data types**.\n", "\n", "### Numbers: *int* and *float*\n", "\n", "We will discuss two kinds of numeric types: integers and floating point numbers. Python has other built in numeric data types, including complex numbers, which are useful in specialised cases.\n", "\n", "Whole numbers, without decimal points are integers, e.g. 1, 6, 2331. \n", "Numbers with decimal points are floating point numbers or “floats”, e.g. 1.0, 232.141, 1.3e5. \n", "That last example, 1.3e5, uses scientific notation and is shorthand for 130000.0." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Scientific Notation\n", "Very large and very small numbers can be written using **scientific notation**. For example, instead of 0.0000241, we would normally write 2.41×10-5. In Python this would be written `2.41e-5` or `2.41e-05`.\n", "\n", ">```python\n", "2.41e-5 == 0.0000241\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Strings\n", "\n", "Strings are any sequence of text. We indicate that a sequence of text is a string, and not a Python command, by enclosing it in single or double quotes. Being able to use either quote type allows strings that themselves contain quotes.\n", "\n", ">```python\n", "'this is a string using single quotes'\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">```python\n", "\"this is a string using double quotes\"\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">```python\n", "'this string has \"nested quotes\"'\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lists\n", "\n", "Python also contains built-in data types for collections of things. For data analysis we often deal with sets of numbers. These can be collected in **lists**.\n", "\n", "A list is denoted by a series separated by commas, and enclosed in square brackets:\n", "\n", ">```python\n", "my_list = [ 1, 2, 3, 4 ]\n", "mylist\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "although lists can contain any set of Python objects, even other lists:\n", "\n", ">```python\n", "my_other_list = [ 4, 1.5, 'peach' ]\n", "my_other_list\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">```python\n", "both_lists = [ my_list, my_other_list ]\n", "both_lists\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To refer to one element in a list, use the **index** of that element. Index numbering counts the number of jumps along the sequence, so starts at zero.\n", "\n", ">```python\n", "# 1st element (zero jumps along the sequence)\n", "print( my_other_list[0] )\n", "# 2nd element (one jump along the sequence)\n", "print( my_other_list[1] ) \n", "# 3rd element (two jumps along the sequence)\n", "print( my_other_list[2] ) \n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using an index outside the range of elements in the list will produce an error. For example, `my_other_list` has three elements, but `my_other_list[3]` tries to return the *4th* element (which does not exist)\n", "\n", ">```python\n", "print( my_other_list[3] )\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "You can also refer to a sequence of elements by giving a *range* as the index:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# run this cell to create the list `alphabet`\n", "alphabet = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', \n", " 'h', 'i', 'j', 'k', 'l', 'm', 'n', \n", " 'o', 'p', 'q', 'r', 's', 't', 'u',\n", " 'v', 'w', 'x', 'y', 'z' ]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">```python\n", "alphabet[3:8]\n", "```\n", "\n", "→ start from three jumps, finish at eight jumps, i.e. elements 4 to 9." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Negative numbers count backwards from the end of the sequence.\n", "\n", ">```python\n", "alphabet[-8:-3]\n", "```\n", "\n", "→ 9th from the end up to 4th from the end." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And leaving out one of the numbers in the range will include all elements up to the start or end of the sequence.\n", "\n", ">```python\n", "alphabet[14:]\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">```python\n", "alphabet[:14]\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# numpy and arrays\n", "\n", "Although lists can be very useful for handling ordered collections of things, for data manipulation we usually deal with ordered lists of only numbers. The flexibility of lists means using them is (relatively) computationally slow. This is not an issue for small data sets, but can be prohibitive for large data sets, with perhaps millions or more entries.\n", "\n", "An alternative data type, specifically designed for manipulating (large) numerical data sets is the **numpy array**. `numpy` is a module for numerical scientific computing with Python, and is conventionally imported via\n", "\n", "```python\n", "import numpy as np\n", "```\n", "\n", "This is similar to the import math we saw [above](#functions_and_modules), but uses the as keyword to make `numpy` more convenient to work with.\n", "\n", ">```python\n", "import math\n", "math.sqrt(4)\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import math" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Having imported `numpy` (as `np`) we can store lists of numbers as `numpy` arrays.\n", "\n", ">```python\n", "import numpy as np\n", "a = np.array( [ 1, 2, 3, 4 ] )\n", "a\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can think of a 1-dimensional `numpy` array as a vector, and we can use very compact code to perform *vector* mathematical operations on the entire array.\n", "\n", ">```python\n", "a + 1\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">```python\n", "a**2\n", "```\n", "\n", "Remember that `**` is the $power$ operator. This code calculates $a^2$ for every number stored in `a`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In both these cases, the mathematical operation (add one; square) is applied to every element in the array, and a new array with *all* the results is returned.\n", "\n", "If the mathematical expression contains two (or more) arrays, then an **element-by-element** operation is performed:\n", "\n", "e.g. vector addition:\n", "\n", ">```python\n", "b = np.array( [ 5, 6, 7, 8 ] )\n", "a + b\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">```python\n", "a * b\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us try to calculate the square root of all the numbers in `a`:\n", "\n", ">```python\n", "from math import sqrt\n", "sqrt(a)\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "a = np.array( [ 1, 2, 3, 4 ] )\n", "a\n", "np.sqrt(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This gives an error. \n", "\n", "Because `numpy` is not part of the standard Python library, the `sqrt` function provided by the `math` module does not know how to treat a `numpy` array of numbers. To do what we want we can use the `sqrt` function in `numpy` instead. \n", "\n", ">```python\n", "np.sqrt(a)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Edit the previous code cell to use the numpy version of sqrt instead of the standard function from the math module.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This cell tests your answers from the three previous code cells.\n", "# You do not need to edit it\n", "assert _[0] == math.sqrt(1)\n", "assert _[1] == math.sqrt(2)\n", "assert _[2] == math.sqrt(3)\n", "assert _[3] == math.sqrt(4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`numpy` contains a great many functions for performing mathematical operations on arrays of numbers, which are all listed on the [`numpy` website](https://docs.scipy.org/doc/numpy/reference/routines.math.html)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To limit the number of decimal places in our result we can use `round()`:\n", "\n", ">```python\n", "np.round( np.sqrt(a), 2 ) # round the result to 2 decimal places\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that here the first argument is `np.sqrt(a)`, which is itself a `numpy` function. This is analogous to a function of a function in mathematics: $f(g(x))$. Nesting functions like this helps write compact code without storing the intermediate results. Nesting several functions can make your code confusing to read, however, and your primary goal should be to write clear understandable code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Generating sequences of numbers\n", "\n", "Often, we will want to use `numpy` arrays to store experimental data. Other times we might just want a list of number, e.g. from 1 to 20. We could write these out to create the array:\n", "\n", "```python\n", "one_to_twenty = np.array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ] )\n", "```\n", "\n", "To save typing (and make your code easier to read) `numpy` contains a function for creating lists of numbers:\n", "\n", ">```python\n", "n = np.arange(1,21)\n", "n\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that `arange` gives us numbers starting from 1, up to, but not including, 21. \n", "\n", "We can generate lists of numbers with different spacings by providing a step-size (which has a default value of 1)\n", "\n", ">```python\n", "m = np.arange(2,21,2)\n", "m\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another way to generate an evenly spaced list of number is to use [`linspace()`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html).\n", "\n", ">```python\n", "p = np.linspace(0,10,50)\n", "p\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`linspace()` takes three arguments: the starting number, the end number, and the total number of values in the sequence. \n", "\n", "`linspace()` is particularly useful for generating evenly spaced points that are not integers." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }