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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/ArrayBasicsGroup -.-> numpy/1d_array("`1D Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/multi_array("`Multi-dimensional Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/ArrayBasicsGroup -.-> numpy/shape_dim("`Shapes and Dimensions`") numpy/IndexingandSlicingGroup -.-> numpy/basic_idx("`Basic Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/comments -.-> lab-86421{{"`NumPy Copy and View`"}} python/lists -.-> lab-86421{{"`NumPy Copy and View`"}} python/tuples -.-> lab-86421{{"`NumPy Copy and View`"}} python/importing_modules -.-> lab-86421{{"`NumPy Copy and View`"}} python/numerical_computing -.-> lab-86421{{"`NumPy Copy and View`"}} python/build_in_functions -.-> lab-86421{{"`NumPy Copy and View`"}} numpy/1d_array -.-> lab-86421{{"`NumPy Copy and View`"}} numpy/multi_array -.-> lab-86421{{"`NumPy Copy and View`"}} numpy/data_array -.-> lab-86421{{"`NumPy Copy and View`"}} numpy/shape_dim -.-> lab-86421{{"`NumPy Copy and View`"}} numpy/basic_idx -.-> lab-86421{{"`NumPy Copy and View`"}} numpy/bool_idx -.-> lab-86421{{"`NumPy Copy and View`"}} numpy/fancy_idx -.-> lab-86421{{"`NumPy Copy and View`"}} end

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