Numpy 03 - View_Copy & OverView

96 阅读2分钟

拷贝及视图

当对阵列进行操作时,阵列中的数据有时会改变有时则不会。下面来进行辨析:

一、完全不复制

简单的操作不会进行阵列复制。和python基础语法相似。

>>> a = np.array([[ 0,  1,  2,  3],
...               [ 4,  5,  6,  7],
...               [ 8,  9, 10, 11]])
>>> b = a            # no new object is created,其实就是将a指针内容赋值给b,对b操作会改变a值
>>> b is a           # a and b are two names for the same ndarray object
True

Python允许各种对象作为函数参数,所以函数也不会进行阵列复制。

>>> def f(x):
...     print(id(x))
...
>>> id(a)  # id is a unique identifier of an object
148293216  # may vary
>>> f(a)# 只有传址调用,没有传值调用
148293216  # may vary

二、查看或者浅复制

不同的阵列对象能共享数据,view方法能创建一个查看相同数据的阵列对象。也就是对阵列创建一个只能查看、修改阵列数据但不能修改阵列形状。

>>> c = a.view() # 创建 a 阵列的view对象, c 只能查看a的各项数据但不能做出形状更改
>>> c is a
False
>>> c.base is a            # c is a view of the data owned by a
True
>>> c.flags.owndata
False
>>>
>>> c = c.reshape((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]])

对阵列进行切片会返回一个切片视图。

>>> s = a[:, 1:3] # s 是a的切片视图范围是 全行 - 1和2列
>>> s[:] = 10  # s[:] is a view of s. 
# Note the difference between s = 10 and s[:] = 10, 前者会将 s 指向一个常数 10
>>> a
array([[   0,   10,   10,    3],
       [1234,   10,   10,    7],
       [   8,   10,   10,   11]])

三、全复制

copy方法提供了一个完全复制阵列的副本。

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

有时完全复制会造成冗余。比如说,假如 a 是一个非常庞大的中间量,而最终结果 b 只会包含 a 很小的一部分,此时就应该进行切片复制而非完全复制。

>>> a = np.arange(int(1e8))
>>> b = a[:100].copy() # 切片复制,节省存储空间
>>> del a  # the memory of ``a`` can be released.

函数和方法总览

  • Array Creation

arange, array, copy, empty, empty_like, eye, fromfile, fromfunction, identity, linspace, logspace, mgrid, ogrid, ones, ones_like, r_, zeros, zeros_like

  • Conversions

ndarray.astype, atleast_1d, atleast_2d, atleast_3d, mat

  • Manipulations

array_split, column_stack, concatenate, diagonal, dsplit, dstack, hsplit, hstack, ndarray.item, newaxis, ravel, repeat, reshape, resize, squeeze, swapaxes, take, transpose, vsplit, vstack

  • Questions

all, any, nonzero, where

  • Ordering

argmax, argmin, argsort, max, min, ptp, searchsorted, sort

  • Operations

choose, compress, cumprod, cumsum, inner, ndarray.fill, imag, prod, put, putmask, real, sum

  • Basic Statistics

cov, mean, std, var

  • Basic Linear Algebra

cross, dot, outer, linalg.svd, vdot