Interpolation helps us find values between known data points. It fills gaps in data smoothly and logically.
You have temperature readings every hour but want to know the temperature at half past the hour.
You know sales numbers for some months and want to estimate sales for missing months.
You have GPS locations recorded every minute and want to estimate position at a specific second.
You want to draw a smooth curve through scattered data points for visualization.
Syntax
SciPy
from scipy.interpolate import interp1d
# Create interpolation function
f = interp1d(x, y, kind='linear')
# Estimate value at new point
new_y = f(new_x)
x and y are arrays of known data points.
kind defines the interpolation method, like 'linear' or 'cubic'.
Examples
Linear interpolation estimates the value at 1.5 between points (1,1) and (2,4).
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 estimate at 1.5 using more points.
SciPy
from scipy.interpolate import interp1d
x = [0, 1, 2, 3]
y = [0, 1, 4, 9]
f = interp1d(x, y, kind='cubic')
print(f(1.5))
Sample Program
This program estimates the temperature at 2.5 hours using linear interpolation between known hourly temperatures.
SciPy
from scipy.interpolate import interp1d
import numpy as np
# Known data points: hours and temperature
hours = np.array([0, 1, 2, 3, 4])
temps = np.array([15, 17, 20, 22, 21])
# Create linear interpolation function
interp_func = interp1d(hours, temps, kind='linear')
# Estimate temperature at 2.5 hours
temp_2_5 = interp_func(2.5)
print(f"Estimated temperature at 2.5 hours: {temp_2_5:.1f} degrees")
OutputSuccess
Important Notes
Interpolation only estimates within the range of known data points, not outside.
Choosing the right interpolation method affects smoothness and accuracy.
Summary
Interpolation fills in missing values between known data points.
It helps make smooth predictions or visualizations.
Scipy's interp1d is a simple way to do interpolation in Python.
Practice
(1/5)
1. What is the main purpose of interpolation in data science?
easy
A. To remove outliers from data
B. To sort data in ascending order
C. To estimate values between known data points
D. To calculate the average of all data points
Solution
Step 1: Understand interpolation concept
Interpolation is used to find unknown values that lie between known data points.
Step 2: Compare options with definition
Only To estimate values between known data points matches the purpose of interpolation, which is estimating values between known points.
Final Answer:
To estimate values between known data points -> Option C
Quick Check:
Interpolation = estimate between points [OK]
Hint: Interpolation fills gaps between data points [OK]
Common Mistakes:
Confusing interpolation with data cleaning
Thinking interpolation sorts data
Assuming interpolation calculates averages
2. Which of the following is the correct way to create a linear interpolation function using scipy's interp1d?
easy
A. interp1d(x, y, kind='linear')
B. interp1d(x, y, method='linear')
C. interp1d(x, y, type='linear')
D. interp1d(x, y, mode='linear')
Solution
Step 1: Recall interp1d syntax
The correct parameter to specify interpolation type is 'kind'.
Step 2: Match options with correct parameter
Only interp1d(x, y, kind='linear') uses 'kind' correctly as 'kind="linear"'. Others use incorrect parameter names.
Final Answer:
interp1d(x, y, kind='linear') -> Option A
Quick Check:
Parameter for interpolation type = kind [OK]
Hint: Use 'kind' to set interpolation type in interp1d [OK]
Common Mistakes:
Using 'method' instead of 'kind'
Using 'type' or 'mode' parameters incorrectly
Forgetting to import interp1d from scipy.interpolate
3. Given the code:
import numpy as np
from scipy.interpolate import interp1d
x = np.array([0, 1, 2])
y = np.array([0, 1, 4])
f = interp1d(x, y, kind='linear')
print(f(1.5))
What is the output?
medium
A. 2.5
B. 3.0
C. 2.0
D. Error
Solution
Step 1: Understand linear interpolation between points
At x=1, y=1 and at x=2, y=4. Linear interpolation at 1.5 is halfway between 1 and 4.
Step 2: Calculate interpolated value
Value = 1 + (4-1)*(1.5-1)/(2-1) = 1 + 3*0.5 = 2.5
Final Answer:
2.5 -> Option A
Quick Check:
Linear interp at 1.5 = 2.5 [OK]
Hint: Linear interp averages values proportionally [OK]
Common Mistakes:
Using y value at 1 or 2 directly
Calculating midpoint incorrectly
Expecting quadratic interpolation result
4. What is wrong with this interpolation code snippet?
from scipy.interpolate import interp1d
x = [0, 1, 2]
y = [0, 1]
f = interp1d(x, y, kind='linear')
print(f(1.5))
medium
A. Function call syntax is wrong
B. Missing import for numpy
C. Incorrect interpolation kind
D. x and y arrays have different lengths
Solution
Step 1: Check lengths of x and y
x has 3 elements, y has 2 elements. They must be equal length for interpolation.
Step 2: Identify error cause
Different lengths cause interp1d to raise an error because points don't match.
Final Answer:
x and y arrays have different lengths -> Option D
Quick Check:
Equal length arrays needed [OK]
Hint: x and y must be same length for interp1d [OK]
Common Mistakes:
Ignoring array length mismatch
Assuming numpy import is mandatory here
Thinking 'kind' parameter causes error
5. You have temperature data recorded every hour: x = [0, 1, 3, 4] hours and y = [15, 16, 20, 21] degrees Celsius. You want to estimate the temperature at 2 hours using interpolation. Which scipy interp1d call and evaluation will give the correct estimate?
hard
A. f = interp1d(x, y, kind='nearest'); f(2)
B. f = interp1d(x, y, kind='linear'); f(2)
C. f = interp1d(x, y, kind='cubic'); f(2)
D. f = interp1d(x, y, kind='quadratic'); f(2)
Solution
Step 1: Understand data points and interpolation types
Data points at 1 and 3 hours have temperatures 16 and 20. Linear interpolation estimates between these points smoothly.
Step 2: Evaluate options for correct interpolation
f = interp1d(x, y, kind='linear'); f(2) uses linear interpolation which is suitable here. 'nearest' picks closest known value, 'cubic' needs more points, 'quadratic' is invalid in interp1d.
Final Answer:
f = interp1d(x, y, kind='linear'); f(2) -> Option B
Quick Check:
Linear interp best for simple estimate [OK]
Hint: Use linear kind for smooth estimate between points [OK]
Common Mistakes:
Using 'quadratic' which is not valid in interp1d
Choosing 'nearest' which does not interpolate
Using 'cubic' with too few points causing error
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.