Assigning Array to Variable
Normal assignments do not make a copy of an array object. Instead, it uses the same ID of the original array to access it.
Any changes in either get reflected in the other.
Example: Assigning Array to Variable and changing Variable
python3
import numpy as np # creating array arr = np.array([ 2 , 4 , 6 , 8 , 10 ]) # assigning arr to nc nc = arr # both arr and nc have same id print ( "id of arr" , id (arr)) print ( "id of nc" , id (nc)) # updating nc nc[ 0 ] = 12 # printing the values print ( "original array- " , arr) print ( "assigned array- " , nc) |
Output:
id of arr 26558736 id of nc 26558736 original array- [12 4 6 8 10] assigned array- [12 4 6 8 10]
NumPy Copy and View of Array
While working with NumPy, you might have seen some functions return the copy whereas some functions return the view.
The main difference between copy and view is that the copy is the new array whereas the view is the view of the original array. In other words, it can be said that the copy is physically stored at another location and the view has the same memory location as the original array.
Contact Us