在对数刻度上返回等间距的数字,并设置在Numpy中生成的样本数量


要在对数刻度上返回等间距的数字,请使用 Python Numpy 中的 **numpy.logspace()** 方法。第一个参数是“**start**”,即序列的起始值。第二个参数是“**end**”,即序列的结束值。第三个参数是 num,即要生成的样本数量。默认为 50。

在线性空间中,序列从基数 **start**(基数的start次方) 开始,以基数 **stop**(参见下面的endpoint) 结束。start 是基数的start次方,是序列的起始值。stop 是基数的stop次方,是序列的最终值,除非 endpoint 为 False。在这种情况下,num + 1 个值在对数空间的区间内均匀分布,其中除了最后一个值之外的所有值都会返回。对数空间的基数。ln(samples) / ln(base)(或 log_base(samples))中元素之间的步长是均匀的。默认为 10.0。

结果中用于存储样本的轴。仅当 start 或 stop 为数组状时才相关。默认情况下 (0),样本将沿着在开头插入的新轴排列。使用 -1 可在末尾获得一个轴。

步骤

首先,导入所需的库:

import numpy as np

要在对数刻度上返回等间距的数字,请使用 numpy.logspace() 方法:

arr = np.logspace(100.0, 200.0, num = 10)
print("Array...
", arr)

获取数组类型:

print("
Type...
", arr.dtype)

获取数组的维度:

print("
Dimensions...
",arr.ndim)

获取数组的形状:

print("
Shape...
",arr.shape)

获取元素数量:

print("
Number of elements...
",arr.size)

示例

import numpy as np

# To return evenly spaced numbers on a log scale, use the numpy.logspace() method in Python Numpy
# The 1st parameter is the "start" i.e. the start of the sequence
# The 2nd parameter is the "end" i.e. the end of the sequence
# The 3rd parameter is the num i.e the number of samples to generate. Default is 50.
arr = np.logspace(100.0, 200.0, num = 10)
print("Array...
", arr) # Get the array type print("
Type...
", arr.dtype) # Get the dimensions of the Array print("
Dimensions...
",arr.ndim) # Get the shape of the Array print("
Shape...
",arr.shape) # Get the number of elements print("
Number of elements...
",arr.size)

输出

Array...
[1.00000000e+100 1.29154967e+111 1.66810054e+122 2.15443469e+133
2.78255940e+144 3.59381366e+155 4.64158883e+166 5.99484250e+177
7.74263683e+188 1.00000000e+200]

Type...
float64

Dimensions...
1

Shape...
(10,)

Number of elements...
10

更新于:2022年2月16日

1K+ 浏览量

启动您的 职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.