-
-
Save btel/9c7f31a7c85e72b1c70a1820cc11dac1 to your computer and use it in GitHub Desktop.
copy or view
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"A modified exercise from [Advanced Numpy](https://paris-swc.github.io/advanced-numpy-lesson/02-indexing.html#view-or-copy) lecture:\n", | |
"\n", | |
"> **View or copy?**\n", | |
">\n", | |
"> Create an array of elements from 0 to 4. Increment the second element by 1 using `x[1] += 1`. Compare the new array with the original. Now define `y = x[:2]` and increment its second element? Does `x` also change? What happens with `x` when you select a single element from `x` (`y = x[1]`) and increment it? \n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The rationale of this exercise is to explain what a \"view\" is and the difference between selecting a single element (copy) and a slice (view). " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## NumPy: indexing by an integer \n", | |
"\n", | |
"We can change an element of NumPy array in place:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" Before: x = [0 1 2 3 4]\n", | |
" After: x = [0 2 2 3 4]\n" | |
] | |
} | |
], | |
"source": [ | |
"x = np.arange(5)\n", | |
"\n", | |
"print \" Before: x =\", x\n", | |
"x[1] += 1\n", | |
"print \" After: x =\", x" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Result**: assignment incremented the second element.\n", | |
"\n", | |
"**Conclusion**: Assigning/incrementing/etc. an array element in place modifies the array." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## A catch\n", | |
"\n", | |
"The previous example does not work if we first assign the element to a new variable" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" Before: x = [0 1 2 3 4]\n", | |
" After: x = [0 1 2 3 4]\n" | |
] | |
} | |
], | |
"source": [ | |
"# indexing by a slice creates a view\n", | |
"\n", | |
"x = np.arange(5)\n", | |
"\n", | |
"# take first two elements\n", | |
"y = x[1]\n", | |
"\n", | |
"print \" Before: x =\", x\n", | |
"y += 1\n", | |
"print \" After: x =\", x" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Result**: Incrementing the value of `y` didn't change `x`!\n", | |
"\n", | |
"**Conclusion**: Selecting a single element from an array creates a copy of the element." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## NumPy: indexing by a slice\n", | |
"\n", | |
"However indexing NumPy array by a slice gives a different result:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" Before: x = [0 1 2 3 4]\n", | |
" After: x = [0 2 2 3 4]\n" | |
] | |
} | |
], | |
"source": [ | |
"# indexing by a slice creates a view\n", | |
"\n", | |
"x = np.arange(5)\n", | |
"\n", | |
"# take first two elements\n", | |
"y = x[:2]\n", | |
"\n", | |
"print \" Before: x =\", x\n", | |
"y[1] += 1\n", | |
"print \" After: x =\", x" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Result**: incrementing the second element of `y` (slice of `x`) changed also `x`.\n", | |
"\n", | |
"**Conclusion**: Indexing a NumPy array by a slice creates a *view* of the sub-array." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.11" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment