Copy & View Array In NumPy

Numpy array copy and view is discussed in this post with examples with the expectation that it will fulfill your learning requirements.



NumPy Array Copy:

Take the original array, generate a copy, modify it, and display it:

Example: 

import numpy as npy mrx = npy.array([0, 2, 4, 5, 8, 10]) ample = mrx.copy() mrx[3] = 6 print(mrx) print(ample)

NumPy Array Copy and view examples

Example: 

import numpy as npy mrx = npy.array(['bass', 'base', 'blew', 'blow', 'cell', 'sell', 'dear', 'deer']) ample = mrx.copy() mrx[3] = 'blue' print(mrx) print(ample)

Modifications to the original array MUST NOT impact the copy.


NumPy Array View:

Build a view, modify the original array, and present the two arrays:

Example: 

import numpy as npy mrx = npy.array([0, 2, 4, 5, 8, 10]) ample = mrx.view() mrx[3] = 6 print(mrx) print(ample)

 

Example: 

import numpy as npy mrx = npy.array(['bass', 'base', 'blew', 'blow', 'cell', 'sell', 'dear', 'deer']) ample = mrx.view() mrx[3] = 'blue' print(mrx) print(ample)

Numpy array copy and view must take into account the modifications made to the initial array.

Now create a view, modify the view, and present the two arrays:

Example: 

import numpy as npy mrx = npy.array([1, 3, 4, 7, 9, 11]) ample = mrx.view() ample[2] = 5 print(mrx) print(ample)

 

Example: 

import numpy as npy mrx = npy.array(['bass', 'base', 'blew', 'blue', 'cell', 'sall', 'dear', 'deer']) ample = mrx.view() ample[5] = 'sell' print(mrx) print(ample)
The initial array must be altered by the modifications made to the view since we are discussing Numpy array copy & view.

Verify That The Array Owns Its Data

Earlier, we discussed that copies hold the data, but how can we make sure that views keep the data?

NumPy arrays have a base property that provides None if the array holds data.

It points to the initial object if the base property is not set.

To find out if an array holds its data, display the value of the base property:

Example: 

import numpy as npy mrx_arr = npy.array([0, 2, 4, 6, 8, 10]) ample1 = mrx_arr.copy() ample2 = mrx_arr.view() print(ample1.base) print(ample2.base)

 

Example: 

import numpy as npy mrx_arr = npy.array(['bass', 'base', 'blew', 'blue', 'cell', 'sall', 'dear', 'deer']) ample1 = mrx_arr.copy() ample2 = mrx_arr.view() print(ample1.base) print(ample2.base)
The copy produces output as None. Original arrays are displayed by the view.

NumPy Array Copy and View Difference

The primary distinction between a Numpy array copy and a view is that a duplicate is a new array, whereas a view is simply a view of an existing array.

Any modifications made to the copy will not modify the primary array, and any alterations made to the primary array will not impact the copy.

Views do not hold data, and any modifications to them will alter the original array, and any modifications to the array will impact the view.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *