Views and Copies
Basic slicing, such as A[0, :], returns a view. A view shares data with the
original array.
That means changing the view can change the original.
Here, row is a view, so A changes too.
A slice can share data
Ready to run.
This behavior is useful for performance, but it can surprise you.
Use copy when you need separation
If you want a separate array, use .copy():
row = A[0, :].copy()
Now changing row does not change A.
Copy before changing
Ready to run.
The habit
When you slice and then mutate, ask:
Do I want this change to affect the original array?
If yes, a view can be fine. If no, copy first.
This is the same idea as list copying, but it matters more with arrays because large numerical data should not be copied accidentally.
Not every selection behaves like a basic slice. Some later NumPy operations
return copies instead. Do not try to memorize every case yet; when mutation
matters, use .copy() to state clearly that you want independent data.
What method makes a separate array after slicing?
Answer it first, then check.
Hint
Use the method whose name means “make a duplicate,” including its parentheses.
Solution
Call .copy() on the slice:
row = A[0, :].copy()
Changing row will then leave A unchanged.