0
0
SciPydata~20 mins

KD-Tree for nearest neighbors in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
KD-Tree Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Find nearest neighbor using KD-Tree
Given the following code that builds a KD-Tree and queries the nearest neighbor for a point, what is the output printed?
SciPy
from scipy.spatial import KDTree
import numpy as np
points = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
tree = KDTree(points)
distance, index = tree.query([4, 5])
print(index, distance)
A3 4.242640687119285
B2 1.4142135623730951
C0 4.242640687119285
D1 1.4142135623730951
Attempts:
2 left
💡 Hint
Remember that KDTree.query returns the distance first and then the index of the closest point.
data_output
intermediate
2:00remaining
Number of neighbors within a radius
Using KD-Tree, how many points lie within a radius of 3 units from the point [5, 5]?
SciPy
from scipy.spatial import KDTree
import numpy as np
points = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [6, 5]])
tree = KDTree(points)
indices = tree.query_ball_point([5, 5], r=3)
print(len(indices))
A4
B3
C2
D5
Attempts:
2 left
💡 Hint
Check which points are within distance 3 from [5,5].
visualization
advanced
3:00remaining
Visualizing KD-Tree nearest neighbors
Which option correctly describes the plot generated by the code below?
SciPy
import matplotlib.pyplot as plt
from scipy.spatial import KDTree
import numpy as np
points = np.random.rand(10, 2) * 10
tree = KDTree(points)
query_point = np.array([5, 5])
dist, idx = tree.query(query_point, k=3)
plt.scatter(points[:,0], points[:,1], label='Points')
plt.scatter(query_point[0], query_point[1], color='red', label='Query Point')
plt.scatter(points[idx,0], points[idx,1], facecolors='none', edgecolors='green', s=150, label='Nearest Neighbors')
plt.legend()
plt.show()
AA scatter plot showing 10 points, a red query point at (5,5), and 3 green circles around the nearest neighbors.
BA line plot connecting the query point to all points with green lines.
CA bar chart showing distances of all points from the query point.
DA scatter plot with only the query point and no other points.
Attempts:
2 left
💡 Hint
Look at the scatter and color parameters used in the plot.
🔧 Debug
advanced
2:00remaining
Identify the error in KD-Tree query usage
What error will this code raise when executed?
SciPy
from scipy.spatial import KDTree
import numpy as np
points = np.array([[1, 2], [3, 4], [5, 6]])
tree = KDTree(points)
result = tree.query([1, 2, 3])
print(result)
AValueError: query point dimension must match data dimension
BTypeError: 'KDTree' object is not callable
CIndexError: index out of range
DNo error, prints nearest neighbor index and distance
Attempts:
2 left
💡 Hint
Check the dimension of the query point compared to the points used to build the tree.
🚀 Application
expert
3:00remaining
Using KD-Tree for batch nearest neighbor search
Given a KD-Tree built from 1000 random 3D points, which option correctly returns the indices of the 5 nearest neighbors for each of 10 query points?
SciPy
from scipy.spatial import KDTree
import numpy as np
points = np.random.rand(1000, 3)
tree = KDTree(points)
queries = np.random.rand(10, 3)
result = tree.query(queries, k=5)
print(result[1].shape)
A(10,)
B(5, 10)
C(10, 5)
D(5,)
Attempts:
2 left
💡 Hint
Check the shape of the indices array returned by KDTree.query when querying multiple points with k neighbors.