VIEWS AND COPIES IN NUMPY

In this tutorial, we will learn about the views and copies in numpy. We will learn about no copy, shallow copy and deep copy in detail followed by a set of handy examples.





What are copies and views in Numpy?

Whenever we work with numpy array, we always return either a copy of the input array through applying a function or we return a view. The whole purpose of copies is to be stored in another location for the time being and if we have just a view of the same type of array that we are working with we call it view.

numpy-copies

No Copy

When we are assigning array variable to another variables then during that assignment array don’t not make a copy because both the variables will have the same ID and shape and if you make changes in one variable, it will automatically reflect in another variable. In order to check that let’s look at an example:

import numpy as np
arr = np.array([0,1,2,3,4,5,0,6,7,8,0,0])
print("arr is: ")
print(arr)

print("\n")
print("ID of arr is: ")
print(id(arr))

print("\n")
print("Assigning arr to arr2:")
arr2 = arr

print("arr2 is: ")
print(arr2)

print("\n")
print("ID of arr2 is: ")
print(id(arr2))

Output:

arr is: 
[0 1 2 3 4 5 0 6 7 8 0 0]

ID of arr is: 
4636185264

Assigning arr to arr2:
arr2 is: 
[0 1 2 3 4 5 0 6 7 8 0 0]

ID of arr2 is: 
4636185264




View or Shallow Copy in Numpy

The view() method in numpy is an nd array object that looks exactly like the original array but it is just working as a view of the original array, not the copy or the array itself. It is very different from assigning one array to the other where IDS and shapes remained the same even if changes were made. For Example:

import numpy as np
arr = np.array([0,1,2,3,4,5,0,6,7,8,0,0])
print("arr is: ")
print(arr)

print("\n")
print("ID of arr is: ")
print(id(arr))


print("\n")
print("Assigning arr view to arr2:")
arr2 = arr.view()

print("arr2 is: ")
print(arr2)

print("\n")
print("ID of arr2 is: ")
print(id(arr2))

Output:

arr is: 
[0 1 2 3 4 5 0 6 7 8 0 0]

ID of arr is: 
4636186464

Assigning arr view to arr2:
arr2 is: 
[0 1 2 3 4 5 0 6 7 8 0 0]

ID of arr2 is: 
4636061168

Deep Copy

Views and Copies in Numpy is the opposite of shallow copy since it doesn’t create just a view. The copy() method in numpy creates a deep copy. It is the real copy of the original array and acts as a separate entity, changes made in the copy of an array doesn’t reflect in the original array. For Example:

import numpy as np
arr = np.array([0,1,2,3,4,5,0,6,7,8,0,0])
print("arr is: ")
print(arr)

print("\n")
print("ID of arr is: ")
print(id(arr))


print("\n")
print("Assigning arr copy to arr2:")
arr2 = arr.copy()

print("arr2 is: ")
print(arr2)

print("\n")
print("ID of arr2 is: ")
print(id(arr2))

Output:

arr is: 
[0 1 2 3 4 5 0 6 7 8 0 0]

ID of arr is: 
4636187344

Assigning arr copy to arr2:
arr2 is: 
[0 1 2 3 4 5 0 6 7 8 0 0]

ID of arr2 is: 
4636187664

After modifying values in arr2, the two arrays will be:

arr2[0] = 100

print("After modifying values, arr is: ")
print(arr)

print("\n")
print("\n")

print("After modifying values, arr2 is: ")
print(arr2)

Output:

After modifying values, arr is: 
[0 1 2 3 4 5 0 6 7 8 0 0]

After modifying values, arr2 is: 
[100   1   2   3   4   5   0   6   7   8   0   0]