I am trying to get a shifting array containing 200 values in a range with a difference of 40.
Therefore i am using numpy.arange(a, b, 0.2)
with starting values a=0
and b=40
and going upwards (a=0.2 b=40.2
, a=0.4 b=40.4
and so on).
When I reach numpy.arange(25.4, 65.4, 0.2)
however I suddenly get an array with a length 201 values:
x = numpy.arange(25.2, 65.2, 0.2)
print(len(x))
Returns 200
x = numpy.arange(25.4, 65.4, 0.2)
print(len(x))
Returns 201
I got so far to notice that this happens probably due to rounding issues because of the data type...
I know there is a option 'dtype' in numpy.arrange()
:
numpy.arange(star, stop, step, dtype)
The question is which data type would fit this problem and why? (I am not so confident with data types jet and https://docs.scipy.org/doc/numpy/reference/generated/numpy.dtype.html#numpy.dtype hasn't helped me to get this issue resolved. Please help!
np.arange
is most useful when you want to precisely control the difference between adjacent elements. np.linspace
, on the other hand, gives you precise control over the total number of elements. It sounds like you want to use np.linspace
instead:
import numpy as np
offset = 25.4
x = np.linspace(offset, offset + 40, 200)
print(x)
print(len(x))
Here's the documentation page for np.linspace
: https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html