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?✗ Incorrect
Indexing in
numpy returns a view, which shares data with the original array.How do you create a copy of a
numpy array slice?✗ Incorrect
The
.copy() method creates a new array independent of the original.If you change a value in a
numpy view, what happens?✗ Incorrect
Views share data with the original array, so changes affect both.
Why might
numpy use views instead of copies?✗ Incorrect
Views avoid copying data, saving memory and making operations faster.
Which method returns a new array that does NOT affect the original when changed?
✗ Incorrect
.copy() creates a separate array independent of the original.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.