np.copy()是深拷贝还是浅拷贝

np.copy到底是深拷贝还是浅拷贝

  • 实验
    • [1. 拷贝矩阵](#1. 拷贝矩阵)
      • [2. 修改m的值](#2. 修改m的值)
      • [3. 修改拷贝矩阵的值](#3. 修改拷贝矩阵的值)
  • 官方文档

最近在用numpy的拷贝操作,发现网上对np.copy()究竟是深拷贝还是浅拷贝说法不一致,因此记录一下。

总结 :如果numpy array是一个简单的数组,np.copy()是深拷贝。如果numpy array内包含了对象,np.copy()是浅拷贝。
ps : arr.copy = np.copy(arr)

实验

1. 拷贝矩阵

原始矩阵m,分别用两种不同的方式拷贝。用np.copy()得到n, 用浅拷贝得到z

python 复制代码
import numpy as np
m = np.array([[0,1,2],[1,2,3],[3,4,5]])
# numpy拷贝, 等同于n = np.copy(m)
n = m.copy()
# 浅拷贝
z = m

输出:

python 复制代码
>>> m
array([[0, 1, 2],
       [1, 2, 3],
       [3, 4, 5]])
>>> n
array([[0, 1, 2],
       [1, 2, 3],
       [3, 4, 5]])
>>> z
array([[0, 1, 2],
       [1, 2, 3],
       [3, 4, 5]])

2. 修改m的值

python 复制代码
m[0][0]=-1

修改m的值后,使用np.copy的n值没有改变,浅拷贝z的值发生了改变

python 复制代码
>>> m
array([[-1,  1,  2],
       [ 1,  2,  3],
       [ 3,  4,  5]])
>>> n
array([[0, 1, 2],
       [1, 2, 3],
       [3, 4, 5]])
>>> z
array([[-1,  1,  2],
       [ 1,  2,  3],
       [ 3,  4,  5]])

3. 修改拷贝矩阵的值

修改n的值,mz值都没有改变

python 复制代码
n[0][0]=-2

>>> m
array([[-1,  1,  2],
       [ 1,  2,  3],
       [ 3,  4,  5]])
>>> n
array([[-2,  1,  2],
       [ 1,  2,  3],
       [ 3,  4,  5]])
>>> z
array([[-1,  1,  2],
       [ 1,  2,  3],
       [ 3,  4,  5]])

修改z的值,m值改变和n值不变

python 复制代码
z[0][0]=-3

array([[-3,  1,  2],
       [ 1,  2,  3],
       [ 3,  4,  5]])
>>> n
array([[-2,  1,  2],
       [ 1,  2,  3],
       [ 3,  4,  5]])
>>> z
array([[-3,  1,  2],
       [ 1,  2,  3],
       [ 3,  4,  5]])

因此np.copy从以上的例子来看是深拷贝, =是浅拷贝

官方文档

但是在 numpy官方文档中明确提到np.copy是浅拷贝。原因是如果array里的元素是一个对象时,如果对象的元素改变,原来的array的对象也会改变。也就是说numpy array中对象元素的拷贝是浅拷贝。

Note that np.copy is a shallow copy and will not copy object elements within arrays. This is mainly important for arrays containing Python objects. The new array will contain the same object which may lead to surprises if that object can be modified (is mutable):

复制代码
a = np.array([1, 'm', [2, 3, 4]], dtype=object)
b = np.copy(a)
b[2][0] = 10
a
array([1, 'm', list([10, 3, 4])], dtype=object)

To ensure all elements within an object array are copied, use copy.deepcopy:

复制代码
import copy
a = np.array([1, 'm', [2, 3, 4]], dtype=object)
c = copy.deepcopy(a)
c[2][0] = 10
c
array([1, 'm', list([10, 3, 4])], dtype=object)
a
array([1, 'm', list([2, 3, 4])], dtype=object)

参考文档

  1. 官方文档
  2. 博客1
  3. 博客2
相关推荐
Awesome Baron10 分钟前
《Learning Langchain》阅读笔记8-RAG(4)在vector store中存储embbdings
python·jupyter·chatgpt·langchain·llm
阡之尘埃12 分钟前
Python数据分析案例73——基于多种异常值监测算法探查内幕交易信息
人工智能·python·机器学习·数据分析·异常检测·无监督学习
蓝莓味柯基1 小时前
Python3:文件操作
python
xiaoh_72 小时前
解决视频处理中的 HEVC 解码错误:Could not find ref with POC xxx【已解决】
python·ffmpeg·音视频
明月与玄武2 小时前
Python编程的真谛:超越语法,理解编程本质
python·编程语言
CodeCraft Studio2 小时前
Excel处理控件Aspose.Cells教程:使用 Python 在 Excel 中进行数据验
开发语言·python·excel
拾忆-eleven3 小时前
C语言实战:用Pygame打造高难度水果消消乐游戏
c语言·python·pygame
旦莫3 小时前
Python 教程:我们可以给 Python 文件起中文名吗?
开发语言·python
豌豆花下猫3 小时前
Python 潮流周刊#99:如何在生产环境中运行 Python?(摘要)
后端·python·ai
小杨4043 小时前
python入门系列二十(peewee)
人工智能·python·pycharm