Copy and View in NumPy Array - GeeksforGeeks

28 Aug, 2020
While working with NumPy, you might have seen some functions return the copy whereas some functions return the view. The main difference between copy and view is that the copy is the new array whereas the view is the view of the original array. In other words, it can be said that the copy is physically stored at another location and view has the same memory location as the original array.
No Copy: Normal assignments do not make the copy of an array object. Instead, it uses the exact same id of the original array to access it. Further, any changes in either get reflected in the other.
import numpy as np  # creating arrayarr = np.array([2, 4, 6, 8, 10])  # assigning arr to ncnc = arr  # both arr and nc have same idprint("id of arr", id(arr))print("id of nc", id(nc))  # updating ncnc[0]= 12  # printing the valuesprint("original array- ", arr)print("assigned array- ", nc)
Output:
id of arr 26558736
id of nc 26558736
original array-  [12  4  6  8 10]
assigned array-  [12  4  6  8 10]

View: This is also known as Shallow Copy. The view is just a view of the original array and view does not own the data. When we make changes to the view it affects the original array, and when changes are made to the original array it affects the view.
Example: (making a view and changing original array)
import numpy as np  # creating arrayarr = np.array([2, 4, 6, 8, 10])  # creating view v = arr.view()  # both arr and v have different idprint("id of arr", id(arr))print("id of v", id(v))  # changing original array# will effect viewarr[0] = 12  # printing array and viewprint("original array- ", arr)print("view- ", v)
id of arr 30480448
id of v 30677968
original array-  [12  4  6  8 10]
view-  [12  4  6  8 10]

Copy: This is also known as Deep Copy. The copy is completely a new array and copy owns the data. When we make changes to the copy it does not affect the original array, and when changes are made to the original array it does not affect the copy.
Example: (making a copy and changing original array)
import numpy as np  # creating arrayarr = np.array([2, 4, 6, 8, 10])  # creating copy of arrayc = arr.copy()  # both arr and c have different idprint("id of arr", id(arr))print("id of c", id(c))  # changing original array# this will not effect copyarr[0] = 12  # printing array and copyprint("original array- ", arr)print("copy- ", c)
Output:
id of arr 35406048
id of c 32095936
original array-  [12  4  6  8 10]
copy-  [ 2  4  6  8 10]

Array Owning it’s Data:To check whether array own it’s data in view and copy we can use the fact that every NumPy array has the attribute base that returns None if the array owns the data. Else, the base attribute refers to the original object.Example:
import numpy as np  # creating arrayarr = np.array([2, 4, 6, 8, 10])  # creating copy of arrayc = arr.copy()  # creating view of arrayv = arr.view()  # printing base attribute of copy and viewprint(c.base)print(v.base)
Output:
None
[ 2  4  6  8 10]

Python numpy-arrayManipulation
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python
Defaultdict in Python
Python | Get unique values from a list
Python | Pandas dataframe.groupby()