[Numpy] 数组和内存的浅复制和深复制(View vs Copy)

本文介绍了Numpy中数组切片的View(浅复制)和Copy(深复制)的区别,重点关注切片操作何时产生视图何时产生复制。当索引对象为非元组序列、numpy数组或包含序列的元组时,会产生复制;其他情况则创建视图,以节省内存。同时,文章提到了as_strided方法始终创建视图,以及如何通过flags.owndata检查复制类型。内容还涉及LaTeX数学表达式、甘特图、UML图表和Flowchart流程图的使用。

Numpy数组和内存 View vs Copy 或称 浅复制或深复制

一句话概括最重要的区别:

在对numpy切片(slicing)的时候,如果切片的表达式内含有带括号的非tuple对象,如数列,或者另一个numpy数列,或者tuple对象(但至少包含一个含有序列的对象或者一个numpy数列),则进行复制操作。其他情况下进行View操作,只是访问了原有numpy数列的内存。

(这个view是指浅复制,和view方法没有关系。)

as_strided方法任何时候都只是View操作。
flags.owndata可以查看view or copy。

例子和英文解释摘自课堂笔记。

numpy uses pass-by-reference semantics so that slice operations are views into the array without implicit copying, which is consistent with python’s semantics. this is particularly helpful with large arrays that already strain available memory. in numpy terminology, slicing creates views (no copying) and advanced indexing creates copies. let’s start with advanced indexing.
if the indexing object (i.e., the item between the brackets) is a non-tuple sequence object, another numpy array (of type integer or boolean), or a tuple with at least one sequence object or numpy array, then indexing creates copies. for the above example, to accomplish the same array extension in numpy, you have to do something like the following

>>> x = np.ones((3,3))
>>> x
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])
>>> x[:,[0,1,2,2]] # notice duplicated last dimension
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
>>> y=x[:,[0,1,2,2]] # same as above, but do assign it to y
# y=x[:,(0,1,2,2)] has the same results.
>>> x[0,0]=999 # change element in x
>>> x                         # changed
array([[999.,   1.,   1.],
       [  1.,   1.,   1.],
       [  1.,   1.,   1.]])
>>> y                         # not changed!
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])

>>> x = np.ones((3,3))
>>> y = x[:2,:2] # view of upper left piece
>>> x[0,0] = 999 # change value
>>> x  # see the change?
array([[999.,   1.,   1.],
       [  1.,   1.,   1.],
       [  1.,   1.,   1.]])
>>> y
array([[999.,   1.],
       [  1.,   1.]])

>>> x = np.arange(5) # create array
>>> x
array([0, 1, 2, 3, 4])
>>> y=x[[0,1,2]] # index by integer list to force copy
>>> y
array([0, 1, 2])
>>> z=x[:3]      # slice creates view
>>> z            # note y and z have same entries
array([0, 1, 2])
>>> x[0]=999     # change element of x
>>> x
array([999,   1,   2,   3,   4])
>>> y            # note y is unaffected,
array([0, 1, 2])
>>> z            # but z is (it's a view).
array([999,   1,   2])
in this example, y is a copy, not a view, because it was created using advanced indexing


>>> from numpy.lib.stride_tricks import as_strided
>>> x = np.arange(16).astype(np.int32)
>>> y=as_strided(x,(7,4),(8,4)) # overlapped entries
>>> y
array([[ 0,  1,  2,  3],
       [ 2,  3,  4,  5],
       [ 4,  5,  6,  7],
       [ 6,  7,  8,  9],
       [ 8,  9, 10, 11],
       [10, 11, 12, 13],
       [12, 13, 14, 15]], dtype=int32)

>>> x[::2]=99 # assign every other value
>>> x
array([99,  1, 99,  3, 99,  5, 99,  7, 99,  9, 99, 11, 99, 13, 99, 15],
      dtype=int32)
>>> y # the changes appear because y is a view
array([[99,  1, 99,  3],
       [99,  3, 99,  5],
       [99,  5, 99,  7],
       [99,  7, 99,  9],
       [99,  9, 99, 11],
       [99, 11, 99, 13],
       [99, 13, 99, 15]], dtype=int32)
       
>>> n = 8 # number of elements
>>> x = np.arange(n) # create array
>>> k = 5 # desired number of rows
>>> y = as_strided(x,(k,n-k+1),(x.itemsize,)*2)
>>> y
array([[0, 1, 2, 3],
       [1, 2, 3, 4],
       [2, 3, 4, 5],
       [3, 4, 5, 6],
       [4, 5, 6, 7]])

# Use flags.owndata to see wether it is a view or copy.

Γ ( z ) = ∫ 0 ∞ t z − 1 e − t d t   . \Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,. Γ(z)=0tz1etdt.

你可以找到更多关于的信息 LaTeX 数学表达式here.

新的甘特图功能,丰富你的文章

Mon 06 Mon 13 Mon 20 已完成 进行中 计划一 计划二 现有任务 Adding GANTT diagram functionality to mermaid
  • 关于 甘特图 语法,参考 这儿,

UML 图表

可以使用UML图表进行渲染。 Mermaid. 例如下面产生的一个序列图::

张三 李四 王五 你好!李四, 最近怎么样? 你最近怎么样,王五? 我很好,谢谢! 我很好,谢谢! 李四想了很长时间, 文字太长了 不适合放在一行. 打量着王五... 很好... 王五, 你怎么样? 张三 李四 王五

这将产生一个流程图。:

链接
长方形
圆角长方形
菱形
  • 关于 Mermaid 语法,参考 这儿,

FLowchart流程图

我们依旧会支持flowchart的流程图:

Created with Raphaël 2.2.0 开始 我的操作 确认? 结束 yes no
  • 关于 Flowchart流程图 语法,参考 这儿.

导出与导入

导出

如果你想尝试使用此编辑器, 你可以在此篇文章任意编辑。当你完成了一篇文章的写作, 在上方工具栏找到 文章导出 ,生成一个.md文件或者.html文件进行本地保存。

导入

如果你想加载一篇你写过的.md文件或者.html文件,在上方工具栏可以选择导入功能进行对应扩展名的文件导入,
继续你的创作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值