我正在嘗試生成一個幾何序列,類似于 1、2、4、8 ......
我有以下代碼:
import numpy as np
lower_price = 1
upper_price = 2
total_grids = 10
grid_box = np.linspace(lower_price , upper_price, total_grids, retstep=True)
print(grid_box)
這輸出:
(array([1. , 1.11111111, 1.22222222, 1.33333333, 1.44444444,
1.55555556, 1.66666667, 1.77777778, 1.88888889, 2. ]), 0.1111111111111111)
此代碼創建算術序列,而不是幾何序列。如何修復此代碼以生成后者而不是前者?
uj5u.com熱心網友回復:
您正在尋找np.logspace,而不是np.linspace:
例如,
# Lower bound is 2**0 == 1
# Upper bound is 2**10 == 1024
np.logspace(0, 10, 10, base=2)
輸出:
[1.00000000e 00 2.16011948e 00 4.66611616e 00 1.00793684e 01
2.17726400e 01 4.70315038e 01 1.01593667e 02 2.19454460e 02
4.74047853e 02 1.02400000e 03]
如果您試圖獲得 1 和 2 之間的 10 個值,請使用:
# Lower bound is 2**0 == 1
# Upper bound is 2**1 == 2
np.logspace(0, 1, 10, base=2)
uj5u.com熱心網友回復:
'percentage' 給出每個值之間的百分比增量。您可以看到它對于常量 total_grids 保持不變,并且只有在您更改它時才會更改。
import numpy as np
lower_price = 10
upper_price = 2000
total_grids = 10
grid_box = np.linspace(lower_price , upper_price, total_grids, retstep=True)
full_range = upper_price - lower_price
correctedStartValue = grid_box[0][1] - lower_price
percentage = (correctedStartValue * 100) / full_range
print(grid_box)
print(percentage)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/454666.html
上一篇:如何在Python中使用find_peaks()查找重復模式的一系列最高峰?
下一篇:如何從串列串列中制作作物
