Numpy - View or shallow copy - Deep Copy
The provided Python code snippet illustrates various concepts related to NumPy arrays, including array views, slicing, and deep copying. Here's a breakdown of the code:
Array Views:
- The 'view()' method creates a new array object 'c' that shares the same data as array 'a'.
- 'c is a' returns False, indicating that 'c' and 'a' are different array objects.
- 'c.base is a' returns True, showing that 'c' is a view of the data owned by 'a'.
- 'c.flags.owndata' returns False, indicating that 'c' does not own its data.
Reshaping and Data Modification:
- 'c = c.reshape((2, 6))' reshapes 'c', but the shape of 'a' remains unchanged.
- Modifying an element of 'c' ('c[0, 4] = 1234') also changes the corresponding element in 'a'.
Slicing and Views:
- Slicing 'a' to create 's' ('s = a[:, 1:3]') returns a view of the sliced portion.
- Modifying 's' modifies the corresponding elements in 'a' as well.
Deep Copy:
- The 'copy()' method creates a complete copy of the array and its data.
- 'd = a.copy()' creates a new array 'd' with its own data, independent of 'a'.
- 'd is a' and 'd.base is a' both return False, indicating 'd' is a separate array.
Best Practices:
- It's recommended to use 'copy()' after slicing if the original array is no longer needed to release memory.
- For example, if 'a' is a large intermediate result and 'b' only contains a small fraction of 'a', a deep copy should be made when constructing 'b' with slicing.
Overall, the code showcases how to create array views, perform slicing operations, deep copy arrays, and emphasizes best practices for memory management when working with large arrays in NumPy.
View or shallow copy
Different array objects can share the same data. The view method creates a new array object that looks at the same data.
>>> c = a.view()
>>> c is a
False
>>> c.base is a # c is a view of the data owned by a
True
>>> c.flags.owndata
False
>>>
>>> c = c.reshape((2, 6)) # a's shape doesn't change
>>> a.shape
(3, 4)
>>> c[0, 4] = 1234 # a's data changes
>>> a
array([[ 0, 1, 2, 3],
[1234, 5, 6, 7],
[ 8, 9, 10, 11]])
Slicing an array returns a view of it:
>>> s = a[:, 1:3]
>>> s[:] = 10 # s[:] is a view of s. Note the difference between s = 10 and s[:] = 10
>>> a
array([[ 0, 10, 10, 3],
[1234, 10, 10, 7],
[ 8, 10, 10, 11]])
Deep copy
The copy method makes a complete copy of the array and its data.
>>> d = a.copy() # a new array object with new data is created
>>> d is a
False
>>> d.base is a # d doesn't share anything with a
False
>>> d[0, 0] = 9999
>>> a
array([[ 0, 10, 10, 3],
[1234, 10, 10, 7],
[ 8, 10, 10, 11]])
Sometimes copy should be called after slicing if the original array is not required anymore. For example, suppose a is a huge intermediate result and the final result b only contains a small fraction of a, a deep copy should be made when constructing b with slicing:
>>> a = np.arange(int(1e8))
>>> b = a[:100].copy()
>>> del a # the memory of ``a`` can be released.
If b = a[:100] is used instead, a is referenced by b and will persist in memory even if del a is executed.
Comments
Post a Comment