YouTip LogoYouTip

Numpy Copies And Views

# NumPy Copies and Views A copy is a complete copy of the data. If we modify the copy, it does not affect the original data, as they are not in the same physical memory location. A view is an alias or reference to the data. Through this alias or reference, we can access and operate on the original data, but the original data is not copied. If we modify the view, it will affect the original data, as they are in the same physical memory location. **Views generally occur when:** * 1. Slicing operations in NumPy return a view of the original data. * 2. Calling the `view()` function of an ndarray creates a view. **Copies generally occur when:** * Slicing operations on Python sequences, calling the `deepCopy()` function. * Calling the `copy()` function of an ndarray creates a copy. ### No Copy Simple assignment does not create a copy of the array object. Instead, it uses the same `id()` to access the original array. `id()` returns the unique identifier of a Python object, similar to a pointer in C. Furthermore, any change to one array is reflected in the other. For example, changing the shape of one array also changes the shape of the other. ## Example import numpy as np a = np.arange(6)print('Our array is:')print(a)print('Calling id() function:')print(id(a))print('Assigning a to b:')b = a print(b)print('b has the same id():')print(id(b))print('Modifying the shape of b:')b.shape = 3,2 print(b)print('Shape of a is also modified:')print(a) The output is: Our array is:Calling id() function:4349302224 Assigning a to b: b has the same id():4349302224Modifying the shape of b:[ ] Shape of a is also modified:[ ] ### View or Shallow Copy The `ndarray.view()` method creates a new array object. Changes to the dimensions of the new array created by this method do not affect the dimensions of the original data. ## Example import numpy as np# Initially, a is a 3X2 array a = np.arange(6).reshape(3,2)print('Array a:')print(a)print('Creating a view of a:')b = a.view()print(b)print('The id() of the two arrays are different:')print('id() of a:')print(id(a))print('id() of b:')print(id(b))# Modifying the shape of b does not modify a b.shape = 2,3 print('Shape of b:')print(b)print('Shape of a:')print(a) The output is: Array a:[ ]Creating a view of a:[ ]The id() of the two arrays are different: id() of a:4314786992 id() of b:4315171296 Shape of b:[ ] Shape of a:[ ] Creating a view using slicing and modifying the data will affect the original array: ## Example import numpy as np arr = np.arange(12)print('Our array:')print(arr)print('Creating a slice:')a=arr[3:]b=arr[3:]a=123 b=234 print(arr)print(id(a),id(b),id(arr[3:])) The output is: Our array:Creating a slice:4545878416 4545878496 4545878576 Variables `a` and `b` are both views of part of `arr`. Modifications to the view are directly reflected in the original data. However, we observe that the `id` of `a` and `b` are different, meaning that although the view points to the original data, it is different from an assignment reference. ### Copy or Deep Copy The `ndarray.copy()` function creates a copy. Modifying the copy does not affect the original data, as they are not in the same physical memory location. ## Example import numpy as np a = np.array([[10,10], [2,3], [4,5]])print('Array a:')print(a)print('Creating a deep copy of a:')b = a.copy()print('Array b:')print(b)# b and a do not share any content print('Can we write to a by writing to b?')print(b is a)print('Modifying the contents of b:')b[0,0] = 100 print('Modified array b:')print(b)print('a remains unchanged:')print(a) The output is: Array a:[ ]Creating a deep copy of a:Array b:[ ]Can we write to a by writing to b?FalseModifying the contents of b:Modified array b:[ ] a remains unchanged:[ ] ### More Related Articles [Analysis of Python Direct Assignment, Shallow Copy, and Deep Copy](#)
← Numpy Linear AlgebraNumpy Sort Search β†’