copy
方法制作完整數組的副本及其數據。
>>> 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]])
copy
如果不再需要原始數組,有時應該在切片后調用。例如,假設a
是一個巨大的中間結果,而最終結果b
只包含 的一小部分a
,則在b
使用切片構造時應進行深拷貝:
>>> a = np.arange(int(1e8))
>>> b = a[:100].copy()
>>> del a # the memory of ``a`` can be released.
如果b?=?a[:100]
被使用,a
則被b
引用并且即使del a
被執(zhí)行a
也會保留在內存中。
更多建議: