在 NumPy 中生成等比數列上的等距數字,並設定要生成的樣本數量


要在等比數列上返回等距的數字,請在 Python NumPy 中使用 **numpy.geomspace()** 方法。第一個引數是“start”,即序列的起始值。第二個引數是“end”,即序列的結束值。第三個引數是 num,即要生成的樣本數量。預設為 50。

start 是序列的起始值。stop 是序列的最終值,除非 endpoint 為 False。在這種情況下,num + 1 個值在對數空間的區間內均勻分佈,其中除了最後一個值(長度為 num 的序列)之外的所有值都將被返回。如果 endpoint 為 True,則 stop 是最後一個樣本。否則,它不被包含。預設為 True。

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

步驟

首先,匯入所需的庫:

import numpy as np

要在等比數列上返回等距的數字,請使用 numpy.geomspace() 方法:

arr = np.geomspace(100, 200, num = 5)
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 geometric progression, use the numpy.geomspace() 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.geomspace(100, 200, num = 5)
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...
[100. 118.9207115 141.42135624 168.17928305 200. ]

Type...
float64

Dimensions...
1

Shape...
(5,)

Number of elements...
5

更新於:2022年2月16日

127 次瀏覽

啟動您的 職業生涯

完成課程獲得認證

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