In 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 real-world 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
- F = interpolate.interp1d(x, y) Interpolation -Example! '1 3 2 2 3 0 Assume the following Data:! ' We can plot the data points like this: Assume we want to find the value for 'when!=2.5 From the plot we see that '=1is a good guess. Interpolation -Example import numpyas np import matplotlib.pyplotas plt.
- Interpolate.interp1d will return nan if the minimum x value is duplicated. Additional information. The solution is implemented by removing duplicate value of xmin when assumesorted is set to false; testing is done; this should be considered a bug because duplicate values of x that aren't the minimum value do not result in nans being returned.
- Scipy.interpolate.interp1d¶ class scipy.interpolate.interp1d(x, y, kind='linear', axis=-1, copy=True, boundserror=True, fillvalue=np.nan, assumesorted=False) source ¶ Interpolate a 1-D function. X and y are arrays of values used to approximate some function f: y = f(x). This class returns a function whose call method uses interpolation.
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.
Series.
interpolate
(self, method='linear', axis=0, limit=None, inplace=False, limit_direction='forward', limit_area=None, downcast=None, **kwargs)[source]¶Interp1d
Interpolate values according to different methods.
Scipy.interpolate.interpolate.interp1d
Please note that only method='linear'
is supported forDataFrame/Series with a MultiIndex.
Parameters: |
|
---|---|
Returns: |
|
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 1-D function.
scipy.interpolate.KroghInterpolator
- Interpolate polynomial (Krogh interpolator).
scipy.interpolate.PchipInterpolator
- PCHIP 1-d monotonic cubic interpolation.
scipy.interpolate.CubicSpline
- Cubic spline data interpolator.
Parameters: |
|
---|---|
Returns: |
|
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 1-D function.
scipy.interpolate.KroghInterpolator
- Interpolate polynomial (Krogh interpolator).
scipy.interpolate.PchipInterpolator
- PCHIP 1-d 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.
Numpy Interp 1d
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.
Using polynomial interpolation.