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)=∫0∞tz−1e−tdt.
你可以找到更多关于的信息 LaTeX 数学表达式here.
新的甘特图功能,丰富你的文章
- 关于 甘特图 语法,参考 这儿,
UML 图表
可以使用UML图表进行渲染。 Mermaid. 例如下面产生的一个序列图::
这将产生一个流程图。:
- 关于 Mermaid 语法,参考 这儿,
FLowchart流程图
我们依旧会支持flowchart的流程图:
- 关于 Flowchart流程图 语法,参考 这儿.
导出与导入
导出
如果你想尝试使用此编辑器, 你可以在此篇文章任意编辑。当你完成了一篇文章的写作, 在上方工具栏找到 文章导出 ,生成一个.md文件或者.html文件进行本地保存。
导入
如果你想加载一篇你写过的.md文件或者.html文件,在上方工具栏可以选择导入功能进行对应扩展名的文件导入,
继续你的创作。