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