NumPy Array Copy Vs View (Python Tutorial)

NumPy Array Copy & View

This is a detailed tutorial of the NumPy Array Copy and View. Find out the difference between both of these with the help of illustrative examples.

Copy

When we try to duplicate some data in the NumPy we usually have to manipulate data in every way possible. During this manipulation of data, we usually use this method of copying things. When we try to copy we make a new file with the same data so that we can make changes in the new copy. With the help of this, no change will occur in the original copy of the data.

A copy of the data owns the data present in that copy and has no link to the original copy of the data. As a result, we can make any number of changes in the original copy it will have no effect on the version of copies of similar data.

When we copy arrays the new copy of the array is present at some other location in the memory. We need some extra space in order to create the copies of the arrays.

Let us go through an example of a copy:

#First we import numpy package with an alias as np
import numpy as np
#We will create an array
array1=np.array([9,8,7,6,5])
#Now we will create a copy of the array
a=array1.copy()
# We will make some change to the original array
array1[1]=3
#Then we will be printing the data of the array
print(array1)
print(a)

Output:

[9 3 7 6 5]
[9 8 7 6 5]

Now we see that first, we create a copy and then we make changes to the original copy. And also no changes will occur in the copy of the array as we make changes in the original array.

Let us go through another example for a Copy:

#First we import numpy package with an alias as np 
import numpy as np 
#We will create an array 
array1=np.array([9,8,7,6,5]) 
array2=np.array([5,4,3,2,9]) 
#Now we will create a copy of the array 
a=array1.copy() 
b=array2.copy() 
# We will make some change to the original array 
array1[1]=3 
array2[4]=1 
#Then we will be printing the data of the array 
print('original array1=',array1) 
print('original array2=',array2) 
print('copy of array1=',a) 
print('copy of array2=',b)

Output:

original array1= [9 3 7 6 5]
original array2= [5 4 3 2 1]
copy of array1= [9 8 7 6 5]
copy of array2= [5 4 3 2 9]

So we get clear that all the changes are made in the original copy, not in the array which we are copying.

View

Creating a view of the data is like viewing the original set of data. It is just the original view of the NumPy array. When we try to create view we are actually looking at the same original file so we try making any changes those changes will be saved in the original file.

A view of data does not own the data. It is a representation of the actual data.  Any changes made in the original file will create change in the view of the file.

When we create a view there is no use of no extra memory it will use the same memory which it is already using. So no extra memory is required for creating a view.

Lets us go through an example for view:

#First we import numpy package with an alias as np 
import numpy as np 
#We will create an array 
array1=np.array([9,8,7,6,5]) 
#Now we will create a view of the array 
a=array1.view() 
# We will make some change to the original array 
array1[4]=3 
#Then we will be printing the data of the array 
print(array1) 
print(a)

Output:

[9 8 7 6 3]
[9 8 7 6 3]

Any changes we make in the original array should automatically have some changes in the view file.

Let us take another example we create a 2-D array and then create a view:

#First we import numpy package with an alias as np 
import numpy as np 
#We will create an 2-D array 
array1=np.array([[9,8,7,6,5],[1,2,3,4,5]]) 
#Now we will create a view of the array 
a=array1.view() 
# We will make some change to the original array 
array1[0,1]=3 
#Then we will be printing the data of the array 
print('Original Array=',array1) 
print('View Array=',a)

Output:

Original Array= [[9 3 7 6 5]
 [1 2 3 4 5]]
View Array= [[9 3 7 6 5]
 [1 2 3 4 5]]

Here we created a view of the 2-D array and we saw that whatever changes we make to the original array automatically create changes in the view.

Let us take another example where we will make a change in the view:

#First we import numpy package with an alias as np 
import numpy as np 
#We will create an array 
array1=np.array([9,8,7,6,5]) 
#Now we will create a view of the array 
a=array1.view() 
# We will make some change to the view array 
a[1]=3 
#Then we will be printing the data of the array 
print('Original Array=',array1) 
print('View Array=',a)

Output:

Original Array= [9 3 7 6 5]
View Array= [9 3 7 6 5]

Here we see that any change we make to the view array automatically affects the original array.

Check the Who Owns the Data

As we know that copy owns the data and it has completely separate space in the memory allocated to them. And in case of View, the view we create of the original file will not own any of the data so any changes made in the view or the original copy will have an effect on each other simultaneously.

In NumPy, we have an attribute that checks if the array owns its data. Also which help of this, we can check if it is a View or Copy. The base Attribute helps us identify if the array owns its data or not.

Let us go through an example to have proper explanations:

#First we import numpy package with an alias as np 
import numpy as np 
#We will create an array 
array1=np.array([9,8,7,6,5]) 
#Now we will create a view  and a copy of the array 
a=array1.view() 
b=array1.copy()
#Then we will be printing the data of the array and also using base attribute.
print(b.base) 
print(a.base)

Output:

None
[9 8 7 6 5]

Here we look through the results and see that it is displaying none for the copy and the exact array for the view. So when it is returning none it means that the array owns the data. And if it is returning the array itself it means the array is not owning the data in it.

Hence copy will always return None if we use the base attribute. And the view will always return the original array whenever we use the base attribute.

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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