NumPy module में ndarray Object का copy() और view() दोनों methods का use array data को copy करने के लिए किया जाता है। लेकिन इन दोनों methods में एक major difference है।

NumPy difference between view() and copy()

copy() method का use करके copy किये गए data में अगर कोई changes होता है , तो original data पर कोई effect नहीं पड़ता है। means वो copied data original data से independent होता है।

NumPy copy() Example

Copy Fullscreen Close Fullscreen Run
#import numpy module.
import numpy as np
org_arr = np.array([10, 20, 30, 40, 50])
#now copy data and made some changes.
copy_arr = org_arr.copy()
copy_arr[0] = 60
print('Original Data : ', org_arr)
print('Copied Data : ', copy_arr)
Output
C:\Users\Rahulkumar\Desktop\python>python module_numpy.py
Original Data :  [10 20 30 40 50]
Copied Data :  [60 20 30 40 50]

Example में आप clearly देख सकते हैं , कि copied data में changes करने पर original data में कोई changes नहीं हुए हैं।

NumPy view() Example

जबकि view() method का use करके copy किये गए data में अगर कोई changes होता है , तो same changes original में भी होते हैं । या पूरी तरह से original data पर ही dependent होता है।
See Example :

Copy Fullscreen Close Fullscreen Run
import numpy as np
org_arr = np.array([10, 20, 30, 40, 50])
#now copy data and made some changes using view() mwthod.
copy_arr = org_arr.view()
copy_arr[0] = 60
print('Original Data : ', org_arr)
print('Copied Data : ', copy_arr))
Output
C:\Users\Rahulkumar\Desktop\python>python module_numpy.py
Original Data :  [60 20 30 40 50]
Copied Data :  [60 20 30 40 50]

Now you can see , view() method का use करके copy किये गए data में changes करने पर original data में भी changes हुए हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers