NumPy interp()

The numpy.interp() function performs one-dimensional linear interpolation for a set of given data points. It estimates the value of a function at intermediate points based on known discrete values.

Syntax

</>
Copy
numpy.interp(x, xp, fp, left=None, right=None, period=None)

Parameters

ParameterTypeDescription
xarray_likeThe x-coordinates where interpolation is performed.
xp1-D sequence of floatsThe x-coordinates of known data points. Must be increasing unless period is specified.
fp1-D sequence of float or complexThe corresponding y-values of the known data points.
leftfloat or complex, optionalValue to return when x is less than the smallest value in xp. Default is fp[0].
rightfloat or complex, optionalValue to return when x is greater than the largest value in xp. Default is fp[-1].
periodNone or float, optionalDefines a periodic cycle for xp, normalizing values before interpolation.

Return Value

Returns an array of interpolated values corresponding to the given x values. The output has the same shape as x.


Examples

1. Basic Linear Interpolation

Performing interpolation at a given point using known data points.

</>
Copy
import numpy as np

# Define known data points
xp = np.array([1, 2, 3, 4, 5])  # x-coordinates
fp = np.array([2, 4, 6, 8, 10])  # Corresponding y-values

# Interpolate for a new x-value
x = 2.5
result = np.interp(x, xp, fp)

print("Interpolated value at x =", x, ":", result)

Output:

Interpolated value at x = 2.5 : 5.0

2. Interpolating Multiple Values

Interpolating multiple x-values at once.

</>
Copy
import numpy as np

# Define known data points
xp = np.array([0, 1, 2, 3, 4])
fp = np.array([0, 10, 20, 30, 40])

# Interpolate multiple x-values
x_values = np.array([0.5, 1.5, 2.5, 3.5])
interpolated_values = np.interp(x_values, xp, fp)

print("Interpolated values:", interpolated_values)

Output:

Interpolated values: [ 5. 15. 25. 35.]

3. Using left and right Parameters

Handling values outside the interpolation range.

</>
Copy
import numpy as np

# Define known data points
xp = np.array([1, 2, 3, 4, 5])
fp = np.array([10, 20, 30, 40, 50])

# Define x-values, some are outside xp's range
x_values = np.array([0, 2.5, 6])

# Interpolate with left and right values specified
interpolated_values = np.interp(x_values, xp, fp, left=5, right=55)

print("Interpolated values with left and right parameters:", interpolated_values)

Output:

Interpolated values with left and right parameters: [ 5.  25. 55.]

4. Using period Parameter for Cyclic Interpolation

Using the period parameter to handle periodic data.

</>
Copy
import numpy as np

# Define known data points for a periodic function
xp = np.array([0, 90, 180, 270, 360])  # Angles in degrees
fp = np.array([0, 1, 0, -1, 0])  # Corresponding sine values

# Define x-values outside the given range
x_values = np.array([450, 540])  # These should wrap within the 0-360 range

# Perform periodic interpolation
interpolated_values = np.interp(x_values, xp, fp, period=360)

print("Interpolated values with periodicity:", interpolated_values)

Output:

Interpolated values with periodicity: [-1.  0.]

The period=360 ensures that values are wrapped within the range of known data points, allowing cyclic interpolation.