In NumPy, a Copy and a View are two different ways to work with an existing array. Both can create a new array, but they handle the original data differently. Understanding this difference helps you avoid unwanted changes when working with array data.
The main difference between NumPy copy vs view is that a copy creates a new and independent array, whereas a view provides access to the original array without creating a separate copy of the data.
1. What is a Copy in NumPy?
A Copy creates a completely separate array with its own data. If you change the copied array, the original array remains unchanged.
Use the copy() method to create a copy.
Example:
import numpy as np
arr = np.array([10, 20, 30, 40])
new_arr = arr.copy()
new_arr[0] = 100
print("Original Array:", arr)
print("Copied Array:", new_arr)
Output:
Original Array: [10 20 30 40]
Copied Array: [100 20 30 40]
Explanation:
arr.copy()creates a separate array.- Changing
new_arrdoes not affectarr. - Both arrays have their own data.
2. What is a View in NumPy?
A View creates a new array that looks at the same data as the original array. It does not create a separate copy of the data.
Use the view() method to create a view.
Example:
import numpy as np
arr = np.array([10, 20, 30, 40])
new_arr = arr.view()
new_arr[0] = 100
print("Original Array:", arr)
print("View Array:", new_arr)
Output:
Original Array: [100 20 30 40]
View Array: [100 20 30 40]
Explanation:
arr.view()creates a view of the original array.- Both arrays share the same underlying data.
- Changing an element through the view also changes the original array.
3. Difference Between Copy and View
The main difference is how each one handles the original array’s data.
| Copy | View |
|---|---|
| Creates a separate copy of the data | Shares data with the original array |
| Changes do not affect the original array | Changes can affect the original array |
Uses copy() | Uses view() |
| Requires separate data storage | Usually does not duplicate the array data |
4. How to Check Whether an Array is a Copy or View?
NumPy provides the base attribute to check whether an array is based on another array.
- If
basereturnsNone, the array owns its data. - If
basereturns another array, it is a view of that array.
Example:
import numpy as np
arr = np.array([10, 20, 30, 40])
copy_arr = arr.copy()
view_arr = arr.view()
print(copy_arr.base)
print(view_arr.base)
Output:
None
[10 20 30 40]
Explanation:
copy_arr.basereturnsNonebecause the copy owns its data.view_arr.baserefers to the original array because the view shares its data.
5. Changes in Original Array: Copy vs View
Let’s understand what happens when we change the original array after creating a copy and a view.
Example:
import numpy as np
arr = np.array([10, 20, 30, 40])
copy_arr = arr.copy()
view_arr = arr.view()
arr[1] = 200
print("Original Array:", arr)
print("Copy Array:", copy_arr)
print("View Array:", view_arr)
Output:
Original Array: [ 10 200 30 40]
Copy Array: [10 20 30 40]
View Array: [ 10 200 30 40]
Explanation:
- Changing the original array affects the view because they share data.
- The copied array remains unchanged because it has separate data.
Student Task
Create a NumPy array containing the marks of five students.
import numpy as np
marks = np.array([75, 80, 65, 90, 85])
Complete these tasks:
- Create a copy of the
marksarray. - Create a view of the
marksarray. - Change the first value of the copy to
95. - Change the second value of the view to
88. - Print all three arrays and observe the differences.
Final Student Challenge: Employee Salary Data
A company stores the monthly salaries of its employees in a NumPy array.
import numpy as np
salary = np.array([25000, 30000, 35000, 40000])
# Create a copy
salary_copy = salary.copy()
# Create a view
salary_view = salary.view()
# Update the copied array
salary_copy[0] = 28000
# Update the view array
salary_view[1] = 32000
print("Original Salary:", salary)
print("Copied Salary:", salary_copy)
print("View Salary:", salary_view)
Expected Output:
Original Salary: [25000 32000 35000 40000]
Copied Salary: [28000 30000 35000 40000]
View Salary: [25000 32000 35000 40000]
Your Challenge:
- Create another copy of the salary array.
- Create another view of the salary array.
- Change the last employee’s salary through the copy.
- Change the third employee’s salary through the view.
- Check which changes appear in the original array.
Key Takeaways
- NumPy
copy()Method creates a separate array with independent data. view()creates an array that shares data with the original.- Changes in a copy do not affect the original array.
- Changes through a view can affect the original array.
- The
baseattribute helps identify whether an array is based on another array.
Example: new_arr = arr.view()
Frequently Asked Questions (FAQs)
1. What is the difference between NumPy Copy vs View in Python?
The main difference between NumPy Copy vs View is that a copy creates a new and independent array, whereas a view shares the underlying data of the original array. Changes made to a view can affect the original array, but changes to a copy do not.
2. What is the NumPy copy() method used for?
The NumPy copy() method is used to create a separate copy of an existing NumPy Array. It stores data independently, so modifications to the copied array do not affect the original array.
3. What is the NumPy view() method in Python?
The NumPy view() method creates a new array object that shares the underlying data with the original array. It allows you to access and work with the same data without creating an independent data copy.
4. How do you create a NumPy Array Copy?
You can create a NumPy Array Copy using the copy() method. It creates an independent array with its own data storage.
Example: new_arr = arr.copy()
5. How do you create a NumPy Array View?
You can create a NumPy Array View using the view() method. The new array object shares the underlying data with the original array.
Example: new_arr = arr.view()
6. Does modifying a NumPy View change the original array?
Yes, modifying elements through a NumPy View generally changes the original array because both arrays share the same underlying data.
7. Does a NumPy Copy affect the original array when modified?
No, a NumPy Copy does not affect the original array when its elements are modified because the copied array has independent data storage.
8. How can you identify Copy and View in Python using NumPy?
You can use the base attribute to identify whether a NumPy Array is based on another array. If arr.base returns None, the array owns its data. If it returns another object, the array is based on shared data.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
