What does it mean when a NumPy operation returns a view instead of a copy?
A view means the new array shares the same data in memory as the original array. Changes to the view affect the original array and vice versa.
Click to reveal answer
beginner
How can you create a true copy of a NumPy array?
Use the copy() method on the array, like arr.copy(). This creates a new array with its own data in memory.
Click to reveal answer
intermediate
What is the difference between shallow copy and deep copy in NumPy?
A shallow copy creates a new array object but shares the data buffer (like a view). A deep copy duplicates both the array object and the data buffer (like copy()).
Click to reveal answer
beginner
Why might slicing a NumPy array not create a copy?
Slicing usually creates a view to save memory and time. It does not copy data but references the original array's data.
Click to reveal answer
intermediate
How can you check if a NumPy array owns its data or is a view?
Check the base attribute. If arr.base is None, the array owns its data. Otherwise, it is a view referencing another array.
Click to reveal answer
What does arr.copy() do in NumPy?
ACreates a new array with its own data
BCreates a view sharing the same data
CDeletes the original array
DModifies the original array in place
✗ Incorrect
arr.copy() creates a new array with a separate copy of the data, so changes to the copy do not affect the original.
If you slice a NumPy array like arr[1:4], what is usually returned?
AA view referencing the original data
BA deep copy of the slice
CA Python list
DAn error
✗ Incorrect
Slicing usually returns a view that shares data with the original array to save memory.
How can you tell if a NumPy array is a view or owns its data?
ACheck if <code>arr.size</code> is zero
BCheck if <code>arr.shape</code> is empty
CCheck if <code>arr.base</code> is <code>None</code>
DCheck if <code>arr.dtype</code> is <code>object</code>
✗ Incorrect
If arr.base is None, the array owns its data; otherwise, it is a view.
What happens if you modify a view of a NumPy array?
AOnly the view changes
BBoth the view and original array change
COnly the original array changes
DAn error is raised
✗ Incorrect
Since a view shares data with the original array, changes to the view affect the original array as well.
Which method creates a deep copy of a NumPy array?
A<code>arr.flatten()</code>
B<code>arr.view()</code>
C<code>arr.slice()</code>
D<code>arr.copy()</code>
✗ Incorrect
arr.copy() creates a deep copy with its own data buffer.
Explain the difference between a view and a copy in NumPy arrays.
Think about memory sharing and data changes.
You got /4 concepts.
Describe how to check if a NumPy array owns its data or is a view.
Look at the array's base attribute.
You got /3 concepts.
Practice
(1/5)
1. What does the copy() method do in NumPy arrays?
easy
A. Creates a full separate copy of the array data
B. Creates a new array object sharing the same data
C. Deletes the original array
D. Converts the array to a list
Solution
Step 1: Understand the purpose of copy()
The copy() method creates a new array with its own separate data, independent of the original array.
Step 2: Compare with view()
Unlike view(), which shares data, copy() protects the original data by making a full copy.
Final Answer:
Creates a full separate copy of the array data -> Option A
Quick Check:
copy() = full separate copy [OK]
Hint: copy() makes a full separate array copy [OK]
Common Mistakes:
Confusing copy() with view() sharing data
Thinking copy() modifies original array
Assuming copy() returns a reference
2. Which of the following is the correct syntax to create a view of a NumPy array a?
easy
A. b = a.view()
B. b = view(a)
C. b = copy(a)
D. b = a.copy()
Solution
Step 1: Recall the method to create a view
In NumPy, the method to create a view of an array is view(), called on the array object.
Step 2: Check syntax correctness
Only b = a.view() correctly calls the method on a. Other options are either copy or invalid function calls.
Final Answer:
b = a.view() -> Option A
Quick Check:
Use view() method on array [OK]
Hint: Use a.view() to create a view, not copy() [OK]
Common Mistakes:
Using copy() instead of view()
Calling view() as a standalone function
Confusing syntax with copy module
3. What will be the output of the following code?
import numpy as np
x = np.array([1, 2, 3])
y = x.view()
y[0] = 10
print(x)
medium
A. Error
B. [1 2 3]
C. [0 2 3]
D. [10 2 3]
Solution
Step 1: Understand view() shares data
The variable y is a view of x, so both share the same data buffer.
Step 2: Modify y and check effect on x
Changing y[0] to 10 changes the shared data, so x[0] also becomes 10.
Final Answer:
[10 2 3] -> Option D
Quick Check:
view() shares data, so x changes [OK]
Hint: view() shares data; changes reflect in original [OK]
Common Mistakes:
Assuming view() creates independent copy
Expecting original array unchanged
Confusing view() with copy()
4. Identify the error in the following code snippet: