While executing the functions, some of them return a copy of the input array, while some return the view. When the contents are physically stored in another location, it is called Copy. If on the other hand, a different view of the same memory content is provided, we call it as View.
Simple assignments do not make the copy of array object. Instead, it uses the same id() of the original array to access it. The id() returns a universal identifier of Python object, similar to the pointer in C.
Furthermore, any changes in either gets reflected in the other. For example, the changing shape of one will change the shape of the other too.
import numpy as np
a = np.arange(6)
print ('Our array is:')
print (a)
print ('\n')
print ('Applying id() function:')
print (id(a))
print ('\n')
print ('a is assigned to b:')
b = a
print (b)
print ('\n')
print ('b has same id():')
print (id(b))
print ('\n')
print ('Change shape of b:')
b.shape = 3,2
print (b)
print ('\n')
print ('Shape of a also gets changed:')
print (a)
print ('\n')
NumPy has ndarray.view() method which is a new array object that looks at the same data of the original array. Unlike the earlier case, change in dimensions of the new array doesn’t change dimensions of the original.
import numpy as np
# To begin with, a is 3X2 array
a = np.arange(6).reshape(3,2)
print ('Array a:')
print (a)
print ('\n')
print ('Create view of a:')
b = a.view()
print (b)
print ('\n')
print ('id() for both the arrays are different:')
print ('id() of a:')
print (id(a))
print ('\n')
print ('id() of b:')
print (id(b))
print ('\n')
# Change the shape of b. It does not change the shape of a
b.shape = 2,3
print ('Shape of b:')
print (b)
print ('\n')
print ('Shape of a:')
print (a)
print ('\n')
import numpy as np
a = np.array([[10,10], [2,3], [4,5]])
print ('Our array is:')
print (a)
print ('\n')
print ('Create a slice:')
s = a[:, :2]
print (s)
print ('\n')
The ndarray.copy() function creates a deep copy. It is a complete copy of the array and its data, and doesn’t share with the original array.
import numpy as np
a = np.array([[10,10], [2,3], [4,5]])
print ('Array a is:')
print (a)
print ('\n')
print ('Create a deep copy of a:')
b = a.copy()
print ('Array b is:' )
print (b)
print ('\n')
#b does not share any memory of a
print ('Can we write b is a')
print (b is a)
print ('\n')
print ('Change the contents of b:')
b[0,0] = 100
print ('\n')
print ('Modified array b:')
print (b)
print ('\n')
print ('a remains unchanged:')
print (a)
print ('\n')
If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.
When going through coding examples, it's quite common to have doubts and errors.
If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.
You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.
If you want to