When dealing with NumPy arrays, there are two main types of copies: shallow copies and deep copies.
A shallow copy, also known as a view, shares the same underlying data buffer with the original array. This means that changes made to the view will affect the original array, and vice versa. A view is essentially a new way of looking at the same data. It has its own shape, strides, and other metadata, but it points to the same memory location as the original array.
A deep copy creates a completely independent copy of the original array. The new array has its own data buffer, so changes made to the copy do not affect the original array, and vice versa.
You can create a view of a NumPy array in several ways. One common way is by slicing an array.
import numpy as np
# Create an original array
original_array = np.array([1, 2, 3, 4, 5])
# Create a view by slicing
view_array = original_array[1:3]
print("Original Array:", original_array)
print("View Array:", view_array)
# Modify the view
view_array[0] = 10
print("After modifying view:")
print("Original Array:", original_array)
print("View Array:", view_array)
In this code, we first create an original array. Then we create a view of the original array by slicing it. When we modify the view, the original array is also affected because they share the same underlying data.
To create a deep copy of a NumPy array, you can use the copy()
method.
import numpy as np
# Create an original array
original_array = np.array([1, 2, 3, 4, 5])
# Create a deep copy
deep_copy_array = original_array.copy()
print("Original Array:", original_array)
print("Deep Copy Array:", deep_copy_array)
# Modify the deep copy
deep_copy_array[0] = 10
print("After modifying deep copy:")
print("Original Array:", original_array)
print("Deep Copy Array:", deep_copy_array)
In this example, we create a deep - copy of the original array using the copy()
method. When we modify the deep - copy array, the original array remains unchanged.
import numpy as np
# Create a large array
large_array = np.arange(1000000)
# Create a view of a subset
view_subset = large_array[100:200]
import numpy as np
original = np.array([[1, 2], [3, 4]])
copy = original.copy()
# Do some operations on copy that won't affect original
import numpy as np
# Create a view for memory - efficient access to a subset
view_sub = large_array[10:20]
# Create a deep copy to preserve original data
copy_array = original_array.copy()
Making copies of NumPy arrays, whether through views (shallow copies) or deep copies, is an important aspect of working with arrays in NumPy. Views are memory - efficient and useful when you want to access a subset of an array without consuming extra memory, but they share the underlying data with the original array. Deep copies, on the other hand, create independent arrays, which is beneficial when you need to preserve the original data. By understanding the fundamental concepts, usage methods, common practices, and best practices presented in this blog, readers should be able to efficiently create and manage copies of NumPy arrays in their projects.