NumPy Copy vs View in Python: Understanding the Difference
Last updated 3 months, 4 weeks ago | 229 views 75 5

When working with arrays in NumPy, it's crucial to understand the difference between copies and views. Misunderstanding this concept can lead to bugs, memory inefficiencies, or unintended changes in your data.
In this article, you will learn:
-
What a copy and a view are in NumPy
-
How to create them
-
How they behave differently
-
Practical examples with code
-
Tips and common mistakes to avoid
What Is a Copy in NumPy?
A copy of a NumPy array is a completely independent array that holds its own data in memory.
Changes to the copy do not affect the original array.
✅ Example: Copy
import numpy as np
a = np.array([1, 2, 3])
b = a.copy()
b[0] = 100
print("Original array:", a) # [1 2 3]
print("Copied array:", b) # [100 2 3]
As you can see, modifying b
does not change a
.
What Is a View in NumPy?
A view is a new array object that looks at the same data as the original array.
Changes made to a view will affect the original array (and vice versa), since they share the same memory.
✅ Example: View
a = np.array([1, 2, 3])
b = a.view()
b[0] = 100
print("Original array:", a) # [100 2 3]
print("Viewed array:", b) # [100 2 3]
Here, changing b
directly changed a
—because both reference the same data.
How to Check if Two Arrays Share Memory
You can use np.shares_memory()
to verify whether two arrays share the same memory space.
a = np.array([1, 2, 3])
b = a.view()
c = a.copy()
print(np.shares_memory(a, b)) # True
print(np.shares_memory(a, c)) # False
Slicing Usually Returns a View
Slicing a NumPy array usually returns a view, not a copy.
a = np.array([10, 20, 30, 40])
b = a[1:3] # This is a view
b[0] = 99
print("Original array:", a) # [10 99 30 40]
print("Sliced view:", b) # [99 30]
To avoid this, use .copy()
explicitly:
b = a[1:3].copy()
Summary Table: Copy vs View
Feature | Copy | View |
---|---|---|
Data independence | Independent copy | Shares data with original |
Memory | New memory allocated | No new memory allocated |
Changes affect original? | ❌ No | ✅ Yes |
Created with | .copy() |
.view() , slicing |
Memory check | np.shares_memory() → False |
True |
Real-World Example
Let’s say you're normalizing a dataset:
data = np.array([100, 200, 300])
# You want a new normalized version, not affecting the original
normalized = data.copy()
normalized = normalized / np.max(normalized)
print("Original:", data) # [100 200 300]
print("Normalized:", normalized) # [0.333 0.666 1.0]
Without .copy()
, your original data
would be unintentionally modified.
Tips and Best Practices
Tip | Reason |
---|---|
Use .copy() when modifying array subsets |
Avoid altering the original data |
Use .view() when performance is critical and you don’t need duplication |
Saves memory |
Always verify shared memory with np.shares_memory() if unsure |
Prevents bugs |
Document whether your function returns a copy or view | Improves code clarity and reusability |
⚠️ Common Pitfalls
Pitfall | Explanation |
---|---|
Assuming slicing returns a copy | It returns a view by default |
Unintended modification of original array | Happens when a view is modified |
Memory waste from unnecessary copies | Use .copy() only when needed |
Confusing .view() and .copy() |
They behave very differently! |
✅ Full Working Example
import numpy as np
# Original array
original = np.array([1, 2, 3, 4, 5])
# Create a view
viewed = original[1:4]
viewed[0] = 99
print("Original after view modification:", original) # [1 99 3 4 5]
# Create a copy
copied = original[1:4].copy()
copied[0] = 77
print("Original after copy modification:", original) # [1 99 3 4 5]
print("Copied version:", copied) # [77 3 4]
# Check memory sharing
print("View shares memory?", np.shares_memory(original, viewed)) # True
print("Copy shares memory?", np.shares_memory(original, copied)) # False
Conclusion
Understanding the difference between copy and view in NumPy is essential for writing safe and efficient numerical code. Whether you want to save memory with views or isolate data with copies, knowing when and how to use them can save hours of debugging and performance tuning.
What’s Next?
Now that you've mastered copy
vs view
, explore:
-
NumPy Broadcasting
-
Advanced Indexing
-
Memory Layout with
np.flags
-
Structured Arrays and Custom dtypes