NumPy Copy and View

NumPyNumPyBeginner
Practice Now

Introduction

In this tutorial, you will learn about the concepts of copy and view for ndarrays in the NumPy library. You will learn how to use the copy() and view() functions to create a new copy of an existing array or create a new view for the array. You will also learn the difference between copy and view and how they behave differently.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Understanding the Difference between Copy and View

The major difference between copy and view is that the copy() function creates a new array whereas the view() function creates a new view of the original array. When we create a new copy of the input array, it is stored at a different location in memory, but when we create a view, it points to the same memory location as the original array. This means that any changes made to the copy of the input array will not affect the original array, and vice versa. However, any changes made to the view will affect the original array, and vice versa.

No Copy or Array Assignment

If you assign a NumPy array to another array, it does not create a direct copy of the original array. Instead, it creates another array with the same content and ID, which becomes a reference to the original array. If you make any changes to this reference array, they are directly reflected in the original array.

import numpy as np

input_arr = np.array([[5,2,7,4],[9,0,2,3],[1,2,3,19]])
print("The Original Array is :\n")
print(input_arr)
print("\nThe ID of array a:")
print(id(input_arr))

b = input_arr #assigning input_arr to b
print("\nNow we make the copy of the input_arr")
print("\nThe ID of b:")
print(id(b))
b.shape = 4, 3 #making some changes to b
print("\nThe Changes on b also reflect to a:")
print(input_arr)

Numpy Copy or Deep Copy

When we create a copy using the copy() function, it is also known as deep copy. The copy of the array owns the data, which means that any changes made to the copy will not affect the original array, and vice versa.

To make a deep copy of the input array, we can use the numpy.ndarray.copy() function.

import numpy as np

## Let us create an array
a = np.array([5, 4, 6, 8, 9])

#Let us create the copy of input array
c = a.copy()

#Now let us check the id of a and c
print("The id of input array a:")
print(id(a))
print("The id of c is:")
print(id(c))

#Now changing the original array
a[0] = 25

## printing both input array and copy
print("The original array:")
print(a)
print("The copy is:")
print(c)

Numpy View or Shallow Copy

When we create a view of an array, it is also known as a shallow copy. The view only points to the original array and does not own the data. This means that any changes made to the view will affect the original array, and vice versa.

To create a view of the input array, we can use the numpy.ndarray.view() function.

import numpy as np

## given input array
ar = np.array([2, 4, 6, 8, 10, 12])

## creating the view
v = ar.view()

## Now both arr and v will have different id
print("The id of ar")
print(id(ar))
print("The id of v")
print(id(v))

## changing the original array will also effect view
ar[3] = 16

## printing both array and view
print("The original array:")
print(ar)
print("The view:")
print(v)

Summary

In this tutorial, you have learned about the concepts of copy and view for ndarrays in the NumPy library. You have learned how to use the copy() and view() functions to create a new copy of an existing array or create a new view for the array. You have also learned the difference between copy and view and how they behave differently. We recommend practicing these concepts with additional examples.

Other NumPy Tutorials you may like