{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Array, Slices and Fancy indexing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this small post we'll investigate (quickly) the difference between `slices` (which are part of the Python standard library), and numpy array, and how these could be used for indexing. First let's create a matrix containing integers so that element at index `i,j` has value `10i+j` for convenience. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from copy import copy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's create a single row, that is to say a matrix or height `1` and width number of element. \n", "We'll use `-1` in reshape to mean \"whatever is necessary\". for 2d matrices and tensor it's not super useful, but for higher dimension object, it can be quite conveneient. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = np.arange(0, 10).reshape(1,-1)\n", "X" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "now a column, same trick." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0],\n", " [10],\n", " [20],\n", " [30],\n", " [40],\n", " [50],\n", " [60],\n", " [70]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Y = (10*np.arange(0, 8).reshape(-1, 1))\n", "Y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By summing, and the rules of \"broadcasting\", we get a nice rectangular matrix. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "R = np.arange(5*5*5*5*5).reshape(5,5,5,5,5)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\n", " [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],\n", " [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],\n", " [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],\n", " [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],\n", " [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],\n", " [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],\n", " [70, 71, 72, 73, 74, 75, 76, 77, 78, 79]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M = X+Y\n", "M" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Slicing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Quick intro about slicing. You have likely use it before if you've encoutered the `objet[12:34]` `objet[42:96:3]` notation. The `X:Y:Z` part is a slice. This way of writing a slice is allowed only in between square bracket for indexing.\n", "\n", "`X`, `Y` and `Z` are optional and default to whatever is convenient, so `::3` (every three), `:7` and `:7:` (until 7), `:` and `::` (everything) are valid slices. \n", "\n", "A slice is an efficent object that (usually) represent \"From X to Y by Every Z\", it is not limitted to numbers." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "From `cow` to `phone` every `traffic jam`.\n" ] } ], "source": [ "class PhylosophicalArray:\n", " \n", " def __getitem__(self, sl):\n", " print(f\"From `{sl.start}` to `{sl.stop}` every `{sl.step}`.\") \n", " \n", "arr = PhylosophicalArray()\n", "arr['cow':'phone':'traffic jam']\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can construct a slice using the `slice` builtin, this is (sometime) convenient, and use it in place of `x:y:z`" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "sl = slice('cow', 'phone', 'traffic jam')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "From `cow` to `phone` every `traffic jam`.\n" ] } ], "source": [ "arr[sl]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In multidimentional arrays, slice of `0` or `1` width, can be used to not drop dimensions, when comparing them to scalars." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 3, 13, 23, 33, 43, 53, 63, 73])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M[:, 3] # third column, now a vector." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 3],\n", " [13],\n", " [23],\n", " [33],\n", " [43],\n", " [53],\n", " [63],\n", " [73]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M[:, 3:4] # now a N,1 matrix. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is convenient when indices represent various quatities, for example an athmospheric ensemble when dimension 1 is latitude, 2: longitude, 3: height, 4: temperature, 5: pressure, and you want to focus on `height==0`, without having to shift temprature index from `4` to `3`, pressure from `5` to `4`...\n", "\n", "Zero-width slices are mostly used to simplify algorythmes to avoid having to check for edge cases." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([], shape=(0, 10), dtype=int64)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 3\n", "b = 3\n", "M[a:b]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "M[a:b] = a-b" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\n", " [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],\n", " [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],\n", " [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],\n", " [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],\n", " [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],\n", " [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],\n", " [70, 71, 72, 73, 74, 75, 76, 77, 78, 79]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M # M is not modified !" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When indexing an array, you will slice each dimention individually.\n", "Here __we extract the center block__ of the matrix __not the 3 diagonal elements__." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[44, 45, 46],\n", " [54, 55, 56],\n", " [64, 65, 66]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M[4:7, 4:7]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "slice(4, 7, None)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sl = slice(4,7)\n", "sl" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[44, 45, 46],\n", " [54, 55, 56],\n", " [64, 65, 66]])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M[sl, sl]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's change the sign the **biggest square block** in the upper left of this matrix. " ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "slice(0, 8, None)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "K = copy(M)\n", "el = slice(0, min(K.shape))\n", "el\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, -1, -2, -3, -4, -5, -6, -7, 8, 9],\n", " [-10, -11, -12, -13, -14, -15, -16, -17, 18, 19],\n", " [-20, -21, -22, -23, -24, -25, -26, -27, 28, 29],\n", " [-30, -31, -32, -33, -34, -35, -36, -37, 38, 39],\n", " [-40, -41, -42, -43, -44, -45, -46, -47, 48, 49],\n", " [-50, -51, -52, -53, -54, -55, -56, -57, 58, 59],\n", " [-60, -61, -62, -63, -64, -65, -66, -67, 68, 69],\n", " [-70, -71, -72, -73, -74, -75, -76, -77, 78, 79]])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "K[el, el] = -K[el, el]\n", "K" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's about for slices, it was already a lot. \n", "\n", "In the next section we'll talk about arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fancy indexing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Array are more or less what you've seem in other languages. Finite Sequences of discrete values" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([4, 5, 6])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ar = np.arange(4,7)\n", "ar" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you slice with array, the elements of each arrays will be taken together. " ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([44, 55, 66])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M[ar,ar]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now get _a partial diagonal_ in out matrix. It does not have to be a diaonal:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([45, 56, 67])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M[ar, ar+1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The result of this operation is a 1 dimentional array (which is a view – when possible –  on the initial matrix memory), \n", "in the same way as we flipped the sign of the largest block in the previous section, we'll try indexing with the same value:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "S = copy(M)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "el = np.arange(min(S.shape))\n", "el" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\n", " [ 10, -11, 12, 13, 14, 15, 16, 17, 18, 19],\n", " [ 20, 21, -22, 23, 24, 25, 26, 27, 28, 29],\n", " [ 30, 31, 32, -33, 34, 35, 36, 37, 38, 39],\n", " [ 40, 41, 42, 43, -44, 45, 46, 47, 48, 49],\n", " [ 50, 51, 52, 53, 54, -55, 56, 57, 58, 59],\n", " [ 60, 61, 62, 63, 64, 65, -66, 67, 68, 69],\n", " [ 70, 71, 72, 73, 74, 75, 76, -77, 78, 79]])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S[el, el] = -S[el,el]\n", "S" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we flipped the value of **only the diagonal elements**. It of couse did not had to do the diagonal elements:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 12, 23, 34, 45, 56, 67, 78])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S[el, el+1]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 0, 2, 3, 4, 5, 6, 7, 8, 9],\n", " [ 10, -11, 0, 13, 14, 15, 16, 17, 18, 19],\n", " [ 20, 21, -22, 0, 24, 25, 26, 27, 28, 29],\n", " [ 30, 31, 32, -33, 0, 35, 36, 37, 38, 39],\n", " [ 40, 41, 42, 43, -44, 0, 46, 47, 48, 49],\n", " [ 50, 51, 52, 53, 54, -55, 0, 57, 58, 59],\n", " [ 60, 61, 62, 63, 64, 65, -66, 0, 68, 69],\n", " [ 70, 71, 72, 73, 74, 75, 76, -77, 0, 79]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S[el, el+1] = 0\n", "S" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nor are we required to have the same elements only once:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-1, 0, 1, 2, 3, 4, 5, 6])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "el-1" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([10, 21, 32, 10, 21, 32])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sy = np.array([0, 1, 2, 0, 1, 2])\n", "sx = np.array([1, 2, 3, 1, 2, 3])\n", "ld = S[sx, sy] # select 3 elements of lower diagonal twice\n", "ld" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "More in the [scipy lectures notes](https://www.scipy-lectures.org/intro/numpy/array_object.html#fancy-indexing), Numpy [quickstart](https://docs.scipy.org/doc/numpy/user/quickstart.html?highlight=fancy#fancy-indexing-and-index-tricks), [Python DataScience Handbook](https://jakevdp.github.io/PythonDataScienceHandbook/02.07-fancy-indexing.html)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Some experiments" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S = copy(M)\n", "S[0:10, 0:10] = 0\n", "S" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],\n", " [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],\n", " [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],\n", " [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [70, 71, 72, 73, 74, 75, 76, 77, 78, 79]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S = copy(M)\n", "S[0:10:2, 0:10] = 0\n", "S" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 0, 3, 0, 5, 0, 7, 0, 9],\n", " [ 0, 11, 0, 13, 0, 15, 0, 17, 0, 19],\n", " [ 0, 21, 0, 23, 0, 25, 0, 27, 0, 29],\n", " [ 0, 31, 0, 33, 0, 35, 0, 37, 0, 39],\n", " [ 0, 41, 0, 43, 0, 45, 0, 47, 0, 49],\n", " [ 0, 51, 0, 53, 0, 55, 0, 57, 0, 59],\n", " [ 0, 61, 0, 63, 0, 65, 0, 67, 0, 69],\n", " [ 0, 71, 0, 73, 0, 75, 0, 77, 0, 79]])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S = copy(M)\n", "S[0:10, 0:10:2] = 0\n", "S" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 0, 3, 0, 5, 0, 7, 0, 9],\n", " [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],\n", " [ 0, 21, 0, 23, 0, 25, 0, 27, 0, 29],\n", " [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],\n", " [ 0, 41, 0, 43, 0, 45, 0, 47, 0, 49],\n", " [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],\n", " [ 0, 61, 0, 63, 0, 65, 0, 67, 0, 69],\n", " [70, 71, 72, 73, 74, 75, 76, 77, 78, 79]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S = copy(M)\n", "S[0:10:2, 0:10:2] = 0\n", "S" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [ 0, 11, 0, 13, 0, 15, 0, 17, 0, 19],\n", " [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [ 0, 31, 0, 33, 0, 35, 0, 37, 0, 39],\n", " [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [ 0, 51, 0, 53, 0, 55, 0, 57, 0, 59],\n", " [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [ 0, 71, 0, 73, 0, 75, 0, 77, 0, 79]])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S = copy(M)\n", "S[0:10:2, 0:10] = 0\n", "S[0:10, 0:10:2] = 0\n", "S" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 0, 0, 0, 0, 0, 0, 0, 8, 9],\n", " [ 0, 0, 0, 0, 0, 0, 0, 0, 18, 19],\n", " [ 0, 0, 0, 0, 0, 0, 0, 0, 28, 29],\n", " [ 0, 0, 0, 0, 0, 0, 0, 0, 38, 39],\n", " [ 0, 0, 0, 0, 0, 0, 0, 0, 48, 49],\n", " [ 0, 0, 0, 0, 0, 0, 0, 0, 58, 59],\n", " [ 0, 0, 0, 0, 0, 0, 0, 0, 68, 69],\n", " [ 0, 0, 0, 0, 0, 0, 0, 0, 78, 79]])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S = copy(M)\n", "S[0:8, 0:8] = 0\n", "S" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\n", " [10, 0, 12, 13, 14, 15, 16, 17, 18, 19],\n", " [20, 21, 0, 23, 24, 25, 26, 27, 28, 29],\n", " [30, 31, 32, 0, 34, 35, 36, 37, 38, 39],\n", " [40, 41, 42, 43, 0, 45, 46, 47, 48, 49],\n", " [50, 51, 52, 53, 54, 0, 56, 57, 58, 59],\n", " [60, 61, 62, 63, 64, 65, 0, 67, 68, 69],\n", " [70, 71, 72, 73, 74, 75, 76, 0, 78, 79]])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S = copy(M)\n", "S[np.arange(0,8), np.arange(0,8)] = 0\n", "S" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\n", " [10, 0, 12, 13, 14, 15, 16, 17, 18, 19],\n", " [20, 21, 0, 23, 24, 25, 26, 27, 28, 29],\n", " [30, 31, 32, 0, 34, 35, 36, 37, 38, 39],\n", " [40, 41, 42, 43, 0, 45, 46, 47, 48, 49],\n", " [50, 51, 52, 53, 54, 0, 56, 57, 58, 59],\n", " [60, 61, 62, 63, 64, 65, 0, 67, 68, 69],\n", " [70, 71, 72, 73, 74, 75, 76, 0, 78, 79]])" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S = copy(M)\n", "S[range(0,8), range(0,8)] = 0\n", "S" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "index 8 is out of bounds for axis 0 with size 8", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\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[0mS\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mM\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mS\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;31m## will fail\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mS\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mIndexError\u001b[0m: index 8 is out of bounds for axis 0 with size 8" ] } ], "source": [ "S = copy(M)\n", "S[np.arange(0, 10), np.arange(0, 10)] = 0 ## will fail\n", "S" ] }, { "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }