Difference Between Copy and View in NumPy
The key difference between a copy and a view of a NumPy array lies in how they handle data:
A copy creates a completely new array that owns its data. Changes made to the copy do not affect the original array, and vice versa.
A view is simply a reference to the original array. It shares the same data, meaning changes to either the view or the original array will affect both.
Example:
Copy
In this example, we create a copy of an array, then modify the original array. The copy remains unaffected:
Program:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
x = arr.copy()
arr[0] = 42
print(arr) # Output: [42 2 3 4 5]
print(x) # Output: [1 2 3 4 5]
Here, x (the copy) does not reflect the change made to arr.
Output:
[42 2 3 4 5]
[1 2 3 4 5]
Example:
View
In this example, we create a view of the array and modify the original array. The view reflects the change:
Program:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
x = arr.view()
arr[0] = 42
print(arr) # Output: [42 2 3 4 5]
print(x) # Output: [42 2 3 4 5]
The view (x) shows the updated value from the original array.
Output:
Example:
Modifying the View
If we modify the view instead, the original array also changes:
Program:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
x = arr.view()
x[0] = 31
print(arr) # Output: [31 2 3 4 5]
print(x) # Output: [31 2 3 4 5]
In this case, updating the view also updates the original array.
Output:
[31 2 3 4 5]
[31 2 3 4 5]
Check If NumPy Array Owns Its Data
In NumPy, whether an array owns its data depends on how it was created.
Copies of an array own their data.
Views do not own their data; instead, they reference the original array.
How to Check Data Ownership:
Every NumPy array has a .base attribute:
If .base is None, the array owns its data.
If .base is not None, it points to the original array from which the data is derived.
Program:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
x = arr.copy() # Creates a copy (owns its data)
y = arr.view() # Creates a view (does not own its data)
print(x.base) # Output: None (owns data)
print(y.base) # Output: [1 2 3 4 5] (refers to original array)