如何创建具有规则间隔值的数组#
有几个 NumPy 函数在应用上很相似,但会产生略有不同的结果,如果使用不当可能会引起混淆。以下指南旨在列出这些函数并描述它们的推荐用法。
这里提到的函数是
一维域(区间)#
linspace 与 arange#
和 numpy.linspace 都提供了将一个区间(一维域)划分为等长子区间的方法。这些划分将根据所选的起始点、结束点和**步长**(子区间的长度)而变化。numpy.arange
如果您想要整数步长,请使用
。numpy.arange依赖步长来确定返回数组中有多少个元素,它不包含结束点。这由numpy.arange的arange参数确定。step示例
>>> np.arange(0, 10, 2) # np.arange(start, stop, step) array([0, 2, 4, 6, 8])
和start参数应该是整数或实数,但不能是复数。stop类似于 Python 内置的numpy.arange。range浮点数的精度问题可能会使
在使用浮点数时产生令人困惑的结果。在这种情况下,您应该改用arange。numpy.linspace如果您希望结果包含结束点,或者您正在使用非整数步长,请使用
。numpy.linspace*可以* 包含结束点,并根据numpy.linspacenum参数(指定返回数组中的元素数量)确定步长。结束点的包含由一个可选的布尔参数
确定,该参数默认为endpoint。请注意,选择True将改变步长的计算以及该函数后续的输出。endpoint=False示例
>>> np.linspace(0.1, 0.2, num=5) # np.linspace(start, stop, num) array([0.1 , 0.125, 0.15 , 0.175, 0.2 ]) >>> np.linspace(0.1, 0.2, num=5, endpoint=False) array([0.1, 0.12, 0.14, 0.16, 0.18])
也可以与复数参数一起使用。numpy.linspace>>> np.linspace(1+1.j, 4, 5, dtype=np.complex64) array([1. +1.j , 1.75+0.75j, 2.5 +0.5j , 3.25+0.25j, 4. +0.j ], dtype=complex64)
其他示例#
如果在
中使用浮点数作为numpy.arange,可能会出现意外结果。为避免这种情况,请确保所有浮点数转换都在计算结果之后进行。例如,替换step>>> list(np.arange(0.1,0.4,0.1).round(1)) [0.1, 0.2, 0.3, 0.4] # endpoint should not be included!
为
>>> list(np.arange(1, 4, 1) / 10.0) [0.1, 0.2, 0.3] # expected result
注意
>>> np.arange(0, 1.12, 0.04) array([0. , 0.04, 0.08, 0.12, 0.16, 0.2 , 0.24, 0.28, 0.32, 0.36, 0.4 , 0.44, 0.48, 0.52, 0.56, 0.6 , 0.64, 0.68, 0.72, 0.76, 0.8 , 0.84, 0.88, 0.92, 0.96, 1. , 1.04, 1.08, 1.12])
和
>>> np.arange(0, 1.08, 0.04) array([0. , 0.04, 0.08, 0.12, 0.16, 0.2 , 0.24, 0.28, 0.32, 0.36, 0.4 , 0.44, 0.48, 0.52, 0.56, 0.6 , 0.64, 0.68, 0.72, 0.76, 0.8 , 0.84, 0.88, 0.92, 0.96, 1. , 1.04])
这些结果不同是因为数值噪声。当使用浮点数时,
可能会成立,因此0 + 0.04 * 28 < 1.12在区间内。实际上,这正是这种情况。1.12>>> 1.12/0.04 28.000000000000004
但是
,因此 1.08 被排除了。0 + 0.04 * 27 >= 1.08>>> 1.08/0.04 27.0
或者,您可以使用
,这将始终为您提供对结束点的精确控制,因为它是整数。np.arange(0, 28)*0.04>>> np.arange(0, 28)*0.04 array([0. , 0.04, 0.08, 0.12, 0.16, 0.2 , 0.24, 0.28, 0.32, 0.36, 0.4 , 0.44, 0.48, 0.52, 0.56, 0.6 , 0.64, 0.68, 0.72, 0.76, 0.8 , 0.84, 0.88, 0.92, 0.96, 1. , 1.04, 1.08])
geomspace 和 logspace#
类似于 numpy.geomspace,但数字在对数尺度上均匀分布(几何级数)。结果包含结束点。numpy.linspace
示例
>>> np.geomspace(2, 3, num=5)
array([2. , 2.21336384, 2.44948974, 2.71080601, 3. ])
类似于 numpy.logspace,但起始点和结束点以对数形式指定(默认基数为 10)。numpy.geomspace
>>> np.logspace(2, 3, num=5)
array([ 100. , 177.827941 , 316.22776602, 562.34132519, 1000. ])
在线性空间中,序列以 (base ** start 的 base 次幂)开始,并以 start 结束。base ** stop
>>> np.logspace(2, 3, num=5, base=2)
array([4. , 4.75682846, 5.65685425, 6.72717132, 8. ])
N 维域#
N 维域可以划分为*网格*。可以使用以下函数之一来完成此操作。
meshgrid#
的目的是根据一组一维坐标数组创建矩形网格。numpy.meshgrid
给定数组
>>> x = np.array([0, 1, 2, 3])
>>> y = np.array([0, 1, 2, 3, 4, 5])
将创建两个坐标数组,可用于生成确定此网格的坐标对。meshgrid
>>> xx, yy = np.meshgrid(x, y)
>>> xx
array([[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3]])
>>> yy
array([[0, 0, 0, 0],
[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4],
[5, 5, 5, 5]])
>>> import matplotlib.pyplot as plt
>>> plt.plot(xx, yy, marker='.', color='k', linestyle='none')
mgrid#
可用作创建网格的快捷方式。它不是一个函数,但当被索引时,会返回一个多维网格。numpy.mgrid
>>> xx, yy = np.meshgrid(np.array([0, 1, 2, 3]), np.array([0, 1, 2, 3, 4, 5]))
>>> xx.T, yy.T
(array([[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3]]),
array([[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5]]))
>>> np.mgrid[0:4, 0:6]
array([[[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3]],
[[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5]]])
ogrid#
与 类似,numpy.mgrid 返回一个*开放*的多维网格。这意味着当它被索引时,每个返回数组只有一个维度大于 1。这避免了数据的重复,从而节省了内存,这通常是可取的。numpy.ogrid
这些稀疏坐标网格旨在与广播一起使用。当所有坐标都在表达式中使用时,广播仍然会导致一个全维度的结果数组。
>>> np.ogrid[0:4, 0:6]
(array([[0],
[1],
[2],
[3]]), array([[0, 1, 2, 3, 4, 5]]))
这里描述的所有三种方法都可以用来在网格上计算函数值。
>>> g = np.ogrid[0:4, 0:6]
>>> zg = np.sqrt(g[0]**2 + g[1]**2)
>>> g[0].shape, g[1].shape, zg.shape
((4, 1), (1, 6), (4, 6))
>>> m = np.mgrid[0:4, 0:6]
>>> zm = np.sqrt(m[0]**2 + m[1]**2)
>>> np.array_equal(zm, zg)
True