Reputation: 43
in my current project I have currently a conversion issue: In a first step I am interpolating two lists with scipy interp1d.
f_speed = scipy.interpolate.interp1d(t, v, 'cubic')
After that I want to get one specific point in this function. This value is now an array from scipy with only one value in it.
currentSpeed = f_speed(tpoint)
# result: array(15.1944)
As a last step I want to calculate other values with the value from the interpolated function, but I only get 0 as a value. There is no Error at all.
real_distance = currentSpeed * (1/15) # some calculations
# result: 0, all results with further calculations are zero
I need a conversion from scipy array to a regular float value to proceed my calculations. Is there any function to do this?
I tried several things like V = currentSpeed[0], or .tolist from numpy (not possible in scipy)...
Thanks for your help in advance!!
Upvotes: 1
Views: 1363
Reputation: 153
Are you using Python 2? If so the problem is the division.
Integer division will result in an integer so 1/15
will result in 0
.
Try 1.0/15
instead. By using 1.0
you make it explicitly a float an then the result will be as expected.
Upvotes: 1
Reputation: 1832
You did not specify which Python version you're using, but if you're using Python 2.7, then operator /
stands for integer division. It means that 1/15
will produce 0. Multiplying something with this results will end up being 0, regardless of how you access array values.
To solve the problem, make sure at least one operand is a float number.
result1 = 1/15 # Produces 0
result2 = 1.0/15 # Produces 0.06666666666666667
If you apply this to your code, you should use
real_distance = currentSpeed * (1.0/15)
Upvotes: 4