0
0
SciPydata~20 mins

2D interpolation (interp2d, griddata) in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
2D Interpolation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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])
A6.0
B5.0
C5.5
DError: interp2d deprecated
Attempts:
2 left
💡 Hint
Recall that interp2d returns an array even for single points; check the interpolation formula for linear kind.
data_output
intermediate
1: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))
A1
B2
C3
DError: xi shape mismatch
Attempts:
2 left
💡 Hint
Count how many points are in xi and how griddata handles points outside convex hull.
🔧 Debug
advanced
2: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)
ATypeError: xi must be 2D array with shape (n, 2)
BValueError: points and values length mismatch
CIndexError: index out of range
DNo error, output: [1. 1.]
Attempts:
2 left
💡 Hint
Check the shape of xi; griddata expects a 2D array with each row as a point.
visualization
advanced
2: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()
AA grid of flat colors with sharp edges and no smooth transitions
BA scatter plot of points only, no color gradient
CAn error due to incompatible shapes in griddata
DA smooth color gradient surface with red dots marking original points
Attempts:
2 left
💡 Hint
Cubic interpolation creates smooth surfaces; red dots show original data points.
🧠 Conceptual
expert
3: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?
AUse <code>griddata</code> with 'linear' method to balance smoothness and noise
BUse <code>interp2d</code> with 'cubic' method for smooth results
CUse <code>griddata</code> with 'nearest' method for smoothness
DUse <code>interp2d</code> with 'nearest' method to avoid oscillations
Attempts:
2 left
💡 Hint
Consider how each method handles noise and irregular spacing.