在 Numpy 中返回等比數列上的等距數字
要返回等比數列上的等距數字,請在 Python Numpy 中使用 **numpy.geomspace()** 方法。第一個引數是“開始”,即序列的開始。第二個引數是“結束”,即序列的結束。第三個引數是 num,即要生成的樣本數。
開始是序列的起始值。停止是序列的最終值,除非 endpoint 為 False。在這種情況下,num + 1 個值在對數空間內的間隔上等距分佈,其中除了最後一個(長度為 num 的序列)之外的所有值都將被返回。endpoint 如果為真,則停止是最後一個樣本。否則,它不包括在內。預設為 True。
結果中儲存樣本的軸。僅當 start 或 stop 為陣列狀時才相關。預設情況下 (0),樣本將沿插入到開頭的新的軸。使用 -1 獲取末尾的軸。
步驟
首先,匯入所需的庫 -
import numpy as np
要返回等比數列上的等距數字,請使用 numpy.geomspace() 方法 -
arr = np.geomspace(150, 500) 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.s the number of samples to generate. Default is 50. arr = np.geomspace(150, 500) 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. 153.73128376 157.55538405 161.47460969 165.49132695 169.60796096 173.82699717 178.15098285 182.58252864 187.12431014 191.77906948 196.549617 201.43883296 206.44966927 211.58515126 216.84837953 222.24253178 227.77086478 233.43671631 239.24350717 245.19474326 251.29401769 257.54501294 263.95150311 270.51735616 277.24653628 284.14310628 291.21123 298.4551749 305.87931455 313.48813135 321.28621918 329.2782862 337.46915768 345.86377894 354.46721829 363.28467013 372.32145807 381.58303815 391.07500211 400.80308081 410.77314766 420.99122217 431.46347358 442.19622461 453.19595523 464.46930662 476.02308517 487.86426656 500. ] Type... float64 Dimensions... 1 Shape... (50,) Number of elements... 50
廣告