So I have a little problem with linspace. I would like to generate an array of numbers like:
[0.000001, 0.00001, 0.0001 , 0.001 ,0 .01 , 0.1]
so I've tried following code:
alphas = np.linspace(0.0000001,10,num=11)
print(alphas)
and got result:
[ 1.00000000e-07 1.00000009e+00 2.00000008e+00 3.00000007e+00
4.00000006e+00 5.00000005e+00 6.00000004e+00 7.00000003e+00
8.00000002e+00 9.00000001e+00 1.00000000e+01]
than i thought that it must be problem with display and format but after trying
if(alphas[0]>1):
print("yes the first number is greater than 1")
if(alphas[1]>1):
print("yes the second number is greater than 1")
the second number is really greater than one
so my question is what's wrong? since linspace should "Return evenly spaced numbers over a specified interval."
It's doing what you are asking: Creating a linearly increasing set of numbers between 1e-7 and 10. Since this is almost the same as the range 0 to 10, then it's not surprising that you get an increment of one.
What you want in your case is np.logspace
which gives you a logarithmic increase instead:
In [2]: np.logspace(-7, 1, num=11)
Out[2]:
array([ 1.00000000e-07, 6.30957344e-07, 3.98107171e-06,
2.51188643e-05, 1.58489319e-04, 1.00000000e-03,
6.30957344e-03, 3.98107171e-02, 2.51188643e-01,
1.58489319e+00, 1.00000000e+01])
As pointed out by Warren Weckesser, as of NumPy 1.12 there is also the np.geomspace
function that is slightly easier to use, as you can give it the endpoints directly, instead of its power-of-ten exponent:
np.geomspace(1e-7, 10, num=11)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With