NumPy Copies and Views
A copy is a complete duplicate of the data; if we modify the copy, it does not affect the original data, and the physical memory is not in the same location.
A view is an alias or reference to the data, through which the original data can be accessed and manipulated, but no copy of the original data is produced. If we modify the view, it affects the original data, and the physical memory is in the same location.
Views generally occur in:
- 1. numpy's slicing operation returns a view of the original data.
- 2. Calling the view() function of an ndarray produces a view.
Copies generally occur in:
- Slicing operations on Python sequences, calling the deepCopy() function.
- Calling the copy() function of an ndarray produces a copy.
No copy
A simple assignment does not create a copy of the array object. Instead, it uses the same id() of the original array to access it. id() returns the universal identifier of a Python object, similar to a pointer in C.
In addition, any change to one array is reflected in the other. For example, changing the shape of one array also changes the shape of the other.
Example
import
numpy
as
np
a
=
np
.
arange
(
6
)
(
'
Our array is:
'
)
(
a
)
(
'
Call the id() function:
'
)
(
id
(
a
)
)
(
'
Assign a to b:
'
)
b
=
a
(
b
)
(
'
b has the same id():
'
)
(
id
(
b
)
)
(
'
Modify the shape of b:
'
)
b
.
shape
=
3
,
2
(
b
)
(
'
The shape of a is also modified:
'
)
(
a
)
The output is:
我们的数组是:
[0 1 2 3 4 5]
Call the id() function:
4349302224
a 赋值给 b:
[0 1 2 3 4 5]
b 拥有相同 id():
4349302224
Modify the shape of b:
[[0 1]
[2 3]
[4 5]]
The shape of a was also modified:
[[0 1]
[2 3]
[4 5]]
View or shallow copy
ndarray.view() creates a new array object; changes to the dimensions of the new array created by this method do not change the dimensions of the original data.
Example
import
numpy
as
np
Initially a is a 3X2 array
a
=
np
.
arange
(
6
)
.
reshape
(
3
,
2
)
(
'
Array a:
'
)
(
a
)
(
'
Create a view of a:
'
)
b
=
a
.
view
(
)
(
b
)
(
'
The id() of the two arrays are different:
'
)
(
'
id() of a:
'
)
(
id
(
a
)
)
(
'
id() of b:
'
)
(
id
(
b
)
)
Modifying the shape of b does not modify a
b
.
shape
=
2
,
3
(
'
Shape of b:
'
)
(
b
)
(
'
Shape of a:
'
)
(
a
)
The output is:
数组 a:
[[0 1]
[2 3]
[4 5]]
Create a view of a:
[[0 1]
[2 3]
[4 5]]
The ids of two arrays are different:
a 的 id():
4314786992
b 的 id():
4315171296
Shape of b:
[[0 1 2]
[3 4 5]]
Shape of a:
[[0 1]
[2 3]
[4 5]]
Creating a view using a slice and modifying the data will affect the original array:
Example
import
numpy
as
np
arr
=
np
.
arange
(
12
)
(
'
Our array:
'
)
(
arr
)
(
'
Create a slice:
'
)
a
=
arr
[
3
:
]
b
=
arr
[
3
:
]
a
[
1
]
=
123
b
[
2
]
=
234
(
arr
)
(
id
(
a
)
,
id
(
b
)
,
id
(
arr
[
3
:
]
)
)
The output is:
我们的数组:
[ 0 1 2 3 4 5 6 7 8 9 10 11]
Create slice:
[ 0 1 2 3 123 234 6 7 8 9 10 11]
4545878416 4545878496 4545878576
Variables a and b are both partial views of arr, and modifications to the views are directly reflected in the original data. But when we observe the ids of a and b, they are different, which means that although views point to the original data, they are still different from assignment references.
Copy or deep copy
The ndarray.copy() function creates a copy. Modifications to the copy data do not affect the original data; their physical memory is not in the same location.
Example
import
numpy
as
np
a
=
np
.
array
(
[
[
10
,
10
]
,
[
2
,
3
]
,
[
4
,
5
]
]
)
(
'
Array a:
'
)
(
a
)
(
'
Create a deep copy of a:
'
)
b
=
a
.
copy
(
)
(
'
Array b:
'
)
(
b
)
b shares nothing with a
(
'
Can we write to b to write to a?
'
)
(
b
is
a
)
(
'
Modify the contents of b:
'
)
b
[
0
,
0
]
=
100
(
'
Modified array b:
'
)
(
b
)
(
'
a remains unchanged:
'
)
(
a
)
The output is:
数组 a:
[[10 10]
[ 2 3]
[ 4 5]]
Create a deep copy of a:
数组 b:
[[10 10]
[ 2 3]
[ 4 5]]
我们能够写入 b 来写入 a 吗?
False
Modify the content of b:
修改后的数组 b:
[[100 10]
[ 2 3]
[ 4 5]]
a keep unchanged:
[[10 10]
[ 2 3]
[ 4 5]]
More related articles
Python direct assignment, shallow copy and deep copy explained