Thursday, December 26, 2024
Google search engine
HomeLanguagesCopy and View in NumPy Array

Copy and View in NumPy Array

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.

Example: (No Copy by Assigning)




import numpy as np
  
# creating array
arr = np.array([2, 4, 6, 8, 10])
  
# assigning arr to nc
nc = arr
  
# both arr and nc have same id
print("id of arr", id(arr))
print("id of nc", id(nc))
  
# updating nc
nc[0]= 12
  
# printing the values
print("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 array
arr = np.array([2, 4, 6, 8, 10])
  
# creating view 
v = arr.view()
  
# both arr and v have different id
print("id of arr", id(arr))
print("id of v", id(v))
  
# changing original array
# will effect view
arr[0] = 12
  
# printing array and view
print("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 array
arr = np.array([2, 4, 6, 8, 10])
  
# creating copy of array
c = arr.copy()
  
# both arr and c have different id
print("id of arr", id(arr))
print("id of c", id(c))
  
# changing original array
# this will not effect copy
arr[0] = 12
  
# printing array and copy
print("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 array
arr = np.array([2, 4, 6, 8, 10])
  
# creating copy of array
c = arr.copy()
  
# creating view of array
v = arr.view()
  
# printing base attribute of copy and view
print(c.base)
print(v.base)


Output:

None
[ 2  4  6  8 10]

RELATED ARTICLES

Most Popular

Recent Comments