{"metadata": {"name": "", "signature": "sha256:e483fead0c191e6c49967b48c55412c8c04b0f3a47abd174fd604a3d0aa2cb5e"}, "nbformat": 3, "nbformat_minor": 0, "worksheets": [{"metadata": {}, "cells": [{"metadata": {}, "cell_type": "markdown", "source": ["# numpy: Indexing"]}, {"collapsed": false, "outputs": [], "language": "python", "metadata": {}, "cell_type": "code", "input": ["import numpy as np"], "prompt_number": 1}, {"collapsed": false, "outputs": [{"metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": ["(3, 4, 5)"]}], "language": "python", "metadata": {}, "cell_type": "code", "input": ["a = np.random.rand(3,4,5)\n", "a.shape"], "prompt_number": 2}, {"metadata": {}, "cell_type": "markdown", "source": ["What's the result of this?"]}, {"collapsed": false, "outputs": [{"metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": ["(4, 5)"]}], "language": "python", "metadata": {}, "cell_type": "code", "input": ["a[0].shape"], "prompt_number": 3}, {"metadata": {}, "cell_type": "markdown", "source": ["And this?"]}, {"collapsed": false, "outputs": [{"metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": ["(3, 4)"]}], "language": "python", "metadata": {}, "cell_type": "code", "input": ["a[...,2].shape"], "prompt_number": 4}, {"collapsed": false, "outputs": [{"metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": ["0.025588609438720655"]}], "language": "python", "metadata": {}, "cell_type": "code", "input": ["a[1,0,3]"], "prompt_number": 5}, {"metadata": {}, "cell_type": "markdown", "source": ["Like all other things in Python, numpy indexes from 0."]}, {"collapsed": false, "outputs": [{"output_type": "pyerr", "traceback": ["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m#keep\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: index 3 is out of bounds for axis 0 with size 3"], "evalue": "index 3 is out of bounds for axis 0 with size 3", "ename": "IndexError"}], "language": "python", "metadata": {}, "cell_type": "code", "input": ["a[3,2,2].shape"], "prompt_number": 6}, {"collapsed": false, "outputs": [{"metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": ["(3, 5)"]}], "language": "python", "metadata": {}, "cell_type": "code", "input": ["a[:,2].shape"], "prompt_number": 7}, {"metadata": {}, "cell_type": "markdown", "source": ["---\n", "\n", "Indexing into numpy arrays usually results in a so-called *view*."]}, {"collapsed": false, "outputs": [{"metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": ["array([[ 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0.]])"]}], "language": "python", "metadata": {}, "cell_type": "code", "input": ["a = np.zeros((4,4))\n", "a"], "prompt_number": 8}, {"metadata": {}, "cell_type": "markdown", "source": ["Let's call `b` the top-left $2\\times 2$ submatrix."]}, {"collapsed": false, "outputs": [{"metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": ["array([[ 0., 0.],\n", " [ 0., 0.]])"]}], "language": "python", "metadata": {}, "cell_type": "code", "input": ["b = a[:2,:2]\n", "b"], "prompt_number": 9}, {"metadata": {}, "cell_type": "markdown", "source": ["What happens if we change `b`?"]}, {"collapsed": false, "outputs": [{"metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": ["array([[ 0., 0.],\n", " [ 5., 0.]])"]}], "language": "python", "metadata": {}, "cell_type": "code", "input": ["b[1,0] = 5\n", "b"], "prompt_number": 10}, {"collapsed": false, "outputs": [{"output_type": "stream", "text": ["[[ 0. 0. 0. 0.]\n", " [ 5. 0. 0. 0.]\n", " [ 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0.]]\n"], "stream": "stdout"}], "language": "python", "metadata": {}, "cell_type": "code", "input": ["print(a)"], "prompt_number": 12}, {"metadata": {}, "cell_type": "markdown", "source": ["To decouple `b` from `a`, use `.copy()`."]}, {"collapsed": false, "outputs": [{"output_type": "stream", "text": ["[[ 0. 0. 0. 0.]\n", " [ 5. 0. 0. 0.]\n", " [ 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0.]]\n"], "stream": "stdout"}], "language": "python", "metadata": {}, "cell_type": "code", "input": ["b = b.copy()\n", "b[1,1] = 7\n", "print(a) "], "prompt_number": 13}, {"metadata": {}, "cell_type": "markdown", "source": ["----\n", "\n", "You can also index with other arrays:"]}, {"collapsed": false, "outputs": [{"metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": ["array([[ 0.94747406, 0.89080192, 0.46799144, 0.54340544],\n", " [ 0.54409333, 0.27586608, 0.60682897, 0.61962813],\n", " [ 0.06203009, 0.7958913 , 0.93468584, 0.88864481],\n", " [ 0.98627827, 0.73442815, 0.90304704, 0.18186312]])"]}], "language": "python", "metadata": {}, "cell_type": "code", "input": ["a = np.random.rand(4,4)\n", "a"], "prompt_number": 14}, {"collapsed": false, "outputs": [{"metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": ["array([[ 0.94747406, 0.89080192, 0.46799144, 0.54340544],\n", " [ 0.06203009, 0.7958913 , 0.93468584, 0.88864481]])"]}], "language": "python", "metadata": {}, "cell_type": "code", "input": ["i = np.array([0,2])\n", "a[i]"], "prompt_number": 15}, {"metadata": {}, "cell_type": "markdown", "source": ["---\n", "\n", "And with conditionals:"]}, {"collapsed": false, "outputs": [{"metadata": {}, "output_type": "pyout", "prompt_number": 16, "text": ["array([[ True, True, False, True],\n", " [ True, False, True, True],\n", " [False, True, True, True],\n", " [ True, True, True, False]], dtype=bool)"]}], "language": "python", "metadata": {}, "cell_type": "code", "input": ["a>0.5"], "prompt_number": 16}, {"collapsed": false, "outputs": [{"metadata": {}, "output_type": "pyout", "prompt_number": 17, "text": ["array([ 0.94747406, 0.89080192, 0.54340544, 0.54409333, 0.60682897,\n", " 0.61962813, 0.7958913 , 0.93468584, 0.88864481, 0.98627827,\n", " 0.73442815, 0.90304704])"]}], "language": "python", "metadata": {}, "cell_type": "code", "input": ["a[a>0.5]"], "prompt_number": 17}]}]}