Updated On : Jul-22,2020 Time Investment : ~10 mins

Learning Numpy - Simple Tutorial For Beginners - NumPy Copies & Views - Part 19

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.

No Copy

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')
Our array is:
[0 1 2 3 4 5]


Applying id() function:
139825477915392


a is assigned to b:
[0 1 2 3 4 5]


b has same id():
139825477915392


Change shape of b:
[[0 1]
 [2 3]
 [4 5]]


Shape of a also gets changed:
[[0 1]
 [2 3]
 [4 5]]


View or Shallow Copy

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')
Array a:
[[0 1]
 [2 3]
 [4 5]]


Create view of a:
[[0 1]
 [2 3]
 [4 5]]


id() for both the arrays are different:
id() of a:
139825054061152


id() of b:
139825054061632


Shape of b:
[[0 1 2]
 [3 4 5]]


Shape of a:
[[0 1]
 [2 3]
 [4 5]]


Example 2

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')
Our array is:
[[10 10]
 [ 2  3]
 [ 4  5]]


Create a slice:
[[10 10]
 [ 2  3]
 [ 4  5]]


Deep Copy

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')
Array a is:
[[10 10]
 [ 2  3]
 [ 4  5]]


Create a deep copy of a:
Array b is:
[[10 10]
 [ 2  3]
 [ 4  5]]


Can we write b is a
False


Change the contents of b:


Modified array b:
[[100  10]
 [  2   3]
 [  4   5]]


a remains unchanged:
[[10 10]
 [ 2  3]
 [ 4  5]]


Dolly Solanki  Dolly Solanki

YouTube Subscribe Comfortable Learning through Video Tutorials?

If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.

Need Help Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

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.

Share Views Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.


Subscribe to Our YouTube Channel

YouTube SubScribe

Newsletter Subscription