🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEscipy

scipy Documentation

LOADING ENGINE...

interpolate.interp1d()

AI & DATA SCIENCE // interpolate-interp1d

scipy.interpolate.interp1d() creates a callable function that estimates values between known data points along one dimension, using linear interpolation by default.

Syntax

scipy.interpolate.interp1d(x, y, kind='linear')

Deep Dive Course

interp1d() takes arrays of known x and y data points and returns a new function you can call with any x value within the original data's range, returning an estimated y value based on the surrounding known points — 'linear', the default, connects consecutive points with straight lines, while kind='cubic', or other options, fits smoother curves through the points instead. By default, calling the resulting function with an x value outside the original data's range raises an error, since extrapolating beyond known data is a fundamentally different, riskier operation than interpolating within it.

1Understanding interpolate.interp1d()

interp1d() takes arrays of known x and y data points and returns a new function you can call with any x value within the original data's range, returning an estimated y value based on the surrounding known points — 'linear', the default, connects consecutive points with straight lines, while kind='cubic', or other options, fits smoother curves through the points instead. By default, calling the resulting function with an x value outside the original data's range raises an error, since extrapolating beyond known data is a fundamentally different, riskier operation than interpolating within it.

💡

By default, interp1d() raises an error for any x value outside the original data's range, rather than silently extrapolating — pass fill_value and bounds_error=False explicitly if you specifically want it to extrapolate, or return a placeholder, instead of erroring on out-of-range values.

editor.html
from scipy.interpolate import interp1d
import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([0, 10, 20, 30])
f = interp1d(x, y)
print(f(1.5))
localhost:3000

2Practical Example

Here is a real-world application of interpolate.interp1d() showing how it is used in production SciPy code.

editor.html
from scipy.interpolate import interp1d
import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([0, 1, 4, 9])
f = interp1d(x, y, kind="cubic")
print(round(float(f(1.5)), 2))
localhost:3000

3Best Practices

Follow these guidelines when working with interpolate.interp1d():

1. Use kind='cubic', or another smooth option, instead of the default linear interpolation when you need a smoother estimated curve through the data, not just straight-line segments

2. Leave the default error-on-extrapolation behavior in place unless you specifically intend to extrapolate, since estimating far beyond known data is much less reliable than interpolating within it

3. Pass fill_value and bounds_error=False explicitly when you do need extrapolation or a specific placeholder value for out-of-range inputs, rather than letting the default error occur unexpectedly

⚠️

Tip: By default, interp1d() raises an error for any x value outside the original data's range, rather than silently extrapolating — pass fill_value and bounds_error=False explicitly if you specifically want it to extrapolate, or return a placeholder, instead of erroring on out-of-range values.

editor.html
from scipy.interpolate import interp1d
import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([0, 10, 20, 30])
f = interp1d(x, y)
print(f(1.5))
localhost:3000

Examples

Example 01Basic Usage
from scipy.interpolate import interp1d
import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([0, 10, 20, 30])
f = interp1d(x, y)
print(f(1.5))
Example 02Advanced Example
from scipy.interpolate import interp1d
import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([0, 1, 4, 9])
f = interp1d(x, y, kind="cubic")
print(round(float(f(1.5)), 2))

Best Practices

  • Use kind='cubic', or another smooth option, instead of the default linear interpolation when you need a smoother estimated curve through the data, not just straight-line segments
  • Leave the default error-on-extrapolation behavior in place unless you specifically intend to extrapolate, since estimating far beyond known data is much less reliable than interpolating within it
  • Pass fill_value and bounds_error=False explicitly when you do need extrapolation or a specific placeholder value for out-of-range inputs, rather than letting the default error occur unexpectedly

Interview Question

Why does interp1d() raise an error by default if you call the resulting function with a value outside the original x range?

Hint: Think about the difference between interpolating within known data and extrapolating beyond it.

Interpolating between two known data points is a fundamentally well-supported estimate, since the true value is bounded and informed by real, nearby observations on both sides. Extrapolating beyond the edge of the known data has no such support — there's no data beyond that boundary to inform the estimate, and the true underlying pattern could easily change in ways the known data gives no indication of. interp1d() raises an error by default specifically to prevent silently returning what could be a wildly unreliable extrapolated guess without explicit awareness that extrapolation, not interpolation, is actually happening.

Exercises

MediumPractice using interpolate.interp1d() in a real scenario.
View Solution
from scipy.interpolate import interp1d
import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([0, 10, 20, 30])
f = interp1d(x, y)
print(f(1.5))

Frequently Asked Questions

Why does interp1d() raise an error by default if you call the resulting function with a value outside the original x range?

Interpolating between two known data points is a fundamentally well-supported estimate, since the true value is bounded and informed by real, nearby observations on both sides. Extrapolating beyond the edge of the known data has no such support — there's no data beyond that boundary to inform the estimate, and the true underlying pattern could easily change in ways the known data gives no indication of. interp1d() raises an error by default specifically to prevent silently returning what could be a wildly unreliable extrapolated guess without explicit awareness that extrapolation, not interpolation, is actually happening.

Related Functions

interpolate-univariatesplineinterpolate-rbfnp-linspace