Interpolation (np.interp)
Interpolation estimates the value of a curve between the data points you actually measured, and np.interp does it with fast straight-line (linear) interpolation in a single call.
Learn Interpolation (np.interp) in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You'll read values between known samples, resample a whole curve onto a finer grid, control what happens past the edges with left and right , and learn why xp must be sorted.
np.interp(x, xp, fp) takes three things: the query x where you want a value, the known x-coordinates xp , and the matching y-values fp . It finds the two surrounding known points and returns the value on the straight line between them. The query can be a single number or an array.
A common use is resampling : you have a curve measured at a few points and you want it on a denser, evenly spaced grid (to plot smoothly or to line two datasets up). Build the new grid with np.linspace , then interpolate the original onto it.
Outside the known range, np.interp does not extrapolate — it clamps to the first and last fp values. Override that with left (for x below xp[0] ) and right (for x above xp[-1] ). And remember: xp must be sorted ascending, or the results are silently wrong.
If your data is not already sorted by x, sort it first — e.g. order = np.argsort(xp) then index both arrays with order — before calling np.interp .
Replace each ___ so the program reads two interpolated values off a simple line.
Expected output: 50.0 then 150.0 . (Answers: interp , 15 .)
np.interp assumes xp increases and never sorts it for you.
✅ Fix: sort first — order = np.argsort(xp) , then np.interp(x, xp[order], fp[order]) .
Outside the range it clamps to the end values instead of continuing the trend.
✅ Fix: use left / right for custom edges, or a dedicated tool like SciPy for true extrapolation.
Temperatures were logged every 6 hours. Estimate the temperature at 3, 9, 15, and 21 hours by interpolating between the logged values.
Lesson complete — interpolation unlocked!
You can now estimate values between known points with np.interp , resample a curve onto a finer grid, control the edges with left and right , and keep xp sorted for correct results.
🚀 Up next: Histograms & Binning — count values into bins with np.histogram and digitize.
Practice quiz
What kind of interpolation does np.interp perform?
- One-dimensional linear (straight-line) interpolation
- Cubic spline interpolation
- Nearest-neighbour only
- Polynomial fitting
Answer: One-dimensional linear (straight-line) interpolation. np.interp draws straight lines between known points for 1D linear interpolation.
What are the three main arguments of np.interp(x, xp, fp)?
- start, stop, step
- rows, cols, depth
- query x, known x-coords xp, known y-values fp
- mean, std, count
Answer: query x, known x-coords xp, known y-values fp. x is where you want a value, xp are the known x's, fp the matching y's.
For xp=[0,1,2,3], fp=[0,10,20,30], what is np.interp(1.5, xp, fp)?
- 10.0
- 20.0
- 15.0
- 1.5
Answer: 15.0. 1.5 is halfway between (1,10) and (2,20), giving 15.0.
By default, what does np.interp do for x above xp[-1]?
- Extrapolates the trend
- Raises an error
- Returns NaN
- Clamps to the last fp value
Answer: Clamps to the last fp value. It clamps: any x above the range returns fp[-1] rather than extrapolating.
What requirement must xp satisfy for correct results?
- It must be sorted ascending (monotonic)
- It must be all integers
- It must be the same as fp
- It must contain zero
Answer: It must be sorted ascending (monotonic). np.interp assumes xp increases; unsorted xp gives silently wrong answers.
Which keyword sets the value returned for x below xp[0]?
- low
- left
- below
- start
Answer: left. left= overrides the clamped value for x below the first known x.
For xp=[1,2,3], fp=[10,20,30], what is np.interp(0, xp, fp, left=-1)?
- 10.0
- 0.0
- -1.0
- 20.0
Answer: -1.0. x=0 is below the range, so left=-1 is returned instead of clamping to 10.
If your data is not sorted by x, what should you do first?
- Reverse fp
- Use np.interp twice
- Nothing, interp sorts it
- Sort both arrays, e.g. with np.argsort(xp)
Answer: Sort both arrays, e.g. with np.argsort(xp). Sort xp and fp together (an argsort on xp) before calling np.interp.
Can np.interp accept an array of query points at once?
- Yes, x can be a scalar or an array
- No, only one value at a time
- Only if xp is a scalar
- Only for integer queries
Answer: Yes, x can be a scalar or an array. The query x may be a single number or a whole array of points.
For xp=[0,10,20], fp=[0,100,200], what is np.interp(5, xp, fp)?
- 50.0
- 5.0
- 100.0
- 10.0
Answer: 50.0. 5 is halfway between (0,0) and (10,100), giving 50.0.