HOW TO CREATE VIEW OF PYTHON NUMPY ARRAY?

View is just a view of the original array, it can be created by view() method and main property of view is that the view does not own the data and any changes made to the view will affect the original array, and any changes made to the original array will affect the view.

import numpy as np

arr = np.array([1, 2, 3, 4]) #Original array
x = arr.view()               #View array 
print(arr) #Original array
print(x)   #View Array

arr[0] = 9 #changing the original array

print(arr)
print(x) #by changing original array view array will change

x[0] = 7 #changing the view array

print(arr) #by changing view array original array will change
print(x)

Output-

[1 2 3 4]
[1 2 3 4]
[9 2 3 4]
[9 2 3 4]
[7 2 3 4]
[7 2 3 4]


Posted

in

Tags:

Comments

Leave a Reply