English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NumPy Array Copies and Views

A copy is a complete copy of data, if we modify the copy, it will not affect the original data, and the physical memory is not in the same location.
A view is an alias or reference to data, through which the original data can be accessed and operated, but no copy is generated. If we modify the view, it will affect the original data, with the physical memory in the same location.

Difference between copies and views

The main difference between copies and array views is that the copy is a new array, while this view is just a view of the original array.Copies own data, any changes made to the copy will not affect the original array, and any changes made to the original array will not affect the copy.Views do not own data, any changes made to the view will affect the original array, and any changes made to the original array will also affect the view.

Views generally occur:

1、The slicing operation of numpy returns a view of the original data.2、Calling the view() function of ndarray to produce a view.

Copies generally occur:

Python sequence slicing operation, calling the deepCopy() function.A copy is produced by calling the copy() function of the ndarray.

无复制

简单的赋值不会创建数组对象的副本。 相反,它使用原始数组的相同id()来访问它。 id()返回 Python 对象的通用标识符,类似于 C 中的指针。此外,一个数组的任何变化都反映在另一个数组上。 例如,一个数组的形状改变也会改变另一个数组的形状。

No copy
Simple assignment does not create a copy of the array object. Instead, it accesses it using the same id() of the original array. id() returns the universal identifier of Python objects, similar to pointers in C. In addition, any change to an array is reflected in the other array. For example, changing the shape of one array will also change the shape of the other array.6)
>>> import numpy as np
>>> a = np.arange( 1 2 3 4 5]
>>> print('Our array is:',a)
>>> print('Call the id() function:',id(a)) 4553321728
Call the id() function:
>>> b.shape =
>>> b = a 1 2 3 4 5]
[0
>>> print('b has the same id():',id(b)) 4553321728
b has the same id(): 3,2
>>> b.shape =
[[0 1]
 [2 3]
 [4 5]]
>>> print(b)
[[0 1]
 [2 3]
 [4 5]]
>>> print(a)

>>>

ndarray.view() will create a new array object, and the change of the dimensions of the new array created by this method will not change the dimensions of the original data. View or shallow copy

import numpy as np 
 
 # Initially, a is a 3X2 of the array
a = np.arange(6).reshape(3,2) 
print ('Array a:')
print (a)
print('Create a view of a:')
b = a.view() 
print (b)
print('The id() of the two arrays are different:')
print('id() of a:')
print(id(a))
print('id() of b:')
print(id(b))
 # Modifying the shape of b will not modify a
b.shape = 2,3
print('Shape of b:')
print (b)
print('Shape of a:')
print (a)

Output Result Is:

Array a:
[[0 1]
 [2 3]
 [4 5]]
Create a view of a:
[[0 1]
 [2 3]
 [4 5]]
The id() of the two arrays are different:
id() of a:
4314786992
id() of b:
4315171296
Shape of b:
[[0 1 2]
 [3 4 5]]
Shape of a:
[[0 1]
 [2 3]
 [4 5]]

Modifying data using slice views will affect the original array:

import numpy as np 
arr = np.arange(12)
print('Our array:')
print(arr)
print('Create slices:')
a=arr[2:]
b=arr[2:]
a[1])=123456
b[2])=23445
print(arr)
print(id(a),id(b),id(arr[3:]))

Output Result Is:

Our array:
[ 0 1 2 3 4 5 6 7 8 9 10 11]
Create slices:
[ 0 1 2 123456 23445 5 6 7 8 9
     10 11]
4669930672 4444330304 4670012352
Process finished with exit code 0

Variables a, b are both parts of the view of arr. Modifications to the view will be reflected directly in the original data. However, when we observe the id of a, b, they are different, which means that although the view points to the original data, there is still a difference between the view and the assignment reference.

Copy or Deep Copy

The ndarray.copy() function creates a copy. Modifying the data in the copy does not affect the original data, as they are not in the same physical memory location.

import numpy as np 
 
a = np.array([[10,10], [2,3], [4,5]] 
print ('Array a:')
print (a)
print ('Create a Deep Copy of a:')
b = a.copy() 
print ('Array b:')
print (b)
 # b and a do not share any content 
print ('Can We Write to b to Write to a?')
print (b is a)
print ('Modify the Content of b:')
b[0,0] = 100 
print ('Modified Array b:')
print (b)
print ('a Keep Unchanged:')
print (a)

Output Result Is:

Array a:
[[10 10]
 [ 2 3]
 [ 4 5]]
Create a Deep Copy of a:
Array b:
[[10 10]
 [ 2 3]
 [ 4 5]]
Can We Write to b to Write to a?
False
Modify the Content of b:
Modified Array b:
[[100 10]
 [ 2 3]
 [ 4 5]]
a Keep Unchanged:
[[10 10]
 [ 2 3]
 [ 4 5]]