- 教程
- 介绍
- 环境
- Ndarray 对象(Ndarray Object)
- 数据类型
- 数组属性(Array Attributes)
- 阵列创建例程(Array Creation Routines)
- 来自现有数据的数组(Array from Existing Data)
- 数值范围中的数组(Array From Numerical Ranges)
- 数值范围中的数组(Array From Numerical Ranges)
- 数值范围中的数组(Array From Numerical Ranges)
- 广播(Broadcasting)
- 迭代数组(Iterating Over Array)
- 数组操作(Array Manipulation)
- Binary 运算符
- 字符串函数(String Functions)
- 数学函数(Mathematical Functions)
- 算术运算(Arithmetic Operations)
- 统计函数(Statistical Functions)
- 统计函数(Statistical Functions)
- 字节交换(Byte Swapping)
- 副本和视图(Copies & Views)
- 矩阵库(Matrix Library)
- 线性代数(Linear Algebra)
- Matplotlib(Matplotlib)
- 使用Matplotlib的直方图(Histogram Using Matplotlib)
- I/O with NumPy
- 有用的资源
- reshape
- flat
- flatten
- ravel
- transpose
- ndarray.T
- rollaxis
- swapaxes
- broadcast
- broadcast_to
- expand_dims
- squeeze
- concatenate
- stack
- hstack
- vstack
- split
- hsplit
- vsplit
- resize
- append
- insert
- delete
- unique
- bitwise_and
- bitwise_or
- invert
- left_shift
- right_shift
- add()
- multiply()
- center()
- capitalize()
- title()
- lower()
- upper()
- split()
- splitlines()
- strip()
- join()
- replace()
- decode()
- encode()
- dot
- vdot
- inner
- matmul
- determinant
- solve
- inv
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
副本和视图(Copies & Views)
在执行函数时,其中一些函数返回输入数组的副本,而一些函数返回视图。 当内容物理存储在另一个位置时,称为Copy 。 另一方面,如果提供相同内存内容的不同视图,我们将其称为View 。
没有副本
简单赋值不会生成数组对象的副本。 相反,它使用原始数组的相同id()来访问它。 id()返回Python对象的通用标识符,类似于C中的指针。
此外,任何一方面的任何变化都会反映在另一方面。 例如,改变一个的形状也会改变另一个的形状。
例子 (Example)
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
它将产生以下输出 -
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]]
查看或浅拷贝
NumPy有ndarray.view()方法,它是一个新的数组对象,它查看原始数组的相同数据。 与前面的情况不同,新阵列的尺寸变化不会改变原始尺寸。
例子 (Example)
import numpy as np
# To begin with, a is 3X2 array
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)
# Change the shape of b. It does not change the shape of a
b.shape = 2,3
print 'Shape of b:'
print b
print 'Shape of a:'
print a
它将产生以下输出 -
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]]
切片的数组创建一个视图。
例子 (Example)
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
它将产生以下输出 -
Our array is:
[[10 10]
[ 2 3]
[ 4 5]]
Create a slice:
[[10 10]
[ 2 3]
[ 4 5]]
深拷贝
ndarray.copy()函数创建一个深层副本。 它是数组及其数据的完整副本,不与原始数组共享。
例子 (Example)
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
它将产生以下输出 -
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]]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论