在 Numpy 中返回幾何級數上的等間距數字,並且不設定端點


要返回幾何級數上的等間距數字,請在 Python Numpy 中使用 **numpy.geomspace()** 方法 -

  • 第一個引數是“start”,即序列的開始
  • 第二個引數是“end”,即序列的結束
  • 第三個引數是“num”,即要生成的樣本數量。預設為 50。
  • 第四個引數是“endpoint”。如果為 True,則 stop 是最後一個樣本。否則,它不包含在內。預設為 True。

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

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

步驟

首先,匯入所需的庫 -

import numpy as np

要返回幾何級數上的等間距數字,請使用 numpy.geomspace() 方法 -

arr = np.geomspace(10, 10000, num = 3, endpoint = False)
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.
# The 4th parameter is the "endpoint". If True, stop is the last sample. Otherwise, it is not included. Default is True.
arr = np.geomspace(10, 10000, num = 3, endpoint = False)
print("Array...
", arr) # Get the array type print("
Type...
", arr.dtype) # Get the dimensions print("
Dimensions...
",arr.ndim) # Get the shape print("
Shape...
",arr.shape) # Get the number of elements print("
Number of elements...
",arr.size)

輸出

Array...
[ 10. 100. 1000.]

Type...
float64

Dimensions...
1

Shape...
(3,)

Number of elements...
3

更新於: 2022-02-16

149 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告