NumPy 复制和视图
来源:互联网 发布:seo行业前景 编辑:程序博客网 时间:2024/06/03 22:20
复制和视图
当运算和处理数组时,它们的数据有时被拷贝到新的数组有时不是。这通常是新手的困惑之源。这有三种情况:
完全不拷贝
简单的赋值不拷贝数组对象或它们的数据。
>>> a = arange(12)>>> b = a # no new object is created>>> b is a # a and b are two names for the same ndarray objectTrue>>> b.shape = 3,4 # changes the shape of a>>> a.shape(3, 4)
Python 传递不定对象作为参考 4,所以函数调用不拷贝数组。
>>> def f(x):... print id(x)...>>> id(a) # id is a unique identifier of an object148293216>>> f(a)148293216
视图(view)和浅复制
不同的数组对象分享同一个数据。视图方法创造一个新的数组对象指向同一数据。
>>> c = a.view()>>> c is aFalse>>> c.base is a # c is a view of the data owned by aTrue>>> c.flags.owndataFalse>>>>>> c.shape = 2,6 # a's shape doesn't change>>> a.shape(3, 4)>>> c[0,4] = 1234 # a's data changes>>> aarray([[ 0, 1, 2, 3], [1234, 5, 6, 7], [ 8, 9, 10, 11]])
切片数组返回它的一个视图:
>>> s = a[ : , 1:3] # spaces added for clarity; could also be written "s = a[:,1:3]">>> s[:] = 10 # s[:] is a view of s. Note the difference between s=10 and s[:]=10>>> aarray([[ 0, 10, 10, 3], [1234, 10, 10, 7], [ 8, 10, 10, 11]])
深复制
这个复制方法完全复制数组和它的数据。
>>> d = a.copy() # a new array object with new data is created>>> d is aFalse>>> d.base is a # d doesn't share anything with aFalse>>> d[0,0] = 9999>>> aarray([[ 0, 10, 10, 3], [1234, 10, 10, 7], [ 8, 10, 10, 11]])
0
>>> a = arange(12)
>>> b=a
>>> b is a
True
>>> b.shape
(12,)
>>> b
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> b.shape =3,4
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> def f(x):
... print id(x)
...
>>> id(a)
35190088
>>> f(a)
35190088
>>> c = a.view()
>>> c
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> c is a
False
>>>
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> c.base is a
True
>>> c.flags.owndata
False
>>> c.shape
(3, 4)
>>> c.shape =2,6
>>> a.shape
(3, 4)
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> c
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]])
>>> c[0,4]=1234
>>> a
array([[ 0, 1, 2, 3],
[1234, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> s = a[ : , 1:3]
>>> s
array([[ 1, 2],
[ 5, 6],
[ 9, 10]])
>>> s[:]=10
>>> s
array([[10, 10],
[10, 10],
[10, 10]])
>>> a
array([[ 0, 10, 10, 3],
[1234, 10, 10, 7],
[ 8, 10, 10, 11]])
>>> d = a.copy()
>>> d is a
False
>>> d.base is a
False
>>> d
array([[ 0, 10, 10, 3],
[1234, 10, 10, 7],
[ 8, 10, 10, 11]])
>>> a
array([[ 0, 10, 10, 3],
[1234, 10, 10, 7],
[ 8, 10, 10, 11]])
>>> d[0,0]=9999
>>> a
array([[ 0, 10, 10, 3],
[1234, 10, 10, 7],
[ 8, 10, 10, 11]])
>>>
- NumPy 复制和视图
- NumPy 复制与视图
- Numpy-复制与视图
- NumPy之三:拷贝和视图
- oracle复制表、临时表和视图
- 物化视图复制概念和体系结构——高级复制
- 物化视图复制概念和体系结构——高级复制
- Numpy-复制操作
- numpy小记,复制array
- NumPy的拷贝和视图(完全不拷贝、视图或浅拷贝、深拷贝)
- 使用numpy复制拓展数据
- Python的Numpy的Array的副本(copy)和视图(view)
- numpy.load和numpy.save
- 【python】numpy数组(array)扩充(复制)方法repeat和tile的使用
- 【ORACLE 高可用】 高级复制的两个配置实例 - 使用高级复制和物化视图
- 复制数据表以及视图
- numpy 攻略 2.5 视图与副本
- numpy中数组元素的切片复制
- k3cloud二次开例子
- js和jquery分别获取select标签选中的值
- 在sts中springboot工程的maven解析异常处理
- zoj1539
- IAR之C-STAT,C-RUN
- NumPy 复制和视图
- zoj1543
- php基本常识
- 机器学习笔记(三)线性模型
- 利用Gson把json转化成Java List异常问题
- 验证码识别
- Win10如何使用Win7自带的照片查看器
- 115. Distinct Subsequences
- Java考试题之七