Challenge - 5 Problems
Spatial Geometry Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of KDTree query for nearest neighbor
What is the output of the following code that uses scipy.spatial.KDTree to find the nearest neighbor of a point?
SciPy
from scipy.spatial import KDTree points = [(1, 2), (3, 4), (5, 6)] tree = KDTree(points) distance, index = tree.query((2, 3)) print((distance, index))
Attempts:
2 left
💡 Hint
Think about which point is closest to (2, 3) using Euclidean distance.
✗ Incorrect
The KDTree query returns the distance and index of the closest point. The point (1, 2) is closest to (2, 3) with distance sqrt((2-1)^2 + (3-2)^2) = sqrt(1 + 1) = 1.414...
❓ data_output
intermediate2:00remaining
Number of points within radius using BallTree
Using scipy.spatial.cKDTree, how many points lie within radius 2.5 from point (3, 3)?
SciPy
from scipy.spatial import cKDTree points = [(1, 2), (3, 4), (5, 6), (2, 2)] tree = cKDTree(points) indices = tree.query_ball_point((3, 3), r=2.5) print(len(indices))
Attempts:
2 left
💡 Hint
Check which points are within 2.5 units from (3, 3).
✗ Incorrect
Points (1,2), (3,4), and (2,2) are within radius 2.5 from (3,3). So count is 3.
❓ visualization
advanced3:00remaining
Visualizing Voronoi diagram from points
Which option shows the correct code to plot a Voronoi diagram for points [(1,1), (2,3), (3,1), (4,4)] using scipy.spatial?
SciPy
import matplotlib.pyplot as plt from scipy.spatial import Voronoi, voronoi_plot_2d points = [(1,1), (2,3), (3,1), (4,4)] vor = Voronoi(points) fig = voronoi_plot_2d(vor) plt.show()
Attempts:
2 left
💡 Hint
Voronoi diagrams partition space based on closest points.
✗ Incorrect
Option A uses Voronoi and voronoi_plot_2d to plot the Voronoi diagram correctly. Others plot different geometric structures.
🧠 Conceptual
advanced2:00remaining
Why spatial algorithms are efficient for geometry problems
Why do spatial algorithms like KDTree and BallTree solve geometry problems efficiently?
Attempts:
2 left
💡 Hint
Think about how searching for neighbors can be faster than checking all points.
✗ Incorrect
Spatial algorithms build tree structures that allow quick pruning of search space, avoiding checking every point.
🔧 Debug
expert2:00remaining
Identify error in KDTree query code
What error does the following code raise and why?
from scipy.spatial import KDTree
points = [(1, 2), (3, 4)]
tree = KDTree(points)
result = tree.query((1, 2, 3))
SciPy
from scipy.spatial import KDTree points = [(1, 2), (3, 4)] tree = KDTree(points) result = tree.query((1, 2, 3))
Attempts:
2 left
💡 Hint
Check the dimension of the query point compared to the points used to build the tree.
✗ Incorrect
KDTree expects query points to have the same dimension as the data points. Here data points are 2D but query point is 3D, causing ValueError.