0
0
SciPydata~20 mins

interp1d for 1D interpolation in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interp1d Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A2.5
B2.0
C3.0
DError: ValueError
Attempts:
2 left
💡 Hint
Linear interpolation finds the value between two points on a straight line.
data_output
intermediate
2: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))
A4
B3
C1
DError: TypeError
Attempts:
2 left
💡 Hint
The input to the interpolation function is a list of points.
🔧 Debug
advanced
2: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))
AValueError: A value (3.0) in x_new is above the interpolation range's maximum value (2)
BIndexError: index out of bounds
CTypeError: unsupported operand type(s)
DNo error, outputs 9
Attempts:
2 left
💡 Hint
By default, interp1d does not allow extrapolation outside the x range.
🧠 Conceptual
advanced
2:00remaining
Effect of 'kind' parameter in interp1d
Which statement correctly describes the effect of changing the 'kind' parameter in interp1d?
A'kind' controls the extrapolation behavior beyond the data range.
B'kind' sets the data type of the output array, e.g., int or float.
C'kind' changes the interpolation method, e.g., linear, nearest, cubic, affecting smoothness and shape of the curve.
D'kind' specifies the axis along which interpolation is performed.
Attempts:
2 left
💡 Hint
Think about how interpolation can be done in different ways.
🚀 Application
expert
3: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)
Afilled_values = values.fillna(method='interpolate')
B
f = interp1d(time, values)
filled_values = f(time)
Cfilled_values = np.interp(time, time, values)
D
mask = ~np.isnan(values)
f = interp1d(time[mask], values[mask])
filled_values = values.copy()
filled_values[np.isnan(values)] = f(time[np.isnan(values)])
Attempts:
2 left
💡 Hint
You must exclude missing values when creating the interpolation function.