在 Numpy 中使用幾何級數返回均勻間隔的數字,但輸入為負數
要返回幾何級數上的均勻間隔的數字,請在 Python Numpy 中使用 **numpy.geomspace()** 方法 -
- 第一個引數是“起始值”,即序列的開始
- 第二個引數是“結束值”,即序列的結束
- 第三個引數是 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(-150, -10, num = 5) print("Array...
", arr)
獲取型別 -
print("
Type...
", arr.dtype)
獲取維度 -
print("
Dimensions...
",arr.ndim)
獲取形狀 -
Get the 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.s the number of samples to generate. Default is 50. # We have set negative inputs arr = np.geomspace(-150, -10, num = 5) print("Array...
", arr) # Get the 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... [-150. -76.21991222 -38.72983346 -19.67989671 -10. ] Type... float64 Dimensions... 1 Shape... (5,) Number of elements... 5
廣告