Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Learn Copy vs View

NumPy, understanding the concepts of copy and view is important for managing memory efficiently and avoiding unintended changes to arrays. Let's explore the differences between copy and view:


Copy

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = arr1.copy()
arr2[0] = 100
print(arr1) # Output: [1 2 3]
print(arr2) # Output: [100 2 3]


View

import numpy as np
arr1 = np.array([1, 2, 3, 4, 5])
view_arr = arr1[1:4]
view_arr[0] = 100
print(arr1) # Output: [ 1 100 3 4 5]
print(view_arr) # Output: [100 3 4]


In summary:
Copy creates a new array with its own data, modifying the copy doesn't affect the original array, and vice versa.
View creates a new array object that shares the same data as the original array, modifying the view affects the original array, and vice versa.




Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java