The Wayback Machine - https://web.archive.org/web/20240620233851/https://www.geeksforgeeks.org/numpy-copy-and-view/
Open In App

NumPy Copy and View of Array

Last Updated : 01 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
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.



Previous Article
Next Article

Similar Reads

copy in Python (Deep Copy and Shallow Copy)
In Python, Assignment statements do not copy objects, they create bindings between a target and an object. When we use the = operator, It only creates a new variable that shares the reference of the original object. In order to create "real copies" or "clones" of these objects, we can use the copy module in Python. Syntax of Python DeepcopySyntax:
5 min read
NumPy ndarray.copy() Method | Make Copy of a Array
The ndarray.copy() method returns a copy of the array. It is used to create a new array that is a copy of an existing array but does not share memory with it. This means that making any changes to the original array won't affect the existing array. Example C/C++ Code # Python program explaining # numpy.ndarray.copy() function import numpy as geek x
2 min read
How to Copy NumPy array into another array?
Many times there is a need to copy one array to another. Numpy provides the facility to copy array using different methods. In this Copy NumPy Array into Another ArrayThere are various ways to copies created in NumPy arrays in Python, here we are discussing some generally used methods for copies created in NumPy arrays those are following. Using np
3 min read
PyQtGraph – Getting View of Image View
In this article, we will see how we can get the view objects of the image view object in PyQTGraph. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.). W
3 min read
Copy a Python Dictionary and only Edit the Copy
Copying a dictionary in Python to preserve the original while making changes to the duplicate is a common task. Developers use methods like the `copy` module or the dictionary's `copy()` method to create an independent duplicate, allowing for isolated edits. This article explores commonly used techniques to copy a dictionary and modify only the dup
4 min read
NumPy ndarray.T | Get View of Transposed Array
The NumPy ndarray.T attribute finds the view of the transposed Array. It can transpose any array having a dimension greater than or equal to 2. It works similarly to the numpy.transpose() method but it is easy and concise to use. SyntaxSyntax: ndarray.T Returns Transpose of given arrayExamplesLet's look at how to use the ndarray.T attribute of the
1 min read
Difference Between Shallow copy VS Deep copy in Pandas Dataframes
The pandas library has mainly two data structures DataFrames and Series. These data structures are internally represented with index arrays, which label the data, and data arrays, which contain the actual data. Now, when we try to copy these data structures (DataFrames and Series) we essentially copy the object's indices and data and there are two
4 min read
Shallow copy vs Deep copy in Pandas Series
The pandas library has mainly two data structures DataFrames and Series. These data structures are internally represented with index arrays, which label the data, and data arrays, which contain the actual data. Now, when we try to copy these data structures (DataFrames and Series) we essentially copy the object's indices and data and there are two
4 min read
Python Dictionary Copy Vs Deep Copy
In Python, Dictionary like other non-primitive data structures can be copied using various methods. The key distinction lies in the depth of the copy. In this article, we are going to learn about deep copy and shallow copy in Python. What is Shallow Copy in Python Dictionary?In Python, a shallow copy creates a new object that references the same un
3 min read
NumPy Array Sorting | How to sort NumPy Array
Sorting an array is a very important step in data analysis as it helps in ordering data, and makes it easier to search and clean. In this tutorial, we will learn how to sort an array in NumPy. You can sort an array in NumPy: Using np.sort() functionin-line sortsorting along different axesUsing np.argsort() functionUsing np.lexsort() functionUsing s
4 min read
Practice Tags :