Wilson's Edited Nearest Neighbour Noise Removal

Quick Overview

Implement Wilson's edited nearest neighbour rule in NumPy, dropping every sample whose label disagrees with the majority of its k nearest neighbours in the original data. It tests leave-one-out neighbour search, self-exclusion with duplicate points, deterministic tie-breaking and input validation.

Wilson's Edited Nearest Neighbour Noise Removal

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `edited_nearest_neighbours(X, y, k=3)`. For each sample, find its `k` nearest neighbours (Euclidean) in the original full set, excluding itself by index, and keep the sample only if its label equals the neighbours' majority label. Return the sorted indices of kept samples as an integer NumPy array. The function must follow these rules: - `X` is 2-D with shape `(n, d)` and `len(y) == n`, otherwise raise `ValueError`. - `k` must be an integer with `1 <= k <= n - 1`, otherwise raise `ValueError`. - Duplicate points (other indices) count as neighbours at distance 0. - Distance ties break to the lower index. Vote ties break to the smallest label. - Single pass: every decision uses the unedited data. Labels may be ints or strings. ```python X = [[0], [1], [2], [3], [10], [11], [12], [13], [1.5]] y = [0, 0, 0, 0, 1, 1, 1, 1, 1] edited_nearest_neighbours(X, y, k=3) # array([0, 1, 2, 3, 4, 5, 6, 7]); index 8 (a label 1 among the 0s) is removed ``` ```hint Self versus duplicates Excluding a sample by its index is a different rule from excluding every point at distance 0. Find a change to the distance matrix that removes only the sample itself, and pick a sort that keeps equal distances in index order. ``` ### Constraints & Assumptions - `X` has shape `(n, d)` with finite values, and `y` holds `n` labels. - The `(n, n)` pairwise distance matrix fits in memory. Scaling past that is a follow-up. ### What a Strong Answer Covers - Neighbours drawn from the unedited set, with each sample excluded by index and duplicates kept - Distance ties going to the lower index and vote ties to the smallest label, both enforced in the code - The input validation for `X`, `y` and `k` - Sorted integer indices as output, with string labels supported - The time and memory cost of the pairwise approach ### Follow-up Questions - How does Repeated ENN or All-kNN differ from this single pass? - The pairwise distance matrix is $O(n^2)$ memory. How would you scale this? - What risks does ENN pose on imbalanced datasets?

Overview: Implement Wilson's edited nearest neighbour rule in NumPy, dropping every sample whose label disagrees with the majority of its k nearest neighbours in the original data. It tests leave-one-out neighbour search, self-exclusion with duplicate points, deterministic tie-breaking and input validation.

General logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement edited_nearest_neighbours(X, y, k=3). For each sample, find its k nearest neighbours (Euclidean) in the original full set, excluding itself by index, and keep the sample only if its label equals the neighbours' majority label. Return the sorted indices of kept samples as an integer NumPy array.

The function must follow these rules:

  • X is 2-D with shape (n, d) and len(y) == n , otherwise raise ValueError .
  • k must be an integer with 1 <= k <= n - 1 , otherwise raise ValueError .
  • Duplicate points (other indices) count as neighbours at distance 0.
  • Distance ties break to the lower index. Vote ties break to the smallest label.
  • Single pass: every decision uses the unedited data. Labels may be ints or strings.
X = [[0], [1], [2], [3], [10], [11], [12], [13], [1.5]]
y = [0, 0, 0, 0, 1, 1, 1, 1, 1]
edited_nearest_neighbours(X, y, k=3)
# array([0, 1, 2, 3, 4, 5, 6, 7]); index 8 (a label 1 among the 0s) is removed

Constraints & Assumptions

  • X has shape (n, d) with finite values, and y holds n labels.
  • The (n, n) pairwise distance matrix fits in memory. Scaling past that is a follow-up.

What a Strong Answer Covers Guidance

  • Neighbours drawn from the unedited set, with each sample excluded by index and duplicates kept
  • Distance ties going to the lower index and vote ties to the smallest label, both enforced in the code
  • The input validation for X , y and k
  • Sorted integer indices as output, with string labels supported
  • The time and memory cost of the pairwise approach

Follow-up Questions Guidance

  • How does Repeated ENN or All-kNN differ from this single pass?
  • The pairwise distance matrix is O(n2)O(n^2) memory. How would you scale this?
  • What risks does ENN pose on imbalanced datasets?