numpy.geomspace
numpy.geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0)[source]
返回数字以对数刻度(几何级数)均匀分布。
这类似于 logspace,但是直接指定了端点。 每个输出样本是前一个样本的恒定倍数。
Changed in version 1.16.0: Non-scalar start and stop are now supported.
参数 : | start :array_like 序列的起始值。 stop :array_like 序列的最终值,除非端点为
返回除最后一个(长度为num的序列)外的所有值。 num : 要生成的样本数。 默认值为 endpoint : 如果为 否则,不包括在内。 默认值为 dtype : 输出数组的类型。 如果未给出 则从其他输入参数推断数据类型。 axis : 结果中的轴用于存储样本。 仅当start或stop类似于数组时才相关。 默认情况下为( 使用 |
返回值 : | samples :ndarray num个samples,以对数刻度等距分布。 |
Notes
如果输入或dtype是复数,则输出将在复数平面中遵循对数螺旋。 (有无限数量的螺旋线穿过两个点;输出将遵循最短的这种路径。)
例子
>>> np.geomspace(1, 1000, num=4) array([ 1., 10., 100., 1000.]) >>> np.geomspace(1, 1000, num=3, endpoint=False) array([ 1., 10., 100.]) >>> np.geomspace(1, 1000, num=4, endpoint=False) array([ 1. , 5.62341325, 31.6227766 , 177.827941 ]) >>> np.geomspace(1, 256, num=9) array([ 1., 2., 4., 8., 16., 32., 64., 128., 256.])
请注意,以上可能不会产生确切的整数:
>>> np.geomspace(1, 256, num=9, dtype=int) array([ 1, 2, 4, 7, 16, 32, 63, 127, 256]) >>> np.around(np.geomspace(1, 256, num=9)).astype(int) array([ 1, 2, 4, 8, 16, 32, 64, 128, 256])
允许负,递减和复杂的输入:
>>> np.geomspace(1000, 1, num=4) array([1000., 100., 10., 1.]) >>> np.geomspace(-1000, -1, num=4) array([-1000., -100., -10., -1.]) >>> np.geomspace(1j, 1000j, num=4) # Straight line array([0. +1.j, 0. +10.j, 0. +100.j, 0.+1000.j]) >>> np.geomspace(-1+0j, 1+0j, num=5) # Circle array([-1.00000000e+00+1.22464680e-16j, -7.07106781e-01+7.07106781e-01j, 6.12323400e-17+1.00000000e+00j, 7.07106781e-01+7.07106781e-01j, 1.00000000e+00+0.00000000e+00j])
endpoint
parameter的图形化图示:
>>> import matplotlib.pyplot as plt >>> N = 10 >>> y = np.zeros(N) >>> plt.semilogx(np.geomspace(1, 1000, N, endpoint=True), y + 1, 'o') [<matplotlib.lines.Line2D object at 0x...>] >>> plt.semilogx(np.geomspace(1, 1000, N, endpoint=False), y + 2, 'o') [<matplotlib.lines.Line2D object at 0x...>] >>> plt.axis([0.5, 2000, 0, 3]) [0.5, 2000, 0, 3] >>> plt.grid(True, color='0.7', linestyle='-', which='both', axis='both') >>> plt.show()