Python numpy.geomspace
numpy.geomspace() 在对数刻度上返回一组等间距的数字(几何级数)。
- Linspace - 与geomspace类似,但端点使用对数和底数来指定。
-
Logspace - 与geomspace类似,但端点使用算术而不是几何级数来指定。
语法
numpy.goemspace(start, stop, num = 50, endpoint = True/False, dtype = None)
参数
上述函数可以接受以下参数:
- start - 序列的起始值;默认为零。
-
stop - 序列的结束值。
-
num - 在起始值和结束值之间生成的元素数量。
-
endpoint - 控制停止值是否包含在输出数组中。如果 endpoint=True ,则停止参数将作为 nd.array 的最后一项。如果 endpoint=False ,则停止参数将不包含在数组中。
-
dtype - 描述输出数组的类型。
示例1
让我们考虑以下示例 –
# Import numpy
import numpy as np
# geomspace() function
x = np.geomspace(1, 2000, num=8)
print ("geomspace of X: \n", x)
输出
它将生成以下输出 –
geomspace of X:
[1.00000000e+00 2.96193630e+00 8.77306662e+00 2.59852645e+01
7.69666979e+01 2.27970456e+02 6.75233969e+02 2.00000000e+03]
示例2
让我们考虑以下示例−
# Import numpy
import numpy as np
# geomspace() function
x = np.geomspace(2, 800, num = 9, endpoint = False)
print ("geomspace of X :\n", x)
输出
上述程序将生成以下输出-
geomspace of X :
[ 2. 3.89177544 7.57295802 14.73612599 28.67484658
55.79803176 108.57670466 211.27807602 411.12341312]
在这里,我们设置了 endpoint=False ,因此停止参数不包括在序列中。