Pour générer une liste de nombres espacés uniformément sur une échelle logarithmique avec python on peut passer par la fonction logspace de numpy. Par exemple pour générer 100 nombres entre $10^{0.1}$ et $10^{3}$:
Table des matières
Avec la fonction numpy logspace
>>> import numpy as np
>>> l = np.logspace(0.1, 3, 100, endpoint=True)
>>> l
array([ 1.25892541, 1.34676844, 1.44074081, 1.54127022,
1.64881419, 1.76386217, 1.88693776, 2.0186011 ,
2.1594514 , 2.3101297 , 2.47132176, 2.64376119,
2.82823278, 3.0255761 , 3.23668929, 3.46253315,
3.70413553, 3.96259601, 4.23909088, 4.53487851,
4.85130507, 5.18981068, 5.55193591, 5.93932887,
6.35375264, 6.79709332, 7.27136862, 7.77873705,
8.32150772, 8.90215085, 9.52330906, 10.18780932,
10.89867589, 11.65914401, 12.47267471, 13.34297048,
14.27399218, 15.26997703, 16.33545792, 17.475284 ,
18.69464281, 19.99908384, 21.3945438 , 22.88737364,
24.48436747, 26.19279345, 28.02042689, 29.97558564,
32.06716793, 34.30469286, 36.69834378, 39.25901454,
41.99835916, 44.92884483, 48.06380863, 51.41751828,
55.00523702, 58.84329312, 62.94915417, 67.34150658,
72.04034062, 77.06704142, 82.44448627, 88.19714876,
94.35121014, 100.9346785 , 107.97751623, 115.51177638,
123.57174852, 132.19411485, 141.41811708, 151.28573508,
161.84187792, 173.13458823, 185.21526088, 198.1388769 ,
211.96425366, 226.75431259, 242.57636554, 259.50242114,
277.60951248, 296.98004774, 317.70218523, 339.87023462,
363.58508612, 388.9546697 , 416.09444628, 445.12793318,
476.1872663 , 509.41380148, 544.95875783, 582.98390597,
623.66230422, 667.17908629, 713.7323038 , 763.53382765,
816.81031231, 873.8042273 , 934.77496078, 1000. ])
Tracer avec matplotlib
Code pour visualiser le résultat:
import numpy as np
import matplotlib.pyplot as plt
l = np.logspace(0.1, 3, 100, endpoint=True)
plt.scatter([i for i in range(l.shape[0])], l)
plt.title(u'List of logarithmically spaced numbers with python')
plt.grid()
plt.savefig("log_scale_list_numbers.png",bbox_inches='tight')
plt.show()
Références
Liens | Site |
---|---|
numpy.logspace | doc scipy |
logarithmically spaced integers | stackoverflow |
How to generate exponentially increasing range in Python | stackoverflow |