0
0
SciPydata~20 mins

Linear vs cubic interpolation in SciPy - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interpolation 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 scipy
What is the output of this code snippet that uses linear interpolation to estimate values?
SciPy
import numpy as np
from scipy.interpolate import interp1d

x = np.array([0, 1, 2, 3])
y = np.array([0, 1, 4, 9])
linear_interp = interp1d(x, y, kind='linear')
result = linear_interp(1.5)
print(result)
A2.5
B2.75
CError: interp1d requires integer input
D3.0
Attempts:
2 left
💡 Hint
Linear interpolation finds the value on the straight line between two known points.
Predict Output
intermediate
2:00remaining
Output of cubic interpolation with scipy
What value does this cubic interpolation code produce at x=1.5?
SciPy
import numpy as np
from scipy.interpolate import interp1d

x = np.array([0, 1, 2, 3])
y = np.array([0, 1, 4, 9])
cubic_interp = interp1d(x, y, kind='cubic')
result = cubic_interp(1.5)
print(round(result, 2))
A2.5
B4.5
C3.38
DError: cubic interpolation needs more points
Attempts:
2 left
💡 Hint
Cubic interpolation fits a smooth curve through the points, not just straight lines.
data_output
advanced
2:00remaining
Number of points generated by linear vs cubic interpolation
Given 5 data points, how many points will the following code generate when interpolating 10 new points between the minimum and maximum x values?
SciPy
import numpy as np
from scipy.interpolate import interp1d

x = np.linspace(0, 4, 5)
y = x**2
linear_interp = interp1d(x, y, kind='linear')
cubic_interp = interp1d(x, y, kind='cubic')
new_x = np.linspace(0, 4, 10)
linear_y = linear_interp(new_x)
cubic_y = cubic_interp(new_x)
print(len(linear_y), len(cubic_y))
A10 10
B5 5
C10 5
D5 10
Attempts:
2 left
💡 Hint
The interpolation returns values for each input in new_x.
visualization
advanced
3:00remaining
Visual difference between linear and cubic interpolation
Which plot correctly shows the difference between linear and cubic interpolation for the points (0,0), (1,1), (2,4), (3,9)?
SciPy
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d

x = np.array([0, 1, 2, 3])
y = np.array([0, 1, 4, 9])
new_x = np.linspace(0, 3, 100)
linear_interp = interp1d(x, y, kind='linear')
cubic_interp = interp1d(x, y, kind='cubic')
plt.plot(x, y, 'o', label='Data points')
plt.plot(new_x, linear_interp(new_x), label='Linear')
plt.plot(new_x, cubic_interp(new_x), label='Cubic')
plt.legend()
plt.show()
ALinear interpolation produces a curve with sharp peaks; cubic interpolation is flat.
BLinear interpolation is a smooth curve; cubic is piecewise straight lines.
CBoth linear and cubic interpolation produce the same straight line.
DLinear interpolation is piecewise straight lines; cubic is a smooth curve passing through points.
Attempts:
2 left
💡 Hint
Think about how linear interpolation connects points versus cubic interpolation.
🧠 Conceptual
expert
2:30remaining
Why cubic interpolation can fail with few points
Why might cubic interpolation raise an error or produce unreliable results when given only two data points?
ACubic interpolation only works with evenly spaced points.
BCubic interpolation requires at least four points to fit a cubic polynomial.
CTwo points are enough for cubic interpolation but it always returns linear results.
DCubic interpolation cannot handle numeric data.
Attempts:
2 left
💡 Hint
Think about the minimum number of points needed to define a cubic curve.