NumPy Copies and Views – Copy Vs View in NumPy

Free NumPy course with real-time projects Start Now!!

NumPy consists of different methods to duplicate an original array. The two main functions for this duplication are copy and view. The duplication of the array means an array assignment.

When we duplicate the original array, the changes made in the new array may or may not reflect. The duplicate array may use the same location or may be at a new memory location.

Copy or Deep copy in NumPy

It returns a copy of the original array stored at a new location. The copy doesn’t share data or memory with the original array. The modifications are not reflected. The copy function is also known as deep copy.

import numpy as np
arr = np.array([20,30,50,70])
a= arr.copy()
#changing a value in original array
arr[0] = 100
 
print(arr)
print(a)

Output

[100 30 50 70]
[20 30 50 70]

Changes made in the original array are not reflected in the copy.

import numpy as np
arr = np.array([20,30,50,70])
a= arr.copy()
#changing a value in copy array
a[0] = 5
 
print(arr)
print(a)

Output

[20 30 50 70]
[ 5 30 50 70]

Changes made in copy are not reflected in the original array

View or Shallow copy in NumPy

It returns a view of the original array stored at the existing location. The view doesn’t have its own data or memory but uses the original array. The modifications reflect in both. We also call the view function shallow copy.

import numpy as np
arr = np.array([20,30,50,70])
a= arr.view()
#changing a value in original array
arr[0] = 100
 
print(arr)
print(a)
 

Output

[100 30 50 70]
[100 30 50 70]

Changes made in the original array reflect in the view.

import numpy as np
arr = np.array([20,30,50,70])
a= arr.view()
#changing a value in the view 
a[0] = 80
 
print(arr)
print(a)

Output

[80 30 50 70]
[80 30 50 70]

Changes made in the view reflected in the original array.

No copy in NumPy

When we make assignments to an array it does not create a copy of the array. It actually accesses the original array through its id(). The id() element is equivalent to pointers in c /c++ language. They just point to the original array.

import numpy as np 
 
#declaring an array 
arr1 = np.arange(10) 
print(arr1)
 
#applying id() function
print (id(arr1))
 
#assign the array to another variable 
arr2 = arr1
print (arr2) 
 
#we print the id() for arr2
print( id(arr2)  )

Output

[0 1 2 3 4 5 6 7 8 9]
139825053003648
[0 1 2 3 4 5 6 7 8 9]
139825053003648

NumPy View Creation

We can create a view via two methods. We can either create it by slicing the original array or changing its data type.

NumPy Slice view

In this type of view creation, we perform slicing of the original array. We can then address the view by offsets, strides, and counts of the original array.

import numpy as np
arr= np.arange(10)
print(arr)
 
#slicing of original array to create a view
v=arr[1:10:2]
print(v)

Output

[0 1 2 3 4 5 6 7 8 9]
[1 3 5 7 9]

NumPy dtype view

In this case we change the data type of the original array and generate a view.

import numpy as np
arr= np.arange(10,dtype='int32')
print(arr)
 
#chanding datatype of original array to create a view
v=arr.view('int16')
print(v)

Output

[0 1 2 3 4 5 6 7 8 9]
[0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0]

Checking if NumPy Array owns its data

We know that copy owns its data while the view does not. We can check this with the base the attribute that returns a none value if array owns its data.

import numpy as np
 
arr = np.array([59,87,64])
 
a = arr.copy()
b = arr.view()
 
print(a.base)
print(b.base)
 

Output

None
[59 87 64]

Difference between NumPy Copy Vs View

The main highlight difference between a copy and view it in its memory location. The copy of an array is a new array. The view, on the other hand, is just a view of the original array.

We store the copy at a new memory location. A copy returns the data stored at the new location. The changes made in the copy data does not reflect in the original array. The copy of an array owns its own data.

The view is a view of the original array at the same memory location. A view returns the data stored at the original memory location. The changes made in the original data are reflected in the view and vice versa. The view of an array does not own its own data.

Summary

NumPy copy and view functions are very useful for array duplication. The difference in their properties helps utilize the duplication in a variety of ways. The changes in the original array can be monitored depending on the type of duplication function.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *