Copy of Array in NumPy
The copy is completely a new array and the copy owns the data.
You can create a copy of an array using the copy() function of the NumPy library.
This is also known as Deep Copy.
When we make changes to the copy it does not affect the original array, and when changes are made to the original array it does not affect the copy.
Example: Making a copy and changing the original array
python3
import numpy as np # creating array arr = np.array([ 2 , 4 , 6 , 8 , 10 ]) # creating copy of array c = arr.copy() # both arr and c have different id print ( "id of arr" , id (arr)) print ( "id of c" , id (c)) # changing original array # this will not effect copy arr[ 0 ] = 12 # printing array and copy print ( "original array- " , arr) print ( "copy- " , c) |
Output:
id of arr 35406048 id of c 32095936 original array- [12 4 6 8 10] copy- [ 2 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.