NumPy - 复制和视图

NumPy - 复制和视图 首页 / Numpy入门教程 / NumPy - 复制和视图

在执行函数时,其中一些返回输入数组的副本,而另一些返回视图,当内容实际存储在其他位置时,称为复制。另一方面,如果提供了相同内存内容的不同视图,无涯教程将其称为 View视图 。

简单分配不会复制数组对象 ,相反,它使用原始数组的相同id()进行访问, id()返回Python对象的通用标识符,类似于C中的指针。

此外,任何一个的变化都会反映在另一个中。例如,一个维度的改变也会改变另一个维度。

import numpy as np 
a = np.arange(6) 

print 'Our array is:' 
print a  

print 'Applying id() function:' 
print id(a)  

print 'a is assigned to b:' 
b = a 
print b  

print 'b has same id():' 
print id(b)  

print 'Change shape of b:' 
b.shape = 3,2 
print b  

print 'Shape of a also gets changed:' 
print a

它将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-copies-and-views.html

来源:LearnFk无涯教程网

Our array is:
[0 1 2 3 4 5]

Applying id() function:
139747815479536

a is assigned to b:
[0 1 2 3 4 5]
b has same id():
139747815479536

Change shape of b:
[[0 1]
 [2 3]
 [4 5]]

Shape of a also gets changed:
[[0 1]
 [2 3]
 [4 5]]

浅拷贝

查看原始数组的相同数据,与之前的情况不同,新数组的维度不会更改原始数组的维度。

import numpy as np 
# 首先,a 是 3X2 数组
a = np.arange(6).reshape(3,2) 

print 'Array a:' 
print a  

print 'Create view of a:' 
b = a.view() 
print b  

print 'id() for both the arrays are different:' 
print 'id() of a:'
print id(a)  
print 'id() of b:' 
print id(b)  

# 改变 b 的形状。它不会改变a的形状
b.shape = 2,3 

print 'Shape of b:' 
print b  

print 'Shape of a:' 
print a

它将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-copies-and-views.html

来源:LearnFk无涯教程网

Array a:
[[0 1]
 [2 3]
 [4 5]]

Create view of a:
[[0 1]
 [2 3]
 [4 5]]

id() for both the arrays are different:
id() of a:
140424307227264
id() of b:
140424151696288

Shape of b:
[[0 1 2]
 [3 4 5]]

Shape of a:
[[0 1]
 [2 3]
 [4 5]]

数组的切片将创建一个视图。

import numpy as np 
a = np.array([[10,10], [2,3], [4,5]]) 

print 'Our array is:' 
print a  

print 'Create a slice:' 
s = a[:, :2] 
print s 

它将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-copies-and-views.html

来源:LearnFk无涯教程网

Our array is:
[[10 10]
 [ 2 3]
 [ 4 5]]

Create a slice:
[[10 10]
 [ 2 3]
 [ 4 5]]

深拷贝

ndarray.copy()函数创建一个深层副本,它是阵列及其数据的完整副本,并且不与原始阵列共享。

import numpy as np 
a = np.array([[10,10], [2,3], [4,5]]) 

print 'Array a is:' 
print a  

print 'Create a deep copy of a:' 
b = a.copy() 
print 'Array b is:' 
print b 

#b does not share any memory of a 
print 'Can we write b is a' 
print b is a  

print 'Change the contents of b:' 
b[0,0] = 100 

print 'Modified array b:' 
print b  

print 'a remains unchanged:' 
print a

它将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-copies-and-views.html

来源:LearnFk无涯教程网

Array a is:
[[10 10]
 [ 2 3]
 [ 4 5]]

Create a deep copy of a:
Array b is:
[[10 10]
 [ 2 3]
 [ 4 5]]
Can we write b is a
False

Change the contents of b:
Modified array b:
[[100 10]
 [ 2 3]
 [ 4 5]]

a remains unchanged:
[[10 10]
 [ 2 3]
 [ 4 5]]

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

程序员进阶攻略 -〔胡峰〕

从0开始学微服务 -〔胡忠想〕

数据结构与算法之美 -〔王争〕

许式伟的架构课 -〔许式伟〕

研发效率破局之道 -〔葛俊〕

高并发系统设计40问 -〔唐扬〕

Flink核心技术与实战 -〔张利兵〕

Go进阶 · 分布式爬虫实战 -〔郑建勋〕

结构会议力 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)