How to fix np.logspace() starting and ending at wrong values

Problem:

You want to use np.logspace() to create an array of logarithmically spaced values, but the start and end values are not what you expect.

For example,

np.logspace(1e-6, 1e-3, num=10)

produces values from 1.0 to ~1.02 whereas you would expect values from 1e-6 to 1e-3.

array([1.0000023 , 1.00025792, 1.00051361, 1.00076936, 1.00102518,
       1.00128106, 1.001537  , 1.00179302, 1.00204909, 1.00230524])

Solution

np.logspace() produces values from base ** start to base ** stop, where base is the base of the logarithm (default is 10).

What you want to use is np.geomspace(), which allows you to specify the start and end values directly:

np.geomspace(1e-6, 1e-3, num=10)

This produces the expected values:

array([1.00000000e-06, 2.15443469e-06, 4.64158883e-06, 1.00000000e-05,
       2.15443469e-05, 4.64158883e-05, 1.00000000e-04, 2.15443469e-04,
       4.64158883e-04, 1.00000000e-03])