Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

NumPy Copies and Views

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:

Copies generally occur in:

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

)

print

(

'

Our array is:

'

)

print

(

a

)

print

(

'

Call the id() function:

'

)

print

(

id

(

a

)

)

print

(

'

Assign a to b:

'

)

b

=

a

print

(

b

)

print

(

'

b has the same id():

'

)

print

(

id

(

b

)

)

print

(

'

Modify the shape of b:

'

)

b

.

shape

=

3

,

2

print

(

b

)

print

(

'

The shape of a is also modified:

'

)

print

(

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

)

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 does not modify a

b

.

shape

=

2

,

3

print

(

'

Shape of b:

'

)

print

(

b

)

print

(

'

Shape of a:

'

)

print

(

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

)

print

(

'

Our array:

'

)

print

(

arr

)

print

(

'

Create a slice:

'

)

a

=

arr

[

3

:

]

b

=

arr

[

3

:

]

a

[

1

]

=

123

b

[

2

]

=

234

print

(

arr

)

print

(

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

]

]

)

print

(

'

Array a:

'

)

print

(

a

)

print

(

'

Create a deep copy of a:

'

)

b

=

a

.

copy

(

)

print

(

'

Array b:

'

)

print

(

b

)

b shares nothing with a

print

(

'

Can we write to b to write to a?

'

)

print

(

b

is

a

)

print

(

'

Modify the contents of b:

'

)

b

[

0

,

0

]

=

100

print

(

'

Modified array b:

'

)

print

(

b

)

print

(

'

a remains unchanged:

'

)

print

(

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