0
0
SciPydata~10 mins

KD-Tree for nearest neighbors in SciPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the KDTree class from scipy.spatial.

SciPy
from scipy.spatial import [1]

points = [[1, 2], [3, 4], [5, 6]]
tree = KDTree(points)
Drag options to blanks, or click blank then click option'
AKD
Bkdtree
CKD_tree
DKDTree
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase letters like 'kdtree' instead of 'KDTree'.
Misspelling the class name with underscores.
2fill in blank
medium

Complete the code to build a KDTree from a list of points.

SciPy
points = [[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]]
tree = [1](points)
Drag options to blanks, or click blank then click option'
AKDTree
Bkdtree
CTreeKD
DKD_tree
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or misspelled class names.
Trying to call a method instead of the class.
3fill in blank
hard

Fix the error in the code to query the nearest neighbor of a point using the KDTree.

SciPy
query_point = [3, 4]
distance, index = tree.[1](query_point)
print(f"Nearest point index: {index}, distance: {distance}")
Drag options to blanks, or click blank then click option'
Aquery_point
Bquery
Cnearest
Dsearch
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like 'nearest' or 'search'.
Trying to call 'query_point' which is a variable, not a method.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each point to its distance from the query point using the KDTree.

SciPy
distances = {tuple(point): tree.[1](point)[[2]] for point in points}
Drag options to blanks, or click blank then click option'
Aquery
B0
C1
Dnearest
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names.
Accessing the wrong tuple index for distance.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each point's index to the point itself if its distance from the query point is less than 5.

SciPy
close_points = {i: points[i] for i in range(len(points)) if tree.query(points[i])[[1]] [2] [3]
Drag options to blanks, or click blank then click option'
A0
B<
C5
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong tuple index for distance.
Using wrong comparison operator or value.