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 copy()
and 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.
-
Here the 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
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)
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.
Using numpy.ndarray.copy()
function:
This function basically returns the copy of the input array. The syntax to use this function is as follows:
numpy.ndarray.copy(order='C')
In the above syntax the order
parameter is taken.
The 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).
Example of numpy.ndarray.copy()
:
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(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)
The id of input array a :
3087432353792
The id of c is:
3087432353952
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.
Using numpy.ndarray.view()
function:
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:
ndarray.view(dtype=None, type=None)
Parameters:
Let us discuss the above mentioned parameters:
-
dtype
It indicates the data-type descriptor of the returned view, e.g., float32
or int16
. The default value is None
that results in the view having the same data-type as the given array.
-
type
It indicates the type of the returned view.
Example of numpy.ndarray.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(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)
The id of ar
3087432354752
The id of v
3087432354352
The Original array:
[ 2 4 6 16 10 12]
The view:
[ 2 4 6 16 10 12]
Summary
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.