0
0
NumPydata~10 mins

Controlling copy behavior in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Controlling copy behavior
Create original array
Assign to new variable
Is it a view or copy?
Modify new
Original changes
This flow shows how assigning arrays can create views or copies, affecting whether changes to the new array affect the original.
Execution Sample
NumPy
import numpy as np
arr = np.array([1, 2, 3])
view = arr[:]
copy = arr.copy()
view[0] = 10
copy[1] = 20
print(arr, view, copy)
This code creates an array, a view, and a copy, then modifies them to show how changes affect the original array.
Execution Table
StepVariableActionValue After ActionEffect on Original arr
1arrCreate array [1, 2, 3][1, 2, 3]Original created
2viewAssign slice arr[:][1, 2, 3]View of arr, no copy
3copyCreate copy with arr.copy()[1, 2, 3]Independent copy
4view[0]Set to 10[10, 2, 3]arr changes because view shares data
5copy[1]Set to 20[1, 20, 3]arr unchanged, copy independent
6printPrint arr, view, copyarr=[10, 2, 3], view=[10, 2, 3], copy=[1, 20, 3]Shows effect of changes
💡 Execution ends after printing arrays showing difference between view and copy.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
arr[1, 2, 3][1, 2, 3][1, 2, 3][10, 2, 3][10, 2, 3][10, 2, 3]
viewN/A[1, 2, 3][1, 2, 3][10, 2, 3][10, 2, 3][10, 2, 3]
copyN/AN/A[1, 2, 3][1, 2, 3][1, 20, 3][1, 20, 3]
Key Moments - 3 Insights
Why does changing view[0] also change arr[0]?
Because view is a slice of arr and shares the same data (see step 4 in execution_table), so modifying view changes arr.
Why does changing copy[1] not affect arr?
copy is a separate copy of arr's data (step 3), so changes to copy do not affect arr (step 5).
Is arr[:] always a copy?
No, arr[:] creates a view (step 2), not a copy. To make a copy, use arr.copy().
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is arr after modifying view[0]?
A[10, 2, 3]
B[1, 2, 3]
C[1, 20, 3]
D[10, 20, 3]
💡 Hint
Check the 'Value After Action' and 'Effect on Original arr' columns at step 4.
At which step does copy become different from arr?
AStep 2
BStep 3
CStep 5
DStep 4
💡 Hint
Look at when copy is modified and compare its value to arr in variable_tracker.
If we replaced view = arr[:] with view = arr.copy(), what would happen at step 4?
Aarr would change when view changes
Barr would not change when view changes
Cview would be the same as arr
Dview would be empty
💡 Hint
Recall that arr.copy() creates an independent copy, unlike arr[:].
Concept Snapshot
Controlling copy behavior in numpy:
- arr[:] creates a view (shares data)
- arr.copy() creates an independent copy
- Modifying a view changes original array
- Modifying a copy does not affect original
- Use copy() to avoid unintended changes
Full Transcript
This lesson shows how numpy arrays can be assigned as views or copies. When you assign a slice like arr[:], it creates a view sharing the same data. Changing the view changes the original array. When you use arr.copy(), it creates a new array independent of the original. Changing the copy does not affect the original. The code example demonstrates this by modifying elements in the view and copy and printing the results. Understanding this helps avoid bugs when working with numpy arrays.