0
0
NumPydata~5 mins

Indexing returns views not copies in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean that numpy indexing returns views, not copies?
It means when you select part of a numpy array using indexing, you get a view that points to the original data. Changing the view changes the original array too.
Click to reveal answer
beginner
How can you create a copy of a numpy array slice instead of a view?
Use the .copy() method on the slice. For example, slice.copy() creates a new array independent of the original.
Click to reveal answer
intermediate
Why is it useful that numpy indexing returns views?
Views save memory and time because they don’t duplicate data. You can work with parts of large arrays efficiently.
Click to reveal answer
beginner
What happens if you modify a view created by numpy indexing?
The original array changes too because the view shares the same data buffer.
Click to reveal answer
beginner
Give an example of numpy indexing that returns a view.
Example:<br><code>import numpy as np<br>arr = np.array([1, 2, 3, 4])<br>view = arr[1:3]<br>view[0] = 10<br>print(arr)</code><br>Output:<br><code>[ 1 10  3  4]</code><br>This shows the original array changed when the view was modified.
Click to reveal answer
What does numpy indexing return by default?
AA new unrelated array
BA copy of the original array
CA list
DA view of the original array
How do you create a copy of a numpy array slice?
AUse <code>.copy()</code> method
BUse <code>.view()</code> method
CUse <code>list()</code> function
DUse <code>slice()</code> function
If you change a value in a numpy view, what happens?
AThe original array changes too
BOnly the view changes
CBoth arrays become disconnected
DAn error occurs
Why might numpy use views instead of copies?
ATo confuse users
BBecause copies are impossible
CTo save memory and improve speed
DTo prevent changes
Which method returns a new array that does NOT affect the original when changed?
ASlicing with colon
B<code>.copy()</code>
CIndexing with brackets
D<code>.view()</code>
Explain in your own words what happens when you index a numpy array. How does this affect memory and data changes?
Think about whether the data is copied or shared.
You got /4 concepts.
    Describe how to safely modify a part of a numpy array without changing the original array.
    Consider how to break the link between the slice and original.
    You got /3 concepts.