Skip to content
Related Articles

Related Articles

Copy and View in NumPy Array
  • Last Updated : 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.

Example: (No Copy by Assigning)

filter_none

edit
close

play_arrow

link
brightness_4
code

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)

chevron_right


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)

filter_none

edit
close

play_arrow

link
brightness_4
code

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)

chevron_right


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)

filter_none

edit
close

play_arrow

link
brightness_4
code

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)

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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)

chevron_right


Output:

None
[ 2  4  6  8 10]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :