Challenge - 5 Problems
Interp1d Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of linear interpolation with interp1d
What is the output of the following code snippet using
interp1d for linear interpolation?SciPy
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) result = f(1.5) print(result)
Attempts:
2 left
💡 Hint
Linear interpolation finds the value between two points on a straight line.
✗ Incorrect
The points at x=1 and x=2 have y=1 and y=4 respectively. Linear interpolation at x=1.5 is halfway between 1 and 4, so (1+4)/2 = 2.5.
❓ data_output
intermediate2:00remaining
Number of interpolated points returned
Given the code below, how many values does the interpolation function return?
SciPy
import numpy as np from scipy.interpolate import interp1d x = np.array([0, 2, 4, 6]) y = np.array([0, 4, 16, 36]) f = interp1d(x, y) values = f([1, 3, 5]) print(len(values))
Attempts:
2 left
💡 Hint
The input to the interpolation function is a list of points.
✗ Incorrect
The function is called with three points [1, 3, 5], so it returns three interpolated values.
🔧 Debug
advanced2:00remaining
Error raised by extrapolation with default interp1d
What error does the following code raise when trying to interpolate a value outside the original x range?
SciPy
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) print(f(3))
Attempts:
2 left
💡 Hint
By default, interp1d does not allow extrapolation outside the x range.
✗ Incorrect
The default interp1d raises a ValueError when asked to interpolate outside the original x range unless 'fill_value' or 'bounds_error' is set.
🧠 Conceptual
advanced2:00remaining
Effect of 'kind' parameter in interp1d
Which statement correctly describes the effect of changing the 'kind' parameter in
interp1d?Attempts:
2 left
💡 Hint
Think about how interpolation can be done in different ways.
✗ Incorrect
The 'kind' parameter selects the interpolation method, such as 'linear' for straight lines, 'nearest' for stepwise, or 'cubic' for smooth curves.
🚀 Application
expert3:00remaining
Using interp1d to fill missing data in a time series
You have a time series with missing values at some timestamps. Which code snippet correctly uses
interp1d to fill missing values by interpolation?SciPy
import numpy as np from scipy.interpolate import interp1d time = np.array([0, 1, 2, 4, 5]) values = np.array([10, np.nan, 14, 20, 22]) # Fill missing values code here print(filled_values)
Attempts:
2 left
💡 Hint
You must exclude missing values when creating the interpolation function.
✗ Incorrect
Option D correctly creates an interpolation function using only known values and fills missing points by evaluating at missing timestamps.