Challenge - 5 Problems
2D Interpolation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of 2D interpolation using interp2d
What is the output of the following code snippet that uses
interp2d for interpolation?SciPy
import numpy as np from scipy.interpolate import interp2d x = np.array([0, 1, 2]) y = np.array([0, 1, 2]) z = np.array([[0, 1, 4], [1, 2, 5], [4, 5, 8]]) f = interp2d(x, y, z, kind='linear') result = f(1.5, 1.5) print(result[0])
Attempts:
2 left
💡 Hint
Recall that interp2d returns an array even for single points; check the interpolation formula for linear kind.
✗ Incorrect
The interp2d function performs bilinear interpolation. At point (1.5, 1.5), the interpolated value is 5.0. The function returns an array, so we print the first element.
❓ data_output
intermediate1:30remaining
Number of points interpolated with griddata
Given scattered points and values, how many points will be interpolated by the following code using
griddata?SciPy
import numpy as np from scipy.interpolate import griddata points = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) values = np.array([0, 1, 1, 0]) xi = np.array([[0.5, 0.5], [0.2, 0.8], [1.5, 1.5]]) zi = griddata(points, values, xi, method='linear') print(len(zi))
Attempts:
2 left
💡 Hint
Count how many points are in xi and how griddata handles points outside convex hull.
✗ Incorrect
The input xi has 3 points, so griddata returns 3 interpolated values. Points outside the convex hull get NaN but are still counted.
🔧 Debug
advanced2:00remaining
Identify the error in griddata usage
What error does the following code raise when using
griddata?SciPy
import numpy as np from scipy.interpolate import griddata points = np.array([[0, 0], [1, 1]]) values = np.array([1, 2]) xi = np.array([0.5, 0.5]) zi = griddata(points, values, xi, method='nearest') print(zi)
Attempts:
2 left
💡 Hint
Check the shape of xi; griddata expects a 2D array with each row as a point.
✗ Incorrect
The xi array is 1D with shape (2,), but griddata expects a 2D array with shape (n, 2). This causes a TypeError.
❓ visualization
advanced2:30remaining
Visualizing interpolation results with griddata
Which option correctly describes the plot generated by this code using
griddata with cubic interpolation?SciPy
import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import griddata points = np.random.rand(10, 2) values = np.sin(points[:,0]*10) + np.cos(points[:,1]*10) grid_x, grid_y = np.mgrid[0:1:100j, 0:1:100j] grid_z = griddata(points, values, (grid_x, grid_y), method='cubic') plt.imshow(grid_z.T, extent=(0,1,0,1), origin='lower') plt.scatter(points[:,0], points[:,1], c='red') plt.title('Cubic interpolation of scattered data') plt.colorbar() plt.show()
Attempts:
2 left
💡 Hint
Cubic interpolation creates smooth surfaces; red dots show original data points.
✗ Incorrect
The plot shows a smooth color gradient from cubic interpolation with red dots marking the scattered points. This visualizes how interpolation fills the gaps.
🧠 Conceptual
expert3:00remaining
Choosing interpolation method for irregular data
You have irregularly spaced 2D data points with noisy measurements. Which interpolation method is best to estimate values on a regular grid while minimizing oscillations?
Attempts:
2 left
💡 Hint
Consider how each method handles noise and irregular spacing.
✗ Incorrect
For irregular noisy data, 'linear' method in griddata balances smoothness and noise without introducing oscillations that cubic might cause. Nearest can be too rough.