Odisseo
Odisseo

Reputation: 777

Numpy Logspace - Why No Negatives?

I would like to easily create arrays on a log scale and I have used

for c in [np.logspace(1,3,num = 3, base=10, dtype=int)]:
    print(c)

which works succesfully for positive exponents. However, what if I want to start from negative exponents i.e. 0.01? Why does

for c in [np.logspace(-2,3,num = 6, base=10, dtype=int)]:
    print(c)

produce:

[   0    0    1   10  100 1000]

rather than

[   0.01    0.1    1   10  100 1000]

and most importantly is there a way to get the above result with a numpy or similar library rather than using something like this:

[10**i for i in range(-2,3)]

Upvotes: 0

Views: 1766

Answers (1)

Primusa
Primusa

Reputation: 13498

In np.logspace() you added a dtype=int keyword argument, which makes the function only return integers.

Just remove that argument:

np.logspace(-2, 3, num=6, base=10)
>>>array([1.e-02, 1.e-01, 1.e+00, 1.e+01, 1.e+02, 1.e+03])

Upvotes: 4

Related Questions