Dynamic Classifier Selection by Local Accuracy

Quick Overview

Implement dynamic classifier selection by local accuracy (DCS-LA) in NumPy: score each ensemble member on a test row's nearest validation neighbors, then route the row to its most competent member. Tests vectorized nearest-neighbor search, fancy indexing and deterministic tie-breaking.

Dynamic Classifier Selection by Local Accuracy

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `local_competence(X_val, y_val, val_preds, X_test, k)`, which returns an `(n_test, n_members)` matrix: the fraction of each test row's `k` nearest validation rows (Euclidean) that each member classified correctly. Then implement `dcs_la_predict(X_val, y_val, val_preds, X_test, test_preds, k)`, which routes each test row to its most locally competent member and returns `(predictions, selected_member_indices)`. Example: ```text X_val = [[0],[1],[2],[10],[11],[12]], y_val = [0,0,0,1,1,1] val_preds = [[0,0,0,0,0,0], [1,1,1,1,1,1], [1,0,1,1,0,1]] X_test = [[0.5],[11.5]], test_preds = [[0,5],[7,1],[3,3]], k = 3 local_competence -> [[1.0, 0.0, 1/3], [0.0, 1.0, 2/3]] dcs_la_predict -> (preds=[0, 1], selected=[0, 1]) ``` For the test row at 0.5, the three nearest validation rows are at 0, 1 and 2. Member 0 labeled all three correctly, member 1 none of them, and member 2 only the row at 1. ```hint Precompute correctness Whether a member got a validation row right does not depend on the test row. Compute that once, then think about how fancy indexing with the neighbor array gathers it for every test row at the same time. ``` ```hint Tie-breaking Both tie rules can come for free from NumPy if you pick the right sort and the right reduction. Check which sorting algorithms keep equal keys in index order. ``` ### Constraints & Assumptions - `val_preds` has shape `(n_members, n_val)` and `test_preds` has shape `(n_members, n_test)`. Wrong shapes raise `ValueError`. - `k` must be an integer in `[1, n_val]`, otherwise raise `ValueError`. - Distance ties: the lower validation index is the nearer neighbor. - Competence ties: select the lowest member index. `selected` is an integer array. - Labels are arbitrary integers. A 1-D `X` is treated as a single feature. ### What a Strong Answer Covers - Nearest-neighbor search that follows the distance tie rule, plus validation of `k` and of the input shapes. - A vectorized correctness matrix gathered through the neighbor indices to get each member's local accuracy. - Member selection with the lowest-index tie rule, and gathering each test row's prediction from its own selected member. - The time and memory cost of the brute-force distance computation. ### Follow-up Questions - How would DCS-LA scale to large validation sets? - How would you weight neighbors so closer validation points count more? - How does this differ from dynamic ensemble selection like KNORA-E?

Overview: Implement dynamic classifier selection by local accuracy (DCS-LA) in NumPy: score each ensemble member on a test row's nearest validation neighbors, then route the row to its most competent member. Tests vectorized nearest-neighbor search, fancy indexing and deterministic tie-breaking.

General logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement local_competence(X_val, y_val, val_preds, X_test, k), which returns an (n_test, n_members) matrix: the fraction of each test row's k nearest validation rows (Euclidean) that each member classified correctly. Then implement dcs_la_predict(X_val, y_val, val_preds, X_test, test_preds, k), which routes each test row to its most locally competent member and returns (predictions, selected_member_indices).

Example:

X_val = [[0],[1],[2],[10],[11],[12]], y_val = [0,0,0,1,1,1]
val_preds = [[0,0,0,0,0,0], [1,1,1,1,1,1], [1,0,1,1,0,1]]
X_test = [[0.5],[11.5]], test_preds = [[0,5],[7,1],[3,3]], k = 3

local_competence -> [[1.0, 0.0, 1/3], [0.0, 1.0, 2/3]]
dcs_la_predict   -> (preds=[0, 1], selected=[0, 1])

For the test row at 0.5, the three nearest validation rows are at 0, 1 and 2. Member 0 labeled all three correctly, member 1 none of them, and member 2 only the row at 1.

Constraints & Assumptions

  • val_preds has shape (n_members, n_val) and test_preds has shape (n_members, n_test) . Wrong shapes raise ValueError .
  • k must be an integer in [1, n_val] , otherwise raise ValueError .
  • Distance ties: the lower validation index is the nearer neighbor.
  • Competence ties: select the lowest member index. selected is an integer array.
  • Labels are arbitrary integers. A 1-D X is treated as a single feature.

What a Strong Answer Covers Guidance

  • Nearest-neighbor search that follows the distance tie rule, plus validation of k and of the input shapes.
  • A vectorized correctness matrix gathered through the neighbor indices to get each member's local accuracy.
  • Member selection with the lowest-index tie rule, and gathering each test row's prediction from its own selected member.
  • The time and memory cost of the brute-force distance computation.

Follow-up Questions Guidance

  • How would DCS-LA scale to large validation sets?
  • How would you weight neighbors so closer validation points count more?
  • How does this differ from dynamic ensemble selection like KNORA-E?