简体   繁体   中英

Cubic Spline function in scipy.interpolate returns a numpy.ndarray

I need to use CubicSpline to interpolated between points. This is my function

cs = CubicSpline(aTime, aControl)
u = cs(t)   # u is a ndarray of one element.

I cannot convert u to a float. uu = float(u) or uu = float(u[0]) doesn't work in the function. I can convert u to a float in the shell by float(u) . This shouldn't work because I have not provided an index but I get an error if I use u[0] . I have read something about np.squeeze . I tried it but it didn't help.

I added a print ("u=",u) statement after the u=cs(t) . The result was

u= [  1.88006889e+09   5.39398193e-01   5.39398193e-01]

How can this be? I expect 1 value. The second and third numbers look about right.

I found the problem. Programming error, of course but the error messages I got were very misleading. I was calling the interpolate function with 3 values so it returned three vales. Why I couldn't get just the one afterwards is still a mystery but now that I call the interpolate with just one value I get one float as expected. Overall this still didn't help as the interpolate1d function is too slow. I wrote my own cubic interpolate function that is MUCH faster.

Again, programming error and poor error messages were the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM