Numpy interp() function returns the one-dimensional piecewise linear interpolant to a function with given discrete data points (xp, fp), evaluated at x.
np.interp
The np.interp() is a numpy mathematical library function that returns one-dimensional linear interpolation. The interp() function accepts five arguments which are x, xp, fp, left, right, and period and returns float or complex (corresponding to fp) or ndarray.
The np.interp() function does not check that the x-coordinate sequence xp is increasing. If the xp is not increasing, the results are nonsense. A simple check for increasing is the following.
np.all(np.diff(xp) > 0)
Let’s see the syntax of the interp() function.
Syntax
numpy.interp(x, xp, fp, left=None, right=None, period=None)
Parameters
x: array_like
The x-coordinates at which to evaluate the interpolated values.
xp: 1-D sequence of floats
The x-coordinates of data points must be increased if the argument period is not specified. Otherwise, the xp is internally sorted after normalizing periodic boundaries with xp = xp % period.
fp: 1-D sequence of float or complex
The y-coordinates of the data points, the same length as xp.
left: Optional float or complex corresponding to fp
Value to return for x < xp[0], default is fp[0].
right: Optional float or complex corresponding to fp
Value to return for x > xp[-1], default is fp[-1].
period: None or float, optional
A period for the x-coordinates. This parameter allows the proper interpolation of angular x-coordinates. Parameters left and right are ignored if the period is specified.
Return Value
The function returns float or complex (corresponding to fp) or ndarray.
Raises exception
The function can raise the ValueError exception if xp and fp have different lengths. If xp or fp is not 1D sequences, If period == 0.
Example of interp() function
See the following code.
# app.py import numpy as np xp = [11, 21, 31] fp = [31, 21, 0] interpol = np.interp(2.5, xp, fp) print(interpol)
Output
python3 app.py 31.0
Now, let’s plot an interpolant to the sine function.
Write the following code inside the app.py file.
# app.py import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2*np.pi, 10) y = np.sin(x) xvals = np.linspace(0, 2*np.pi, 50) yinterp = np.interp(xvals, x, y) plt.plot(x, y, 'o') plt.plot(xvals, yinterp, '-x') plt.show()
Output
Interpolation with periodic x-coordinate in Python
See the following code.
# app.py import numpy as np x = [-180, -170, -185, 185, -10, -5, 0, 365] xp = [190, -190, 350, -350] fp = [5, 10, 3, 4] interpol = np.interp(x, xp, fp, period=360) print(interpol)
Output
python3 app.py [7.5 5. 8.75 6.25 3. 3.25 3.5 3.75]
Numpy complex interpolation
If you don’t know complex numbers, then check out the Python complex() function.
See the following code.
# app.py import numpy as np x = [1.5, 4.0] xp = [2, 3, 5] fp = [1.0j, 0, 2+3j] interpol = np.interp(x, xp, fp) print(interpol)
Output
python3 app.py [0.+1.j 1.+1.5j]
Finally, the Numpy interp function example is over.