{"nbformat": 3, "worksheets": [{"cells": [{"cell_type": "code", "input": ["import numpy as np"], "collapsed": false, "outputs": [], "language": "python", "metadata": {}, "prompt_number": 1}, {"cell_type": "code", "input": ["a = np.random.rand(3,4,5)\n", "a.shape"], "collapsed": false, "outputs": [{"output_type": "pyout", "text": ["(3, 4, 5)"], "metadata": {}, "prompt_number": 2}], "language": "python", "metadata": {}, "prompt_number": 2}, {"cell_type": "markdown", "source": ["What's the result of this?"], "metadata": {}}, {"cell_type": "code", "input": ["a[0].shape"], "collapsed": false, "outputs": [{"output_type": "pyout", "text": ["(4, 5)"], "metadata": {}, "prompt_number": 3}], "language": "python", "metadata": {}, "prompt_number": 3}, {"cell_type": "markdown", "source": ["And this?"], "metadata": {}}, {"cell_type": "code", "input": ["a[...,2].shape"], "collapsed": false, "outputs": [{"output_type": "pyout", "text": ["(3, 4)"], "metadata": {}, "prompt_number": 4}], "language": "python", "metadata": {}, "prompt_number": 4}, {"cell_type": "code", "input": ["a[1,0,3]"], "collapsed": false, "outputs": [{"output_type": "pyout", "text": ["0.025588609438720655"], "metadata": {}, "prompt_number": 5}], "language": "python", "metadata": {}, "prompt_number": 5}, {"cell_type": "markdown", "source": ["Like all other things in Python, numpy indexes from 0."], "metadata": {}}, {"cell_type": "code", "input": ["a[3,2,2].shape"], "collapsed": false, "outputs": [{"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"], "output_type": "pyerr", "evalue": "index 3 is out of bounds for axis 0 with size 3", "ename": "IndexError"}], "language": "python", "metadata": {}, "prompt_number": 6}, {"cell_type": "code", "input": ["a[:,2].shape"], "collapsed": false, "outputs": [{"output_type": "pyout", "text": ["(3, 5)"], "metadata": {}, "prompt_number": 7}], "language": "python", "metadata": {}, "prompt_number": 7}, {"cell_type": "markdown", "source": ["---\n", "\n", "Indexing into numpy arrays usually results in a so-called *view*."], "metadata": {}}, {"cell_type": "code", "input": ["a = np.zeros((4,4))\n", "a"], "collapsed": false, "outputs": [{"output_type": "pyout", "text": ["array([[ 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0.]])"], "metadata": {}, "prompt_number": 8}], "language": "python", "metadata": {}, "prompt_number": 8}, {"cell_type": "markdown", "source": ["Let's call `b` the top-left $2\\times 2$ submatrix."], "metadata": {}}, {"cell_type": "code", "input": ["b = a[:2,:2]\n", "b"], "collapsed": false, "outputs": [{"output_type": "pyout", "text": ["array([[ 0., 0.],\n", " [ 0., 0.]])"], "metadata": {}, "prompt_number": 9}], "language": "python", "metadata": {}, "prompt_number": 9}, {"cell_type": "markdown", "source": ["What happens if we change `b`?"], "metadata": {}}, {"cell_type": "code", "input": ["b[1,0] = 5\n", "b"], "collapsed": false, "outputs": [{"output_type": "pyout", "text": ["array([[ 0., 0.],\n", " [ 5., 0.]])"], "metadata": {}, "prompt_number": 10}], "language": "python", "metadata": {}, "prompt_number": 10}, {"cell_type": "code", "input": ["print(a)"], "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": {}, "prompt_number": 12}, {"cell_type": "markdown", "source": ["To decouple `b` from `a`, use `.copy()`."], "metadata": {}}, {"cell_type": "code", "input": ["b = b.copy()\n", "b[1,1] = 7\n", "print(a) "], "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": {}, "prompt_number": 13}, {"cell_type": "markdown", "source": ["----\n", "\n", "You can also index with other arrays:"], "metadata": {}}, {"cell_type": "code", "input": ["a = np.random.rand(4,4)\n", "a"], "collapsed": false, "outputs": [{"output_type": "pyout", "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]])"], "metadata": {}, "prompt_number": 14}], "language": "python", "metadata": {}, "prompt_number": 14}, {"cell_type": "code", "input": ["i = np.array([0,2])\n", "a[i]"], "collapsed": false, "outputs": [{"output_type": "pyout", "text": ["array([[ 0.94747406, 0.89080192, 0.46799144, 0.54340544],\n", " [ 0.06203009, 0.7958913 , 0.93468584, 0.88864481]])"], "metadata": {}, "prompt_number": 15}], "language": "python", "metadata": {}, "prompt_number": 15}, {"cell_type": "markdown", "source": ["---\n", "\n", "And with conditionals:"], "metadata": {}}, {"cell_type": "code", "input": ["a>0.5"], "collapsed": false, "outputs": [{"output_type": "pyout", "text": ["array([[ True, True, False, True],\n", " [ True, False, True, True],\n", " [False, True, True, True],\n", " [ True, True, True, False]], dtype=bool)"], "metadata": {}, "prompt_number": 16}], "language": "python", "metadata": {}, "prompt_number": 16}, {"cell_type": "code", "input": ["a[a>0.5]"], "collapsed": false, "outputs": [{"output_type": "pyout", "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])"], "metadata": {}, "prompt_number": 17}], "language": "python", "metadata": {}, "prompt_number": 17}], "metadata": {}}], "metadata": {"name": "", "signature": "sha256:f917b361fc03cb61a65223e434356878d7e732fd00cf69438e6df87c6221ad44"}, "nbformat_minor": 0}