Difference Between Copy and View Array in NumPy
Let's start by understanding the concepts of copying and viewing in NumPy arrays.
In NumPy, a copy of an array is a completely separate array object with its own data and memory.
On the other hand, a view of an array is a new array object that refers to the same data as the original array,
To explain this further, let's consider some examples:
1. Copying an array copy():
When you create a copy of an array, any changes made to the copied array will not affect the original array, and vice versa.
import numpy as np
# Creating an original array
original_array = np.array([1, 2, 3, 4, 5])
# Creating a copy of the original array
copied_array = original_array.copy()
# Modifying the copied array
copied_array[0] = 10
# Printing both arrays
print("Original array:", original_array) # Output: [1 2 3 4 5]
print("Copied array:", copied_array) # Output: [10 2 3 4 5]
As you can see, changing the value in the copied array does not affect the original array.
2. Creating a view of an array view():
When you create a view of an array, any changes made to the view will affect the original array, and vice versa.
import numpy as np
# Creating an original array
original_array = np.array([1, 2, 3, 4, 5])
# Creating a view of the original array
view_array = original_array.view()
# Modifying the view array
view_array[0] = 10
# Printing both arrays
print("Original array:", original_array) # Output: [10 2 3 4 5]
print("View array:", view_array) # Output: [10 2 3 4 5]
In this example, changing the value in the view array affects the original array as well.
3. Modifying shape with a view:
Creating a view can also involve modifying the shape of the array. This means the view will have a different number of elements, but it still refers to the same underlying data.
import numpy as np
# Creating an original array
original_array = np.array([1, 2, 3, 4, 5])
# Creating a view with a different shape
view_array = original_array.view()
view_array.shape = (5, 1)
# Printing both arrays
print("Original array:", original_array) # Output: [1 2 3 4 5]
print("View array:", view_array)
# Output:
# [[1]
# [2]
# [3]
# [4]
# [5]]
How to check if Array Owns its Data ?
To check if a NumPy array owns its data or if it is a view of another array, you can use the base attribute. The base attribute returns None for an array that owns its data, and it returns the original array from which the view was created.
Here's an example to illustrate how to check if an array owns its data:
import numpy as np
# Creating an array that owns its data
original_array = np.array([1, 2, 3, 4, 5])
print("Original array:")
print(original_array)
print("Base of original array:", original_array.base) # Output: None
# Creating a view of the original array
view_array = original_array.view()
print("
View array:")
print(view_array)
print("Base of view array:", view_array.base) # Output: [1 2 3 4 5]
# Modifying the view array
view_array[0] = 10
# Printing both arrays
print("
Original array after modification:")
print(original_array) # Output: [10 2 3 4 5]
print("View array after modification:")
print(view_array) # Output: [10 2 3 4 5]
print("Base of view array:", view_array.base) # Output: [10 2 3 4 5]