Here’s an example to illustrate the difference:
import numpy as np
# Create a random matrix
original_matrix = np.random.rand(3, 3)
# Create a subset (view) of the original matrix
subset_view = original_matrix[:2, :2]
# Modify a value in the subset
subset_view[0:1] = 99
# Check the original matrix
print(original_matrix)
[[99. 99. 0.70388917]
[ 0.43076191 0.13218762 0.29151863]
[ 0.33869492 0.86248627 0.16560777]]
In this example, modifying subset_view
also changes the corresponding value in original_matrix
. This is because subset_view
is a view of the original data, not a separate copy. It shares the same underlying data, and modifications to one affect the other.
To avoid this behavior and create a separate copy, you can use the copy
method:
import numpy as np
# Create a random matrix
original_matrix = np.random.rand(3, 3)
# Create a copy of the original matrix
subset_copy = original_matrix[:2, :2].copy()
# Modify a value in the subset copy
subset_copy[0, 0] = 99
# Check the original matrix
print(original_matrix)
Now, modifying subset_copy
doesn’t affect the original matrix because you’ve created an independent copy.
In summary, be aware of whether you are working with views or copies when manipulating NumPy arrays to prevent unintended modifications to the original data. You can use the copy
method explicitly when needed to ensure that you are working with a separate copy of the data.