Python NumPy Copy and View
In this tutorial, we will cover the concept of copy and view, for ndarray in the NumPy library.
In Numpy, using the
view() functions, we can create a new copy of any existing array or create a new view for the array.
Let us take a look at what is the major 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. It is important to note here that physically the copy of an input array is stored at some other location whereas in the case of view, the different view of the same memory location is returned.
In layman terms it can be said that the copy is just physically stored at another location and view has the same memory location as the original array but a different represenation.
So it is important to note here that the copy object owns the data and whenever there are any changes made to the copy of the input array then it will not affect the original array and likewise any changes made to the original array will not affect the copy of the array.
On the other hand, the view does not owns the data and if there are any changes made to the view then it will surely affect the original array, and any changes made to the original array will affect the view.
No Copy or Array Assignment
If you make the assignment of a numpy array to another then it does not create a direct copy of the original array, rather it makes another array having same content and same id. Thus it becomes the reference to the original array.
If you will majke any changes to this reference array then these changes are directly reflected in the original array.
id() function is used that mainly returns the universal identifier of the array similar to the pointer in C.
Hence, we can say, if we just use a simple assignment operator, then only a reference is created and not an actual new copy.
Let us have a look at an example for the same:
import numpy as np
print("The Original Array is :\n")
print("\nThe ID of array a:")
b = input_arr#assigning input_arr to b
print("\nNow we make the copy of the input_arr")
print("\nThe ID of b:")
b.shape = 4,3; #making some changes to b
print("\nThe Changes on b also reflect to a:")
Here is the output of the above code:
Numpy Copy or Deep Copy
The copy of any array is basically a new array and when we create a copy using the
copy() function it is also known as Deep Copy.
We have already told you that copy of an array, owns the data.
So whenever we will make any changes to the copy then it will not affect the original array
Likewise, when changes are made to the original array then it does not affect the copy.
To return the copy of the input array, the
numpy.ndarray.copy() function is used.
This function basically returns the copy of the input array. The syntax to use this function is as follows:
In the above syntax the
order parameter is taken.
order parameter is mainly used to control the memory layout of the copy. Here the C means C-order and F means F-order, A also means F if the given array is Fortran contiguous, C otherwise. K means(to match the layout of the given array as closely as possible).
Now in the example given below we will make the copy of the input array and then make changes to the input array and then check what it returns:
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("The id of c is:")
#Now changing the original array
a = 25
# printing both input array and copy
print("The original array:")
print("The copy is:")
The id of input array a :
The id of c is:
The original array:
[25 4 6 8 9]
The copy is:
[5 4 6 8 9]
Numpy View or Shallow Copy
The view is only just a view of the original array.
When we create a view of any given array it is also known as the Shallow Copy.
Unlike copy, view does not owns the data.
It implies that if we make any changes to the view then it affects the original array similarly when we make changes to the original array then it affects the view.
- To return the view of the input array, the
numpy.ndarray.view() function is used.
This function is mainly used to get a new view of any given array with the same data. The syntax to use this function is as follows:
Let us discuss the above mentioned parameters:
It indicates the data-type descriptor of the returned view, e.g.,
int16. The default value is
None that results in the view having the same data-type as the given array.
It indicates the type of the returned view.
Now in the example given below we will make the view of the input array and then make changes to the input array and then check what it returns:
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("The id of v")
# changing the original array will also effect view
ar = 16
# printing both array and view
print("The Original array:")
The id of ar
The id of v
The Original array:
[ 2 4 6 16 10 12]
[ 2 4 6 16 10 12]
This tutorial is all about creation of different copies and views of Numpy ndarray, and how they behave differently. We have covered No Copy, Deep Copy and shallow copy along with their functions and we have provided the code examples for the same. We would recommend to do some more practice and run few more examples to understand these concepts completely.