numpy 数组复制问题 数组的几种复制

引自https://docs.scipy.org/doc/numpy/user/quickstart.html#copies-and-views

Copies and Views

When operating and manipulating arrays, their data is sometimes copied into a new array and sometimes not. This is often a source of confusion for beginners. There are three cases:

No Copy at All

Simple assignments make no copy of array objects or of their data.

a = np.arange(12)

b = a # no new object is created
b is a # a and b are two names for the same ndarray object
True
b.shape = 3,4 # changes the shape of a
a.shape
(3, 4)

Python passes mutable objects as references, so function calls make no copy.

def f(x):
... print(id(x))
...
id(a) # id is a unique identifier of an object
148293216
f(a)
148293216

View or Shallow Copy

Different array objects can share the same data. The view method creates a new array object that looks at the same data.

c = a.view()
c is a
False
c.base is a # c is a view of the data owned by a
True
c.flags.owndata
False

c.shape = 2,6 # a's shape doesn't change
a.shape
(3, 4)
c[0,4] = 1234 # a's data changes
a
array([[ 0, 1, 2, 3],
[1234, 5, 6, 7],
[ 8, 9, 10, 11]])

Slicing an array returns a view of it:

s = a[ : , 1:3] # spaces added for clarity; could also be written "s = a[:,1:3]"
s[:] = 10 # s[:] is a view of s. Note the difference between s=10 and s[:]=10
a
array([[ 0, 10, 10, 3],
[1234, 10, 10, 7],
[ 8, 10, 10, 11]])

Deep Copy

The copy method makes a complete copy of the array and its data.

d = a.copy() # a new array object with new data is created
d is a
False
d.base is a # d doesn't share anything with a
f(a)
148293216


### View or Shallow Copy

Different array objects can share the same data. The view method creates a new array object that looks at the same data.

>>> c = a.view()
>>> c is a
False
>>> c.base is a # c is a view of the data owned by a
True
>>> c.flags.owndata
False
>>>
>>> c.shape = 2,6 # a's shape doesn't change
>>> a.shape
(3, 4)
>>> c[0,4] = 1234 # a's data changes
>>> a
array([[ 0, 1, 2, 3],
[1234, 5, 6, 7],
[ 8, 9, 10, 11]])


Slicing an array returns a view of it:

>>> s = a[ : , 1:3] # spaces added for clarity; could also be written "s = a[:,1:3]"
>>> s[:] = 10 # s[:] is a view of s. Note the difference between s=10 and s[:]=10
>>> a
array([[ 0, 10, 10, 3],
[1234, 10, 10, 7],
[ 8, 10, 10, 11]])


### Deep Copy

The copy method makes a complete copy of the array and its data.

>>> d = a.copy() # a new array object with new data is created
>>> d is a
False
>>> d.base is a # d doesn't share anything with a
False
d[0,0] = 9999
a
array([[ 0, 10, 10, 3],
[1234, 10, 10, 7],
[ 8, 10, 10, 11]])

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容