لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

# Python Data Science NumPy Array Copy vs View

The Difference Between Copy and View

The main difference between a copy and a view of an array is that the copy is a new array, and the view is just a view of the original array.

The copy owns the data and any changes made to the copy will not affect original array, and any changes made to the original array will not affect the copy.

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.

COPY

Example 1: Make a copy, change the original array, and display both arrays.

Code

import numpy as np

arr = np.array([5, 7, 3, 4, 2])
x = arr.copy()
arr[0] = 45

print(arr)
print(x)

the output will be

[45 7 3 4 2]
[5 7 3 4 2]

Note: The copy SHOULD NOT be affected by the changes made to the original array.

Example 1-B: To insert more than 1 values in the original array, Make a copy, change the original array, and display both arrays.

Code

import numpy as np

arr = np.array([5, 7, 3, 4, 2])
x = arr.copy()
arr[0] = 45
arr[2] = 48

print(arr)
print(x)

the output will be

[45 7 48 4 2]
[5 7 3 4 2]

Note: As you can see we have inserted 45 at [0] index and 48 at index [2] position.

VIEW

Example 2: Make a view, change the original array, and display both arrays.

Code

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])

x = arr.view()

arr[0] = 45

print(arr)

print(x)

the output will be

[45 2 3 4 5 6]
[45 2 3 4 5 6]

Note: The view SHOULD be affected by the changes made to the original array.

Example 2 B: To insert more than one value in the array, Make a view, change the original array, and display both arrays.

Code

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])
x = arr.view()
arr[0] = 45
arr [5] = 49

print(arr)
print(x)

the output will be

[45 2 3 4 5 49]
[45 2 3 4 5 49]

Note: As you can see we have inserted 45 at [0] index and 49 at index [5] position.
The view SHOULD be affected by the changes made to the original array.

Make Changes in the VIEW

Example 3: Make a view, change the view, and display both arrays.

Code

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])
x = arr.view()
x[0] = 31

print(arr)
print(x)

the output will be

[31 2 3 4 5 6 7]
[31 2 3 4 5 6 7]

Note: The original array SHOULD be affected by the changes made to the view.

How to Check if Array Owns its Data?

As mentioned above, copies owns the data, and views does not own the data.

How to check this?

Every NumPy array has the attribute base that returns None if the array owns the data.

Otherwise, the base attribute refers to the original object.

Example 4: Print the value of the base attribute to check if an array owns it's data or not. Code

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

x = arr.copy()
y = arr.view()

print(x.base)
print(y.base)

the output will be

None
[1 2 3 4 5 6 7]

Note: The copy returns None.

The view returns the original array.