Difference Between Reference, View and Copy in NumPy

Machine Learning courses with 100+ Real-time projects Start Now!!

Program 1

# Differece between reference , view and copy in Numpy
import numpy as np

ar=np.array([10,20,30,40,50])
#Copy
ar_copy=ar.copy()
print("id of ar",id(ar))
print("id of ar_view",id(ar_copy))
print("Before change: ")
print(ar)
print(ar_copy)
print("After change: ")
ar[2]=900
print(ar)
print(ar_copy)
print("----------------------------")
print(ar.base)
print(ar_copy.base)

# View
# ar_view=ar.view()
# print("id of ar",id(ar))
# print("id of ar_view",id(ar_view))
# print("Before change: ")
# print(ar)
# print(ar_view)
# print("After change: ")
# ar[2]=900
# print(ar)
# print(ar_view)
# print("----------------------------")
# print(ar.base)
# print(ar_view.base)

#Reference 
# ar_ref=ar
# print("ar id: ",id(ar))
# print("ar_ref id: ",id(ar_ref))

# print("Before change: ")
# print(ar)
# print(ar_ref)
# print("After change: ")
# ar_ref[2]=700
# print(ar)
# print(ar_ref)
# print("----------------------------")
# print(ar.base)
# print(ar_ref.base)

 

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

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