What Is The Difference Between Copy And View Of An Array In Numpy In Python
What Is Copy Of An Array In Numpy In Python:
The copy of an array owns the data of the original array as the Copy Of the Array
What Is View Of An Array In Numpy In Python:
What is the diffrence between Copy and View
The main difference between a copy and a view of an array is that the copy is a new array just the replica of the main array, and the view is just a view of the original array.
As the copy owns the data and any changes made to the copy will not affect original array, and any changes made to the original array will not affect the copy.
As the view does not own the data and any changes made to the view will affect the original array, and any changes made to the original array will affect the view.
How To Make A Copy Of An Array In Numpy Python
Let's have alook at some examples of Copy function of the Array
Example
Make a copy, change the original array, and display both arrays:
import numpy as np
arr = np.array([6, 2, 9, 4, 7])
x = arr.copy()
arr[0] = 69
print(arr)
print(x)
The copy SHOULD NOT be affected by the changes made to the original array.
Output:
[69 2, 9, 4, 7]
[6 2 9 4 7]
Explanation:
So in the above example you can see first we are storing the values in the arr and then are making a copy of arr and keeping it in the variable x.
After that we are storing the value 69 at the 0th index of the array arr.But as x is the copy of the array so this modification will not impact the value of x.
Hence x is showing the old array content before the change and arr is showing the updated content after the change.
How To Make A View Of An Array In Numpy Python
Let's have a look at the example of a View Here:
Example
import numpy as np
arr = np.array([9, 8, 3, 4, 5])
x = arr.view()
arr[0] = 68
print(arr)
print(x)
The view SHOULD be affected by the changes made to the original array.
Output:
[68 8 3 4 5]
[68 8 3 4 5]
Explanation:
As view contains the structure of the array so after making the change in the array the updated values will only be shown by the View output of the array.
How To Make Changes in the VIEW
Example
Make a view, change the view, and display both arrays:
import numpy as np
arr = np.array([8, 9, 3, 4, 5])
x = arr.view()
x[0] = 36
print(arr)
print(x)
The original array SHOULD be affected by the changes made to the view.
Output:
[36 9 3 4 5]
[36 9 3 4 5]
Explanation:\
How To Check If The Copy or the View Of The Array Contains It's Own Data:
How we can check if the copy or view of the array contains the original array data or not.
In NumPy array there is a attribute base that returns None if the array owns the data.
Otherwise, the base attribute refers to the original object.
Example
Print the value of the base attribute to check if an array have it's data or not:
import numpy as np
arr = np.array([4, 6, 3, 9, 5])
x = arr.copy()
y = arr.view()
print(x.base)
print(y.base)
Output:
None
[4 6 3 9 5]
The copy returns None.
The view returns the original array's elements.
Comments
Post a Comment
souvikdutta.aec@gmail.com