Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python SciPy UnivariateSpline returns NaN - value in range

Tags:

python

scipy

I'm trying to use SciPy's UnivariateSpline to locate a point on a curve. Unfortunately, my result is nan.
Here's a minimal example:

from scipy.interpolate import UnivariateSpline  

spline = UnivariateSpline([0.6, 0.4, 0.2, 0.0], [-0.3, -0.1, 0.1, 0.3], w=None, bbox=[None, None], k=1, s=0)  
POINT = spline([0.15])  
print POINT  

The result is [ NaN].
Which feature of UnivariateSpline did I miss?

I'm using Python 2.6.6 and scipy version 0.7.2

I cannot guarantee that I have always increasing datapoints so interp might not be an alternative.

like image 512
Schorsch Avatar asked Apr 11 '26 00:04

Schorsch


1 Answers

As the docstring for UnivariateSpline states, the values in x must be increasing. You'll have to sort your data if you want to use UnivariateSpline. E.g. something like this:

In [71]: x = np.array([0.6, 0.4, 0.2, 0.0])

In [72]: y = np.array([-0.3, -0.1, 0.1, 0.3])

In [73]: order = np.argsort(x)

In [74]: spline = UnivariateSpline(x[order], y[order], w=None, bbox=[None, None], k=1, s=0)

In [75]: spline([0.15])
Out[75]: array([ 0.15])
like image 108
Warren Weckesser Avatar answered Apr 13 '26 13:04

Warren Weckesser