在 NumPy 中以對數刻度返回等間距數字並設定基數


要在對數刻度上返回等間距的數字,請使用 Python NumPy 中的 **numpy.logspace()** 方法。第一個引數是“起始值”,即序列的起始值。第二個引數是“結束值”,即序列的結束值。第三個引數是“數量”,即要生成的樣本數,預設為 50。第四個引數是“基數”,即對數空間的基數。元素之間在 ln(樣本)/ln(基數)(或 log_base(樣本))中的步長是均勻的。

線上性空間中,序列從 base ** start(基數的起始值次方)開始,以 base ** stop(參見下面的 endpoint)結束。起始值是 base ** start,是序列的起始值。結束值是 base ** stop,是序列的最終值,除非 endpoint 為 False。在這種情況下,將在對數空間的區間上間隔 num + 1 個值,其中返回所有值,最後一個值除外。對數空間的基數。元素之間在 ln(樣本)/ln(基數)(或 log_base(樣本))中的步長是均勻的。預設為 10.0。

結果中儲存樣本的軸。僅當 start 或 stop 是陣列狀時才相關。預設情況下 (0),樣本將沿在開頭插入的新軸排列。使用 -1 可在末尾獲得一個軸。

步驟

首先,匯入所需的庫:

import numpy as np

要在對數刻度上返回等間距的數字,請使用 numpy.logspace() 方法。第一個引數是“起始值”,即序列的起始值:

arr = np.logspace(10.0, 20.0, num = 3, base = 2.0)
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.
# The 4th parameter is the "base" i.e. the base of the log space.
# The step size between the elements in ln(samples) / ln(base) (or log_base(samples)) is uniform. Default is 10.0.
arr = np.logspace(10.0, 20.0, num = 3, base = 2.0)
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.024000e+03 3.276800e+04 1.048576e+06]

Type...
float64

Dimensions...
1

Shape...
(3,)

Number of elements...
3

更新於:2022年2月16日

138 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.