Last active
September 8, 2015 10:52
-
-
Save CMCDragonkai/c0845f37ab0a77d04114 to your computer and use it in GitHub Desktop.
Python: Array Access & Manipulation Syntax (Including Numpy!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:5396527f06996ae84589918cd7a5eb85e24456df5cbf8468b9203c954002f9bd" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Python has varied array access and manipulation syntax. This is a guide to both native syntax and the extended Numpy syntax." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# 3 dimensional object\n", | |
"\n", | |
"# 3 rows long\n", | |
"# 2 columns across\n", | |
"# 3 layers deep\n", | |
"\n", | |
"# 0 1\n", | |
"# 0 [1,2,3] [4,5,6]\n", | |
"# 1 [7,8,9] [10,11,12]\n", | |
"# 2 [13,14,15] [16,17,18]\n", | |
"\n", | |
"array = [\n", | |
" [\n", | |
" [1,2,3],\n", | |
" [4,5,6]\n", | |
" ],\n", | |
" [\n", | |
" [7,8,9],\n", | |
" [10,11,12]\n", | |
" ],\n", | |
" [\n", | |
" [13, 14, 15],\n", | |
" [16, 17, 18]\n", | |
" ]\n", | |
"]\n", | |
"\n", | |
"array" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 1, | |
"text": [ | |
"[[[1, 2, 3], [4, 5, 6]],\n", | |
" [[7, 8, 9], [10, 11, 12]],\n", | |
" [[13, 14, 15], [16, 17, 18]]]" | |
] | |
} | |
], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# access by row-index 0\n", | |
"array[0]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 2, | |
"text": [ | |
"[[1, 2, 3], [4, 5, 6]]" | |
] | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# access by row-index 0, column-index 1, layer-index 2\n", | |
"array[0][1][2]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 3, | |
"text": [ | |
"6" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# access by row-index-range start-end\n", | |
"# using the [:] operator ALWAYS returns an array\n", | |
"array[:]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 4, | |
"text": [ | |
"[[[1, 2, 3], [4, 5, 6]],\n", | |
" [[7, 8, 9], [10, 11, 12]],\n", | |
" [[13, 14, 15], [16, 17, 18]]]" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# access by row-index-range 1-end\n", | |
"array[1:]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 5, | |
"text": [ | |
"[[[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]]" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# access by row-index-range start-1 (exclusive)\n", | |
"array[:1]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 6, | |
"text": [ | |
"[[[1, 2, 3], [4, 5, 6]]]" | |
] | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# access by row-index-range 1-2 (exclusive)\n", | |
"array[1:2]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 7, | |
"text": [ | |
"[[[7, 8, 9], [10, 11, 12]]]" | |
] | |
} | |
], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# access by row-index 0, column-index-range 1-end, range-index 0, layer-index 1\n", | |
"# note how column-index-range returns an array\n", | |
"# pick the first row\n", | |
"# take a range of columns\n", | |
"# pick the column\n", | |
"# pick the layer depth\n", | |
"array[0][1:][0][1]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 8, | |
"text": [ | |
"5" | |
] | |
} | |
], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# access by row-index-range start-end, row-index 0 !!\n", | |
"print array[:][0]\n", | |
"\n", | |
"# access by row-index 0\n", | |
"print array[0]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[1, 2, 3], [4, 5, 6]]\n", | |
"[[1, 2, 3], [4, 5, 6]]\n" | |
] | |
} | |
], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# notice that these are equal, the `[:]` is a no-op\n", | |
"array[:][0] == array[0]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 10, | |
"text": [ | |
"True" | |
] | |
} | |
], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# it works in the middle too\n", | |
"# it doesn't do anything here\n", | |
"array[0][:][0] == array[0][0]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 11, | |
"text": [ | |
"True" | |
] | |
} | |
], | |
"prompt_number": 11 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# having an empty range access may seem useless, but it's useful for copying values\n", | |
"# arrays are usually assigned by reference\n", | |
"# if you don't want the original array to be modified\n", | |
"# we can use list slicing to acquire a new array\n", | |
"# however this only works on the current 1st dimension\n", | |
"# it's a shallow copy\n", | |
"# you can still mutate the original array by accessing the 2nd or 3rd dimension\n", | |
"array_new = array[:]\n", | |
"array_new[1] = \"replaced!\"\n", | |
"print array\n", | |
"print array_new" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]]\n", | |
"[[[1, 2, 3], [4, 5, 6]], 'replaced!', [[13, 14, 15], [16, 17, 18]]]\n" | |
] | |
} | |
], | |
"prompt_number": 12 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# to get a completely new list, we need to perform a deep copy\n", | |
"# now array_new is a completely independent array in all 3 dimensions\n", | |
"import copy\n", | |
"array_new = copy.deepcopy(array)\n", | |
"array_new[1][0] = \"replaced!\"\n", | |
"print array\n", | |
"print array_new" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]]\n", | |
"[[[1, 2, 3], [4, 5, 6]], ['replaced!', [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]]\n" | |
] | |
} | |
], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# access by row-index-range start-end & step by 1\n", | |
"array[::1]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 14, | |
"text": [ | |
"[[[1, 2, 3], [4, 5, 6]],\n", | |
" [[7, 8, 9], [10, 11, 12]],\n", | |
" [[13, 14, 15], [16, 17, 18]]]" | |
] | |
} | |
], | |
"prompt_number": 14 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# access by row-index-range start-end & step by 2\n", | |
"# notice how it skips a row\n", | |
"array[::2]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 15, | |
"text": [ | |
"[[[1, 2, 3], [4, 5, 6]], [[13, 14, 15], [16, 17, 18]]]" | |
] | |
} | |
], | |
"prompt_number": 15 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# access by row-index-range start-end & step by -1\n", | |
"# reverses the rows\n", | |
"array[::-1]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 16, | |
"text": [ | |
"[[[13, 14, 15], [16, 17, 18]],\n", | |
" [[7, 8, 9], [10, 11, 12]],\n", | |
" [[1, 2, 3], [4, 5, 6]]]" | |
] | |
} | |
], | |
"prompt_number": 16 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# compare the 2\n", | |
"print array\n", | |
"print array[::-1]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]]\n", | |
"[[[13, 14, 15], [16, 17, 18]], [[7, 8, 9], [10, 11, 12]], [[1, 2, 3], [4, 5, 6]]]\n" | |
] | |
} | |
], | |
"prompt_number": 17 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# access by row-index-range start-end & step by -2\n", | |
"# reverses the rows & skips 1\n", | |
"array[::-2]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 18, | |
"text": [ | |
"[[[13, 14, 15], [16, 17, 18]], [[1, 2, 3], [4, 5, 6]]]" | |
] | |
} | |
], | |
"prompt_number": 18 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# list comprehensions can allow one to index an array from another array\n", | |
"\n", | |
"# the surrounding [] always returns a list\n", | |
"# the array[i] expression evaluates to the element and will store it in the surrounding list\n", | |
"\n", | |
"# range strict evaluation\n", | |
"print [array[i] for i in range(len(array))]\n", | |
"# xrange lazy generator\n", | |
"print [array[i] for i in xrange(len(array))]\n", | |
"# with a filter\n", | |
"print [array[i] for i in xrange(len(array)) if i == 0]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]]\n", | |
"[[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]]\n", | |
"[[[1, 2, 3], [4, 5, 6]]]\n" | |
] | |
} | |
], | |
"prompt_number": 19 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# since all arrays are passed by reference\n", | |
"# all of our access patterns above can be used as assignable references\n", | |
"import copy\n", | |
"\n", | |
"array_mut = copy.deepcopy(array)\n", | |
"# elements can be assigned\n", | |
"array_mut[0] = [[0, 0, 0], [0, 0, 0]]\n", | |
"# ranges can be assigned, but only to other arrays\n", | |
"array_mut[1][0:2] = [[1,1,1], [2,2,2]]\n", | |
"# however it is not possible do this for elements internal to the range\n", | |
"# this is because the slice operator [:] returns a new list (shallow copy)\n", | |
"array_mut[2][::-1][0] = \"non-existent!\"\n", | |
"\n", | |
"# compare\n", | |
"print array_mut\n", | |
"print array" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[[0, 0, 0], [0, 0, 0]], [[1, 1, 1], [2, 2, 2]], [[13, 14, 15], [16, 17, 18]]]\n", | |
"[[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]]\n" | |
] | |
} | |
], | |
"prompt_number": 20 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# other array construction techniques\n", | |
"# using the same value for all elements\n", | |
"print [1] * 10\n", | |
"# using range with a step\n", | |
"print range(1, 10, 2)\n", | |
"# using xrange with a step\n", | |
"print list(xrange(1, 10, 2))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n", | |
"[1, 3, 5, 7, 9]\n", | |
"[1, 3, 5, 7, 9]\n" | |
] | |
} | |
], | |
"prompt_number": 21 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"array" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 22, | |
"text": [ | |
"[[[1, 2, 3], [4, 5, 6]],\n", | |
" [[7, 8, 9], [10, 11, 12]],\n", | |
" [[13, 14, 15], [16, 17, 18]]]" | |
] | |
} | |
], | |
"prompt_number": 22 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This is the pretty much it for Python array manipulation syntax.\n", | |
"\n", | |
"Numpy and possibly other packages extend this syntax to include more access and manipulation patterns!" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import numpy as np" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 23 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# construct a numpy array from our original array\n", | |
"# numpy recognises the array as 3 dimensions\n", | |
"# this deep copies the array by default\n", | |
"narray = np.array(array)\n", | |
"narray" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 24, | |
"text": [ | |
"array([[[ 1, 2, 3],\n", | |
" [ 4, 5, 6]],\n", | |
"\n", | |
" [[ 7, 8, 9],\n", | |
" [10, 11, 12]],\n", | |
"\n", | |
" [[13, 14, 15],\n", | |
" [16, 17, 18]]])" | |
] | |
} | |
], | |
"prompt_number": 24 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# alternatively we can easily create a numpy range\n", | |
"# syntax is equivalent to range/xrange\n", | |
"np.arange(1, 10, 2)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 25, | |
"text": [ | |
"array([1, 3, 5, 7, 9])" | |
] | |
} | |
], | |
"prompt_number": 25 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# standard access patterns are available\n", | |
"print narray[0]\n", | |
"print narray[1:2]\n", | |
"print narray[0][:]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[1 2 3]\n", | |
" [4 5 6]]\n", | |
"[[[ 7 8 9]\n", | |
" [10 11 12]]]\n", | |
"[[1 2 3]\n", | |
" [4 5 6]]\n" | |
] | |
} | |
], | |
"prompt_number": 26 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# numpy arrays don't perform a shallow copy on the range slicing operator [:]\n", | |
"# this is important because the slicing operator can be used to assign \n", | |
"# values in the selection range unlike native lists\n", | |
"\n", | |
"# numpy objects have a copy method which automatically performs a deep copy\n", | |
"narray_new = narray.copy()\n", | |
"narray_new[1] = 123434345\n", | |
"\n", | |
"# narray_notnew range selects all of narray and does not copy any values\n", | |
"narray_notnew = narray[:]\n", | |
"# this assigns 0 to the first row of narray by reference of narray_notnew\n", | |
"narray_notnew[0] = 0\n", | |
"\n", | |
"# compare\n", | |
"print \"narray_new:\"\n", | |
"print narray_new\n", | |
"print \"narray:\"\n", | |
"print narray" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"narray_new:\n", | |
"[[[ 1 2 3]\n", | |
" [ 4 5 6]]\n", | |
"\n", | |
" [[123434345 123434345 123434345]\n", | |
" [123434345 123434345 123434345]]\n", | |
"\n", | |
" [[ 13 14 15]\n", | |
" [ 16 17 18]]]\n", | |
"narray:\n", | |
"[[[ 0 0 0]\n", | |
" [ 0 0 0]]\n", | |
"\n", | |
" [[ 7 8 9]\n", | |
" [10 11 12]]\n", | |
"\n", | |
" [[13 14 15]\n", | |
" [16 17 18]]]\n" | |
] | |
} | |
], | |
"prompt_number": 27 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# did you see how assignment was applied on every field in the range?\n", | |
"# it wasn't a replacement of the selected range, it was an overloaded \n", | |
"# mapping operation\n", | |
"#\n", | |
"# narray_notnew[0] represents the 2 dimensional structure:\n", | |
"# [\n", | |
"# [ 1 2 3 ]\n", | |
"# [ 4 5 6 ]\n", | |
"# ]\n", | |
"\n", | |
"# maps a scalar to every field (row * column)\n", | |
"narray_notnew[0] = 1\n", | |
"print narray\n", | |
"# [\n", | |
"# [ 1 1 1 ]\n", | |
"# [ 1 1 1 ]\n", | |
"# ]\n", | |
"\n", | |
"# maps a 1 dimensional object to every row\n", | |
"narray_notnew[0] = [1,2,3]\n", | |
"print narray\n", | |
"# [\n", | |
"# [ 1 2 3 ]\n", | |
"# [ 1 2 3 ]\n", | |
"# ]\n", | |
"\n", | |
"# maps a 2 dimensional object to the to itself (identity)\n", | |
"narray_notnew[0] = [[1,2,3], [4,5,6]]\n", | |
"print narray\n", | |
"# [\n", | |
"# [ 1 2 3 ]\n", | |
"# [ 4 5 6 ]\n", | |
"# ]\n", | |
"\n", | |
"# the most important lesson here is that numpy arrays keep their dimensional\n", | |
"# structure during assignments\n", | |
"# you can change their dimensions by assigning objects that don't fit into \n", | |
"# the original structure\n", | |
"# this would result in error: `narray_notnew[0] = [[1,2,3], [4,5,6,7]]`" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[[ 1 1 1]\n", | |
" [ 1 1 1]]\n", | |
"\n", | |
" [[ 7 8 9]\n", | |
" [10 11 12]]\n", | |
"\n", | |
" [[13 14 15]\n", | |
" [16 17 18]]]\n", | |
"[[[ 1 2 3]\n", | |
" [ 1 2 3]]\n", | |
"\n", | |
" [[ 7 8 9]\n", | |
" [10 11 12]]\n", | |
"\n", | |
" [[13 14 15]\n", | |
" [16 17 18]]]\n", | |
"[[[ 1 2 3]\n", | |
" [ 4 5 6]]\n", | |
"\n", | |
" [[ 7 8 9]\n", | |
" [10 11 12]]\n", | |
"\n", | |
" [[13 14 15]\n", | |
" [16 17 18]]]\n" | |
] | |
} | |
], | |
"prompt_number": 28 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# we are back to our baseline\n", | |
"narray" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 29, | |
"text": [ | |
"array([[[ 1, 2, 3],\n", | |
" [ 4, 5, 6]],\n", | |
"\n", | |
" [[ 7, 8, 9],\n", | |
" [10, 11, 12]],\n", | |
"\n", | |
" [[13, 14, 15],\n", | |
" [16, 17, 18]]])" | |
] | |
} | |
], | |
"prompt_number": 29 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# another feature of numpy arrays is the usage of tuple selections indexes\n", | |
"\n", | |
"# compare these 3\n", | |
"print narray[:,1] # row-index-range start-end & column-index 1\n", | |
"print \n", | |
"print narray[:][1] # row-index-range start-end, row-index 1\n", | |
"print\n", | |
"print narray[1] # row-index 1\n", | |
"\n", | |
"# the tuple selection represents a cartesian product of conditions to fulfill\n", | |
"# [:,1] means get me this {x | x <= A and x <= B}\n", | |
"# where {} constructs ana rray\n", | |
"# x represents the value in the array\n", | |
"# A is the condition that which x must be a member of rows start to end\n", | |
"# B is the condition that which x must be a member of column index 1 for all A\n", | |
"#\n", | |
"# subsequent conditions build up on previous conditions\n", | |
"#\n", | |
"# remember this?\n", | |
"#\n", | |
"# 0 1\n", | |
"# 0 [1,2,3] [4,5,6]\n", | |
"# 1 [7,8,9] [10,11,12]\n", | |
"# 2 [13,14,15] [16,17,18]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[ 4 5 6]\n", | |
" [10 11 12]\n", | |
" [16 17 18]]\n", | |
"\n", | |
"[[ 7 8 9]\n", | |
" [10 11 12]]\n", | |
"\n", | |
"[[ 7 8 9]\n", | |
" [10 11 12]]\n" | |
] | |
} | |
], | |
"prompt_number": 30 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# tuple selection can be extended to use lists as its members or other ranges\n", | |
"# and the more commas are used, the more dimensional constraints are added \n", | |
"# to the selection\n", | |
"\n", | |
"# notice the differences between them all\n", | |
"# tbh, I'm not entirely sure what is happening here\n", | |
"\n", | |
"print narray[:,:1]\n", | |
"print \"---\"\n", | |
"print narray[:,[0]]\n", | |
"print \"---\"\n", | |
"print narray[[0,1,2], :1]\n", | |
"print \"---\"\n", | |
"print narray[:,0]\n", | |
"print \"---\"\n", | |
"print narray[[0,1,2], [0]]\n", | |
"print \"---\"\n", | |
"print narray[[0,1,2], 0]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[[ 1 2 3]]\n", | |
"\n", | |
" [[ 7 8 9]]\n", | |
"\n", | |
" [[13 14 15]]]\n", | |
"---\n", | |
"[[[ 1 2 3]]\n", | |
"\n", | |
" [[ 7 8 9]]\n", | |
"\n", | |
" [[13 14 15]]]\n", | |
"---\n", | |
"[[[ 1 2 3]]\n", | |
"\n", | |
" [[ 7 8 9]]\n", | |
"\n", | |
" [[13 14 15]]]\n", | |
"---\n", | |
"[[ 1 2 3]\n", | |
" [ 7 8 9]\n", | |
" [13 14 15]]\n", | |
"---\n", | |
"[[ 1 2 3]\n", | |
" [ 7 8 9]\n", | |
" [13 14 15]]\n", | |
"---\n", | |
"[[ 1 2 3]\n", | |
" [ 7 8 9]\n", | |
" [13 14 15]]\n" | |
] | |
} | |
], | |
"prompt_number": 31 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# row-index-range start-end & column-index-range start-end & layer-index [2,1]\n", | |
"# the layer index [2, 1] means\n", | |
"# get the 2nd of the first row & column field\n", | |
"# get the 1st of the second row & column field\n", | |
"# this ends up filtering our initial table where it has 3 layers down to 2 layers\n", | |
"# and the 2 layers are indexed by the original 2nd and 1st layer\n", | |
"narray[:,:,[2,1]]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 32, | |
"text": [ | |
"array([[[ 3, 2],\n", | |
" [ 6, 5]],\n", | |
"\n", | |
" [[ 9, 8],\n", | |
" [12, 11]],\n", | |
"\n", | |
" [[15, 14],\n", | |
" [18, 17]]])" | |
] | |
} | |
], | |
"prompt_number": 32 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# what do you think this means?\n", | |
"# it ends up selecting the 0th column of the 0th row, and the 1st column of the 1st row\n", | |
"narray[[0,1], [0,1]]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 33, | |
"text": [ | |
"array([[ 1, 2, 3],\n", | |
" [10, 11, 12]])" | |
] | |
} | |
], | |
"prompt_number": 33 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# using lists inside tuple selections is not an independent condition \n", | |
"# like selecting the 3 rows, and select the 3 columns out of the 3 rows\n", | |
"# instead the 2nd list is zipped on the rows\n", | |
"# that is, if the 2nd list contains 3 members [1, 2, 3]\n", | |
"# then we get end getting row1[1], row2[2], row3[3]\n", | |
"# if the rows are the same, then it's just row[1], row[2], row[3]\n", | |
"# this actually makes it possible to do diagonal selections!\n", | |
"# this demonstrates it more clearly:\n", | |
"\n", | |
"matrix = np.zeros((6, 6))\n", | |
"\n", | |
"matrix[1,0:4] = range(100,104)\n", | |
"matrix[2,[0,1,2,3]] = [200, 201, 202, 203]\n", | |
"matrix[3,0:4] = np.arange(300,304)\n", | |
"\n", | |
"# let's take a look at the matrix\n", | |
"print matrix\n", | |
"print \"---\"\n", | |
"print matrix[[1,2,3]] # get these 3 rows\n", | |
"print \"---\"\n", | |
"print matrix[[1,2,3], [1,2,3]] # zipper (1-1, 2-2, 3-3)\n", | |
"# print matrix[[1,2,3], [1,2]] <- this is an error, as it doesn't map on nicely (neither zipper nor applicative)\n", | |
"print matrix[[1,2,3], [1]] # applicative (1 on every row)\n", | |
"print matrix[[1,2,3], 1] # applicative (1 on every row)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[ 0. 0. 0. 0. 0. 0.]\n", | |
" [ 100. 101. 102. 103. 0. 0.]\n", | |
" [ 200. 201. 202. 203. 0. 0.]\n", | |
" [ 300. 301. 302. 303. 0. 0.]\n", | |
" [ 0. 0. 0. 0. 0. 0.]\n", | |
" [ 0. 0. 0. 0. 0. 0.]]\n", | |
"---\n", | |
"[[ 100. 101. 102. 103. 0. 0.]\n", | |
" [ 200. 201. 202. 203. 0. 0.]\n", | |
" [ 300. 301. 302. 303. 0. 0.]]\n", | |
"---\n", | |
"[ 101. 202. 303.]\n", | |
"[ 101. 201. 301.]\n", | |
"[ 101. 201. 301.]\n" | |
] | |
} | |
], | |
"prompt_number": 34 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# tada! this gives us a diagonal selection!\n", | |
"print matrix[[1,2,3], [1,2,3]]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[ 101. 202. 303.]\n" | |
] | |
} | |
], | |
"prompt_number": 35 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment