Skip to Content
ModulesNumpy TutorialCopying vs Viewing Arrays

Copying vs Viewing Arrays in NumPy

When working with NumPy arrays, it’s crucial to understand whether a new operation creates a copy or a view of the array. This distinction determines whether modifying one affects the other.


Copy vs View: Key Differences

AspectCopyView
DefinitionCreates a completely new array with its own data.References the original array; shares data.
Data IndependenceIndependent of the original array.Changes in the view affect the original.
Memory UsageRequires additional memory for the new array.Shares the same memory as the original.
Use CaseUse when the original array must remain unchanged.Use for efficient slicing or temporary edits.

Creating a Copy

A copy creates a completely new array with its own data. Changes made to the copy do not affect the original array.

Example: Using copy()

import numpy as np # Original array array = np.array([1, 2, 3, 4, 5]) # Create a copy array_copy = array.copy() # Modify the copy array_copy[0] = 99 print("Original array:", array) # Output: [1 2 3 4 5] print("Modified copy:", array_copy) # Output: [99 2 3 4 5]

Output:

Original array: [1 2 3 4 5] Modified copy: [99 2 3 4 5]

Creating a View

A view is a reference to the original array. Changes made to the view also reflect in the original array.

Example: Using Slicing (View)

# Original array array = np.array([1, 2, 3, 4, 5]) # Create a view array_view = array[1:4] # Modify the view array_view[0] = 99 print("Original array:", array) # Output: [ 1 99 3 4 5] print("Modified view:", array_view) # Output: [99 3 4]

Output:

Original array: [ 1 99 3 4 5] Modified view: [99 3 4]

Identifying Copies and Views

Use the base attribute to determine if an array is a view. If base is None, the array is a copy.

Example:

# Original array array = np.array([1, 2, 3, 4, 5]) # Create a copy and a view array_copy = array.copy() array_view = array[1:4] # Check base attribute print("Copy base:", array_copy.base) # Output: None print("View base:", array_view.base) # Output: [1 2 3 4 5]

Output:

Copy base: None View base: [1 2 3 4 5]

Practical Implications

When to Use a Copy:

  • If the original array must remain unchanged.
  • For independent data manipulations.

When to Use a View:

  • To save memory when working with large datasets.
  • For temporary or localized changes.

Try It Yourself

Problem 1: Identifying Copy vs View

Create an array and a sliced view of it. Modify the view and observe the changes in the original array. Use the base attribute to verify if it’s a copy or a view.

Show Code

import numpy as np # Create an array array = np.array([10, 20, 30, 40, 50]) # Create a view array_view = array[2:4] # Modify the view array_view[0] = 99 # Check base attribute print("Original array:", array) # Output: [10 20 99 40 50] print("View base:", array_view.base) # Output: [10 20 99 40 50]

Problem 2: Working with Copies

Create an array and make a copy of it. Modify the copy and verify that the original array remains unchanged.

Show Code

import numpy as np # Create an array array = np.array([5, 10, 15, 20, 25]) # Create a copy array_copy = array.copy() # Modify the copy array_copy[2] = 99 # Check arrays print("Original array:", array) # Output: [ 5 10 15 20 25] print("Modified copy:", array_copy) # Output: [ 5 10 99 20 25]

Pyground

Play with Python!

Output:

Last updated on