Skip to content
FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS
Close
Beyond Knowledge Innovation

Beyond Knowledge Innovation

Where Data Unveils Possibilities

  • Home
  • AI & ML Insights
  • Machine Learning
    • Supervised Learning
      • Introduction
      • Regression
      • Classification
    • Unsupervised Learning
      • Introduction
      • Clustering
      • Association
      • Dimensionality Reduction
    • Reinforcement Learning
    • Generative AI
  • Knowledge Base
    • Introduction To Python
    • Introduction To Data
    • Introduction to EDA
  • References
HomeKnowledge BasePythonNumPy View array vs. Copy array
Python

NumPy View array vs. Copy array

January 19, 2024January 25, 2024CEO 205 views
When you create a subset of a NumPy array and modify its values, it can affect the original array if the subset is actually a view of the original array rather than a copy. NumPy provides views to enhance performance and memory efficiency by avoiding unnecessary data copying. Understanding whether you’re working with a view or a copy is crucial to avoid unintentional side effects.

Here’s an example to illustrate the difference:

import numpy as np

# Create a random matrix
original_matrix = np.random.rand(3, 3)

# Create a subset (view) of the original matrix
subset_view = original_matrix[:2, :2]

# Modify a value in the subset
subset_view[0:1] = 99

# Check the original matrix
print(original_matrix)
[[99.         99.          0.70388917]
 [ 0.43076191  0.13218762  0.29151863]
 [ 0.33869492  0.86248627  0.16560777]]

In this example, modifying subset_view also changes the corresponding value in original_matrix. This is because subset_view is a view of the original data, not a separate copy. It shares the same underlying data, and modifications to one affect the other.

To avoid this behavior and create a separate copy, you can use the copy method:

import numpy as np

# Create a random matrix
original_matrix = np.random.rand(3, 3)

# Create a copy of the original matrix
subset_copy = original_matrix[:2, :2].copy()

# Modify a value in the subset copy
subset_copy[0, 0] = 99

# Check the original matrix
print(original_matrix)

Now, modifying subset_copy doesn’t affect the original matrix because you’ve created an independent copy.

In summary, be aware of whether you are working with views or copies when manipulating NumPy arrays to prevent unintended modifications to the original data. You can use the copy method explicitly when needed to ensure that you are working with a separate copy of the data.

array, matrix, numpy

Post navigation

Previous Post
Previous post: What is NumPy?
Next Post
Next post: How to Save Your Python Objects in Google Colab

You Might Also Like

No image
NumPy function argmax
March 10, 2024 Comments Off on NumPy function argmax
No image
NumPy function argsort
March 10, 2024 Comments Off on NumPy function argsort
No image
How-to: cap/clip outliers in a column
February 6, 2024 Comments Off on How-to: cap/clip outliers in a column
No image
How to Save Your Python Objects in…
January 19, 2024 Comments Off on How to Save Your Python Objects in Google Colab
No image
What is NumPy?
January 18, 2024 Comments Off on What is NumPy?
  • Recent
  • Popular
  • Random
  • No image
    8 months ago Low-Rank Factorization
  • No image
    8 months ago Perturbation Test for a Regression Model
  • No image
    8 months ago Calibration Curve for Classification Models
  • No image
    March 15, 20240Single linkage hierarchical clustering
  • No image
    April 17, 20240XGBoost (eXtreme Gradient Boosting)
  • No image
    April 17, 20240Gradient Boosting
  • No image
    February 28, 2024Linear regression model coefficients
  • No image
    October 21, 2024Perturbation Test for a Regression Model
  • No image
    March 7, 2024Python scikit-learn library for Decision Tree model
  • Implementation (55)
    • EDA (4)
    • Neural Networks (10)
    • Supervised Learning (26)
      • Classification (17)
      • Linear Regression (8)
    • Unsupervised Learning (11)
      • Clustering (8)
      • Dimensionality Reduction (3)
  • Knowledge Base (44)
    • Python (27)
    • Statistics (6)
June 2025
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

We are on

FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS

Subscribe

© 2025 Beyond Knowledge Innovation
FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS