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