{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Data Analysis using Jupyter Notebooks Part 2\n", "\n", "Benjamin J. Morgan\n", "\n", "# Tutorial 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2D numpy arrays\n", "\n", "In all the examples using `numpy` arrays that you have seen so far, these arrays have been **one dimensional**.\n", "\n", "It can often be useful to collect different data together in a table. \n", "One way to do this is by combining one-dimensional `numpy` arrays into larger, **two-dimensional**, arrays.\n", "\n", ">```python\n", "import numpy as np\n", "# create an array `x` with the integers 1 to 5\n", "x = np.arange(1,6)\n", "# create three new arrays by performing calculations on `x`\n", "y1 = x**2\n", "y2 = x+3\n", "y3 = x/2 + 1\n", "print('x=',x)\n", "print('y1=',y1)\n", "print('y2=',y2)\n", "print('y3=',y3)\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can combine numpy arrays into a table as **columns** using `np.column_stack()`\n", "\n", ">```python\n", "# combine x, y1, y2, and y3 as columns in a new table\n", "column_table = np.column_stack( ( x, y1, y2, y3 ) )\n", "print( column_table )\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or as **rows** using np.row_stack()\n", "\n", ">```python\n", "# arrange x, y1, y2, and y3 as rows in a new table\n", "row_table = np.row_stack( ( x, y1, y2, y3 ) )\n", "print( row_table )\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember that a 1D `numpy` array can be indexed like a list.\n", ">```python\n", "my_1D_array = np.array( [ 1, 2, 3, 4, 5, 6] )\n", "my_1D_array[2:5] \n", "# [2:5] selects from 2 jumps, up to, but not including, 5 jumps\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A 2D `numpy` array can be treated like a list of lists, and indexing returns selected rows.\n", ">```python\n", "row_table[1] # return the 2nd row (1 jump from the start)\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because each row is a 1D `numpy` array, you can use a second index to select a single entry.\n", ">```python\n", "row_table[1][3]\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These two indices can be combined into a single bracket, where the first value selects one of more rows, and the second value selects one or more columns.\n", ">```python\n", "row_table[1,3]\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To select a single row, we make use of the range character `:`. Remember, for a list or 1D array, `:` lets us select a range of elements, and leaving out one of the numbers selects all elements up to the start, or end, of the list.\n", "\n", ">```python\n", "my_list = [ 'a', 'b', 'c', 'd', 'e' ]\n", "my_list[1:]\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Leaving out *both* numbers extends our selection up to both ends of the list or array.\n", ">```python\n", "my_list[:]\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For a 2D array, you can think of this as “every row” or “every column”.\n", "\n", ">```python\n", "print( row_table )\n", "print()\n", "print( row_table[:,3] ) # all rows, jump 3 columns\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Use a combination of row and column indexing to select [ 6., 7., 8.] from row_table\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also convert between 1D and 2D numpy arrays (or even higher dimensions) using `np.reshape()`\n", "\n", ">```python\n", "a = ( [ 1, 2, 3, 4, 5, 6 ] )\n", "print( a )\n", "b = np.reshape( a, (3, 2) ) # reshape into a 3×2 2D array\n", "print( b )\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and back again\n", "\n", ">```python\n", "c = np.reshape( b, (6) )\n", "print( c )\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Create a numpy 1D array containing the numbers 1–100.
\n", "Reshape this 1D array into a 10×10 2D array. \n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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 }