Learning Objectives – after this chapter you will be able to:
- Distinguish a copy from a view.
- Check ownership with the
baseattribute andnp.shares_memory(). - Know which operations return views and which return copies.
- Avoid accidental changes to original data.
Copy vs View
| Basis | Copy | View |
|---|---|---|
| Data | A new array with its own data. | A new array object that shares the original’s data. |
| Changes to it | Do not affect the original. | Do affect the original (and vice versa). |
| Memory & speed | Uses extra memory; takes time to copy. | No data copied; fast. |
base attribute | None (owns its data) | Refers to the original array |
| Created by | copy(), fancy indexing, boolean indexing, flatten() | view(), basic slicing, reshape() (usually), .T, ravel() (usually) |
Plain assignment
b = a is neither: it creates no new array at all – both names refer to the very same object.copy()
import numpy as np arr = np.array([1, 2, 3, 4, 5]) c = arr.copy() arr[0] = 42 print(arr) print(c) # copy is NOT affected print(c.base) # copy owns its data
Output
[42 2 3 4 5] [1 2 3 4 5] None
view()
import numpy as np arr = np.array([1, 2, 3, 4, 5]) v = arr.view() arr[0] = 42 print(v) # view IS affected v[4] = 99 print(arr) # and changes flow back print(v.base is arr) # view does not own its data
Output
[42 2 3 4 5] [42 2 3 4 99] True
Checking Whether Data is Shared
import numpy as np arr = np.arange(6) s = arr[1:4] # slice -> view f = arr[[1, 2, 3]] # fancy indexing -> copy r = arr.reshape(2, 3) # reshape -> view here b = arr # same object print(np.shares_memory(arr, s), s.base is arr) print(np.shares_memory(arr, f), f.base is None) print(np.shares_memory(arr, r)) print(b is arr)
Output
True True False True True True
A Common Mistake
import numpy as np marks = np.array([55, 72, 38, 90]) top_two = marks[:2] # view! top_two += 5 # meant to change only the extract... print(marks) # ...but the original changed too marks = np.array([55, 72, 38, 90]) top_two = marks[:2].copy() # correct: independent copy top_two += 5 print(marks, top_two)
Output
[60 77 38 90] [55 72 38 90] [60 77]
Key Takeaways
- A copy owns its data (
base is None); a view shares the original’s data. - Slicing gives views; fancy and boolean indexing give copies.
- Use
.copy()whenever later changes must not affect the original.
Video : Array Copy vs View
Question / Answer (15 questions)
Answer : A copy is a new array with its own data, so changes to it do not affect the original. A view is a new array object that shares the original’s data, so changes made through either one are visible in both.
Answer : arr.copy() creates a copy; arr.view() creates a view. Basic slicing such as arr[1:4] also creates a view.
Answer : base is None if the array owns its data (for example a copy). For a view, base refers to the array that owns the data.
Answer : Use np.shares_memory(a, b), which returns True if the arrays use any of the same memory. Checking b.base is a also identifies a direct view.
Answer : Neither. Assignment creates no new array; a and b are two names for the same object, so b is a is True.
Answer : Fancy (integer-array) indexing, such as arr[[1, 2, 3]], and boolean indexing, such as arr[arr > 5], always return copies.
Answer : Basic slicing, arr.view(), the transpose arr.T, and usually reshape() and ravel() (when the data layout allows it).
Answer : Views avoid copying data, saving memory and time. This is important when working with very large arrays.
Answer : Modifying a view unintentionally changes the original array, which can corrupt data that other parts of the program rely on.
Answer : [60 77 38 90]. t is a view of the first two elements, so adding 5 to t also changes marks.
Answer : Take a copy of the slice: t = marks[:2].copy(). Then t += 5 leaves marks unchanged.
Answer : flatten() always returns a new copy. ravel() returns a view whenever possible (and a copy only if needed), so it is faster but changes may affect the original.
Answer : arr[0] changes as well, because the view and the original share the same data.
Answer : When you need to modify extracted data without affecting the source, when storing a snapshot of an array’s current values, or when passing data to code that might modify it.
Answer : No. Boolean indexing selects an arbitrary set of elements that cannot be described by a simple stride pattern, so NumPy returns a copy.
MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.
- NumPy Developers. (n.d.). Copies and views. NumPy User Guide. https://numpy.org/doc/stable/user/basics.copies.html
- NumPy Developers. (n.d.). The N-dimensional array (ndarray). NumPy Reference. https://numpy.org/doc/stable/reference/arrays.ndarray.html
- NumPy Developers. (n.d.). NumPy quickstart. NumPy User Guide. https://numpy.org/doc/stable/user/quickstart.html
- VanderPlas, J. (2016). Python Data Science Handbook. O’Reilly Media. (Free online edition.) https://jakevdp.github.io/PythonDataScienceHandbook/