We use interp1d to find values between known data points. It helps us guess missing or in-between values smoothly.
You have temperature readings every hour but want to know the temperature at half past the hour.
You know sales numbers for some days and want to estimate sales on days without data.
You have a few points of a curve and want to draw a smooth line connecting them.
You want to convert data sampled at one rate to another rate by estimating values in between.
Syntax
SciPy
from scipy.interpolate import interp1d
f = interp1d(x, y, kind='linear', fill_value='extrapolate')
y_new = f(x_new)
x and y are your known data points (arrays).
kind controls the interpolation type: 'linear' (default), 'nearest', 'cubic', etc.
Examples
Linear interpolation between points (1,1) and (2,4) gives 2.5 at 1.5.
SciPy
from scipy.interpolate import interp1d
x = [0, 1, 2]
y = [0, 1, 4]
f = interp1d(x, y)
print(f(1.5))
Cubic interpolation gives a smoother curve and a different value at 1.5.
SciPy
f_cubic = interp1d(x, y, kind='cubic')
print(f_cubic(1.5))
Nearest interpolation picks the closest known y value, here 1 at 1.5.
SciPy
f_nearest = interp1d(x, y, kind='nearest')
print(f_nearest(1.5))
Sample Program
This program creates a linear interpolation function from known points and finds values at new points between them.
SciPy
from scipy.interpolate import interp1d
import numpy as np
# Known data points
x = np.array([0, 1, 2, 3, 4])
y = np.array([0, 2, 4, 6, 8])
# Create interpolation function
f_linear = interp1d(x, y, kind='linear')
# New points to estimate
x_new = np.array([0.5, 1.5, 2.5, 3.5])
# Calculate interpolated values
y_new = f_linear(x_new)
print(y_new)
OutputSuccess
Important Notes
Input x must be increasing and 1-dimensional.
By default, fill_value='extrapolate' estimates values outside the range of x. Use bounds_error=True to raise an error instead.
Different kind values change smoothness and accuracy.
Summary
interp1d helps estimate values between known points.
Choose interpolation kind based on smoothness needed.
Use the created function to find new values easily.
Practice
(1/5)
1. What is the main purpose of interp1d in scipy?
easy
A. To sort data points in ascending order
B. To estimate values between known data points
C. To calculate the derivative of a function
D. To perform matrix multiplication
Solution
Step 1: Understand the function role
interp1d is used to create a function that estimates values between given points.
Step 2: Compare options with purpose
Only To estimate values between known data points describes estimating values between known points, which matches interp1d's purpose.
Final Answer:
To estimate values between known data points -> Option B
Quick Check:
interp1d = estimate between points [OK]
Hint: interp1d fills gaps between points [OK]
Common Mistakes:
Confusing interpolation with sorting
Thinking it calculates derivatives
Mixing it up with matrix operations
2. Which of the following is the correct way to create a linear interpolation function using interp1d with x and y arrays?
easy
A. f = interp1d(x, y, method='linear')
B. f = interp1d(y, x, kind='linear')
C. f = interp1d(x, y, kind='linear')
D. f = interp1d(x, y, kind='smooth')
Solution
Step 1: Check parameter order and names
interp1d takes x and y arrays in order, and the interpolation type is set by kind.
Step 2: Validate options
f = interp1d(x, y, kind='linear') uses correct order and kind='linear'. f = interp1d(y, x, kind='linear') swaps x and y, which is wrong. f = interp1d(x, y, method='linear') uses wrong parameter name method. f = interp1d(x, y, kind='smooth') uses invalid kind 'smooth'.
Final Answer:
f = interp1d(x, y, kind='linear') -> Option C
Quick Check:
Correct syntax uses kind='linear' [OK]
Hint: Use kind='linear' and x,y in order [OK]
Common Mistakes:
Swapping x and y arrays
Using wrong parameter name 'method'
Using unsupported kind values
3. Given the code:
import numpy as np
from scipy.interpolate import interp1d
x = np.array([0, 1, 2, 3])
y = np.array([0, 1, 4, 9])
f = interp1d(x, y, kind='linear')
print(f(1.5))
What is the output?
medium
A. 2.5
B. 3.25
C. 4.5
D. Error
Solution
Step 1: Understand interpolation at 1.5
Between x=1 (y=1) and x=2 (y=4), linear interpolation at 1.5 is halfway: (1 + 4)/2 = 2.5.
from scipy.interpolate import interp1d
x = [0, 1, 2]
y = [1, 4]
f = interp1d(x, y)
print(f(1.5))
medium
A. x and y arrays have different lengths
B. interp1d requires x to be sorted descending
C. Missing kind parameter causes error
D. Cannot interpolate at 1.5 because it's outside x range
Solution
Step 1: Check lengths of x and y
x has 3 elements, y has 2 elements. They must be the same length for interpolation.
Step 2: Understand interp1d requirements
Different lengths cause a ValueError. Sorting or kind parameter is not mandatory. 1.5 is within x range (0 to 2).
Final Answer:
x and y arrays have different lengths -> Option A
Quick Check:
Equal length arrays needed [OK]
Hint: Check x and y lengths match [OK]
Common Mistakes:
Assuming kind is mandatory
Thinking x must be descending
Confusing interpolation range limits
5. You have temperature data recorded every hour: x = [0, 1, 2, 3, 4] hours and y = [15, 17, 20, 22, 21] degrees Celsius. You want to estimate temperature at half-hour intervals using cubic interpolation. Which code correctly creates the interpolation function and estimates temperature at 2.5 hours?
hard
A. f = interp1d(x, y, kind='linear')\nprint(f(2.5))
B. f = interp1d(x, y, kind='quadratic')\nprint(f(2.5))
C. f = interp1d(y, x, kind='cubic')\nprint(f(2.5))
D. f = interp1d(x, y, kind='cubic')\nprint(f(2.5))
Solution
Step 1: Identify correct parameters for cubic interpolation
Use kind='cubic' with x and y arrays in correct order.
Step 2: Check options
f = interp1d(x, y, kind='cubic')\nprint(f(2.5)) uses correct order and cubic kind. f = interp1d(x, y, kind='linear')\nprint(f(2.5)) uses linear, not cubic. f = interp1d(y, x, kind='cubic')\nprint(f(2.5)) swaps x and y, which is wrong. f = interp1d(x, y, kind='quadratic')\nprint(f(2.5)) uses quadratic kind instead of cubic.
Final Answer:
f = interp1d(x, y, kind='cubic')
print(f(2.5)) -> Option D
Quick Check:
Cubic kind with correct x,y order [OK]
Hint: Use kind='cubic' and correct x,y order [OK]
Common Mistakes:
Swapping x and y arrays
Using quadratic instead of cubic
Choosing linear instead of cubic for smoothness
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.