Interp1d
Posted : admin On 1/26/2022In MatLab the usage is slightly different:
yi = interp1(x,Y,xi,'cubic')
While in SciPy it's like this:
f = interp1d(x,Y,kind='cubic')
yi = f(xi)
For a trivial example the results are the same:
MatLab:
interp1([0 1 2 3 4], [0 1 2 3 4],[1.5 2.5 3.5],'cubic')
1.5000 2.5000 3.5000
Python:
interp1d([1,2,3,4],[1,2,3,4],kind='cubic')([1.5,2.5,3.5])
array([ 1.5, 2.5, 3.5])
But for a realworld example they are not the same:
x = 0.0000e+000 2.1333e+001 3.2000e+001 1.6000e+004 2.1333e+004 2.3994e+004
Y = 6 6 20 20 6 6
xi = 0.00000 11.72161 23.44322 35.16484
Matlab: 6.0000 12.3303 3.7384 22.7127
Python: 6. 15.63041012 2.04908267 30.43054192
Any thoughts as to how I can get results that are consistent with MatLab?
Thanks
Lynn
Series.
interpolate
(self, method='linear', axis=0, limit=None, inplace=False, limit_direction='forward', limit_area=None, downcast=None, **kwargs)[source]¶OctaveForge is a collection of packages providing extra functionality for GNU Octave. Here are the examples of the python api scipy.interpolate.interp1d taken from open source projects. By voting up you can indicate which examples are most useful and appropriate. 200 Examples prev 1 2 3 4.
Interpolate values according to different methods.
Please note that only method='linear'
is supported forDataFrame/Series with a MultiIndex.
Parameters: 


Returns: 

The SciPy documentation explains that interp1d 's kind argument can take the values ‘linear’, ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’. The last three are spline orders and 'linear' is selfexplanatory. What do 'nearest' and 'zero' do? Apr 14, 2020 For most of the interpolation methods scipy.interpolate.interp1d is used in the background. This class returns a function whose call method uses interpolation to find the value of new points. Here are some of the interpolation methods which uses scipy backend nearest, zero, slinear, quadratic, cubic, spline, barycentric, polynomial. Hi All I am a very recent member of this list so apologise if this matter has been raised previously I have an Excel workbook that uses numerous instances of Interp1d from XonGrid 64. I give this workbook free to students and colleagues with the instruction to install the addIn.
See also
fillna
 Fill missing values using different methods.
scipy.interpolate.Akima1DInterpolator
 Piecewise cubic polynomials (Akima interpolator).
scipy.interpolate.BPoly.from_derivatives
 Piecewise polynomial in the Bernstein basis.
scipy.interpolate.interp1d
 Interpolate a 1D function.
scipy.interpolate.KroghInterpolator
 Interpolate polynomial (Krogh interpolator).
scipy.interpolate.PchipInterpolator
 PCHIP 1d monotonic cubic interpolation.
scipy.interpolate.CubicSpline
 Cubic spline data interpolator.
Notes
The ‘krogh’, ‘piecewise_polynomial’, ‘spline’, ‘pchip’ and ‘akima’methods are wrappers around the respective SciPy implementations ofsimilar names. These use the actual numerical values of the index.For more information on their behavior, see theSciPy documentationand SciPy tutorial.
Examples
Filling in NaN
in a Series
via linearinterpolation.
Filling in NaN
in a Series by padding, but filling at most twoconsecutive NaN
at a time.
Filling in NaN
in a Series via polynomial interpolation or splines:Both ‘polynomial’ and ‘spline’ methods require that you also specifyan order
(int).
Interp1d Numpy
Fill the DataFrame forward (that is, going down) along each columnusing linear interpolation.
Note how the last entry in column ‘a’ is interpolated differently,because there is no entry after it to use for interpolation.Note how the first entry in column ‘b’ remains NaN
, because thereis no entry befofe it to use for interpolation.
Interp1d Extrapolate
Using polynomial interpolation.