Open In App

NumPy Copy and View of Array

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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 the view has the same memory location as the original array.

View of Array in NumPy

The view is just a view of the original ndarray and the view does not own the data. 

You can create an array view using the view() function of the NumPy library.

This is also known as Shallow Copy

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 the original array

Python3




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)


 Output:

id of arr 30480448
id of v 30677968
original array-  [12  4  6  8 10]
view-  [12  4  6  8 10]

Copy of Array in NumPy

The copy is completely a new array and the copy owns the data. 

You can create a copy of an array using the copy() function of the NumPy library.

This is also known as Deep Copy.

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 the original array

python3




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]

Assigning Array to Variable

Normal assignments do not make a copy of an array object. Instead, it uses the same ID of the original array to access it. 

Any changes in either get reflected in the other.

Example: Assigning Array to Variable and changing Variable

python3




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]

How to check if the Array is a View or a Copy

As we know, copy owns the data and view does not own the data. 

You can use the base attribute of ndarray to tell if the array is a copy or a view.  The base attribute returns None for a copy and returns the original array for a view.

You can also use the base attribute to tell if the array owns the data.

Example: Check if the array is a view or copy

python3




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]

From the above code we can see that object ‘c’ returned None meaning it is a copy and owns the data.

Object ‘v’ returns the original array meaning it is a view and does not own the data.

Conclusion

View and copy are two different ways of working with NumPy ndarray in Python. View provides a window into existing data without duplicating it but copy creates an independent copy of the array.

In this tutorial, we have covered view and copy in the NumPy array. We have also discussed the difference between copy and view. Both these methods are useful in different situations, carefully use either of them.



Last Updated : 01 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads