TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

KNN Distance + Neighbor Lookup

Classic ML
Medium

For every test point, find the indices of its kkk nearest training points using Euclidean distance:

d(x,z)=∑j=1D(xj−zj)2d(\mathbf{x},\mathbf{z})=\sqrt{\sum_{j=1}^{D}(x_j-z_j)^2}d(x,z)=j=1∑D​(xj​−zj​)2​

Here, DDD is the feature count and jjj indexes a feature. Sort neighbors from smallest to largest distance. If kkk exceeds the training-set size, append −1-1−1 until every row has length kkk. One-dimensional inputs represent collections of scalar samples. Return an integer NumPy array of shape (n_test, k).

Loading visualization...

Examples

Input: X_train = [1, 3, 5], X_test = [2], k = 2

Output: [[0, 1]]

Explanation: Training values 1 and 3 are equally close to 2, and their indices remain in ascending order.

Input: X_train = [[0, 0], [1, 1], [2, 2]], X_test = [[0.5, 0.5]], k = 2

Output: [[0, 1]]

Hint 1

Reshape scalar datasets to (-1, 1) before broadcasting.

Hint 2

Use X_test[:, None, :] - X_train[None, :, :] to form all pairwise differences.

Hint 3

Concatenate an integer array filled with -1 when padding is required.

Requirements

  • Compute all test-to-training Euclidean distances
  • Sort neighbor indices from closest to farthest
  • Pad missing neighbors with -1 when k exceeds the training size
  • Return an integer NumPy array of shape (n_test, k)

Constraints

  • Training and test samples have the same feature width
  • k is a positive integer
  • Use NumPy only
Try Similar Problems
Euclidean DistanceManhattan DistanceCosine SimilarityK Means AssignmentK Means Centroid Update

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

You must run your code first.
PrevNext

KNN Distance + Neighbor Lookup

Classic ML
Medium

For every test point, find the indices of its kkk nearest training points using Euclidean distance:

d(x,z)=∑j=1D(xj−zj)2d(\mathbf{x},\mathbf{z})=\sqrt{\sum_{j=1}^{D}(x_j-z_j)^2}d(x,z)=j=1∑D​(xj​−zj​)2​

Here, DDD is the feature count and jjj indexes a feature. Sort neighbors from smallest to largest distance. If kkk exceeds the training-set size, append −1-1−1 until every row has length kkk. One-dimensional inputs represent collections of scalar samples. Return an integer NumPy array of shape (n_test, k).

Loading visualization...

Examples

Input: X_train = [1, 3, 5], X_test = [2], k = 2

Output: [[0, 1]]

Explanation: Training values 1 and 3 are equally close to 2, and their indices remain in ascending order.

Input: X_train = [[0, 0], [1, 1], [2, 2]], X_test = [[0.5, 0.5]], k = 2

Output: [[0, 1]]

Hint 1

Reshape scalar datasets to (-1, 1) before broadcasting.

Hint 2

Use X_test[:, None, :] - X_train[None, :, :] to form all pairwise differences.

Hint 3

Concatenate an integer array filled with -1 when padding is required.

Requirements

  • Compute all test-to-training Euclidean distances
  • Sort neighbor indices from closest to farthest
  • Pad missing neighbors with -1 when k exceeds the training size
  • Return an integer NumPy array of shape (n_test, k)

Constraints

  • Training and test samples have the same feature width
  • k is a positive integer
  • Use NumPy only
Try Similar Problems
Euclidean DistanceManhattan DistanceCosine SimilarityK Means AssignmentK Means Centroid Update

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

You must run your code first.