View of Array in NumPy
The view is just a view of the original ndarray and the view does not own the data.
You can create an array view using the view() function of the NumPy library.
This is also known as Shallow Copy.
When we make changes to the view it affects the original array, and when changes are made to the original array it affects the view.
Example: Making a view and changing the original array
Python3
import numpy as np # creating array arr = np.array([ 2 , 4 , 6 , 8 , 10 ]) # creating view v = arr.view() # both arr and v have different id print ( "id of arr" , id (arr)) print ( "id of v" , id (v)) # changing original array # will effect view arr[ 0 ] = 12 # printing array and view print ( "original array- " , arr) print ( "view- " , v) |
Output:
id of arr 30480448 id of v 30677968 original array- [12 4 6 8 10] view- [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.