0
0
NumPydata~10 mins

View vs copy behavior in NumPy - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - View vs copy behavior
Create original array
Slice or operation
View created?
YesShared data, changes reflect
No
Copy created, independent data
When you slice or operate on a numpy array, sometimes you get a view (shared data) and sometimes a copy (independent data). Views reflect changes back to the original, copies do not.
Execution Sample
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
slice_view = arr[1:3]
slice_view[0] = 20
copy_arr = arr[1:3].copy()
copy_arr[0] = 30
Create an array, make a slice view and modify it, then make a copy and modify it to see difference.
Execution Table
StepCode LineActionarrslice_viewcopy_arrNotes
1arr = np.array([1, 2, 3, 4])Create original array[1 2 3 4]N/AN/AOriginal array created
2slice_view = arr[1:3]Create slice view[1 2 3 4][2 3]N/Aslice_view shares data with arr
3slice_view[0] = 20Modify slice_view[1 20 3 4][20 3]N/AChange in slice_view affects arr
4copy_arr = arr[1:3].copy()Create copy[1 20 3 4][20 3][20 3]copy_arr is independent copy
5copy_arr[0] = 30Modify copy_arr[1 20 3 4][20 3][30 3]Change in copy_arr does NOT affect arr or slice_view
6EndExecution ends[1 20 3 4][20 3][30 3]Final states shown
💡 Execution stops after modifying copy_arr; changes to copy_arr do not affect original arr or slice_view.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
arr[1 2 3 4][1 2 3 4][1 20 3 4][1 20 3 4][1 20 3 4][1 20 3 4]
slice_viewN/A[2 3][20 3][20 3][20 3][20 3]
copy_arrN/AN/AN/A[20 3][30 3][30 3]
Key Moments - 3 Insights
Why does changing slice_view affect arr but changing copy_arr does not?
slice_view is a view sharing the same data as arr (see step 3 in execution_table), so changes reflect back. copy_arr is a separate copy (step 4), so changes do not affect arr.
Is slice_view a new array or just a window into arr?
slice_view is a view, meaning it is a window into arr's data, not a new array (step 2). This is why modifying it changes arr.
What happens if we modify arr after creating slice_view?
Since slice_view shares data with arr, changes to arr in the slice range will also appear in slice_view. This is implied by shared data behavior shown in steps 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the value of arr after modifying slice_view?
A[1 2 3 4]
B[1 30 3 4]
C[1 20 3 4]
D[20 3 4 4]
💡 Hint
Check the 'arr' column at step 3 in execution_table.
At which step does copy_arr get created as an independent copy?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the step where copy_arr first appears in execution_table.
If we changed slice_view[0] = 50 instead of 20 at step 3, what would arr[1] be after step 3?
A50
B30
C20
D2
💡 Hint
Recall that slice_view shares data with arr, so changes reflect back (see step 3).
Concept Snapshot
numpy View vs Copy Behavior:
- Slicing usually creates a view (shared data).
- Modifying a view changes the original array.
- Using .copy() creates an independent copy.
- Modifying a copy does NOT affect the original.
- Always check if your operation returns a view or copy.
Full Transcript
This lesson shows how numpy arrays behave when sliced or copied. When you slice an array, numpy often returns a view, which means the new array shares the same data as the original. Changing the view changes the original array too. But if you use the copy() method, numpy makes a new independent array. Changing this copy does not affect the original. We traced code step-by-step to see these effects clearly.