[SciPy-User] Return type of scipy.interpolate.splev for input array of length 1

Yves Frederix yves.frederix@gmail....
Sun Jan 17 04:25:40 CST 2010


I stumbled upon the following unlogical behavior of
scipy.interpolate.splev. When presented with a length-1 array, the
output is converted to a scalar.

import scipy.interpolate
import numpy as N

x = N.arange(5.)
y = N.arange(5.)
tck = scipy.interpolate.splrep(x,y)

x_eval = N.asarray([1.])
y_eval = scipy.interpolate.splev(x_eval, tck)

print 'scipy.interpolate.splev(x_eval, tck):', y_eval
print 'type(x_eval):', type(x_eval)
print 'type(y_eval):', type(y_eval)

with output

scipy.interpolate.splev(x_eval, tck): 1.0
type(x_eval): <type 'numpy.ndarray'>
type(y_eval): <type 'numpy.float64'>

It was rather unexpected that the type of input and output data are
different. After checking interpolate/fitpack.py it seems that this
behavior results from the fact that the length-1 case is explicitly
treated differently (probably to be able to deal with the case of
scalar input, for which scalar output is expected):

 434 def splev(x,tck,der=0):
 487         if ier: raise TypeError,"An error occurred"
 488         if len(y)>1: return y
 489         return y[0]

Wouldn't it be less confusing to have the return value always have the
same type as the input data?


More information about the SciPy-User mailing list