VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 15: The ML coding round

Lesson 15.1 of 12 in this module Unrolled Pro

k-nearest neighbours classifier

Classify each test point by a majority vote of its k closest training points, in NumPy. From Python loops over every (test, train) pair with a full sort per test point (O(m · n · d) interpreted operations), to one matrix product that computes all squared distances at once via ‖a − b‖² = ‖a‖² + ‖b‖² − 2a·b, argpartition instead of a sort (O(n) per row), and chunking so the m × n distance matrix fits in memory. Then feature scaling, distance-weighted votes, regression, and approximate search when n is in the millions.

about 10 min 3 Python examples 2 quick checks

Problem. Given training points X_train (n × d) with integer labels y_train, test points X_test (m × d) and k, predict each test point's label as the most common label among its k nearest training points (Euclidean distance). Break vote ties by the smaller label.

text
X_train = [[0, 0], [0, 1], [1, 0], [5, 5], [5, 6], [6, 5]],  y_train = [0, 0, 0, 1, 1, 1]
X_test  = [[0.2, 0.1], [5.5, 5.2]],  k = 3       →   [0, 1]

In an ML coding round, the algorithm is never the hard part. What's being tested is whether you can turn "for each pair, compute a distance" into array operations that run in compiled code, avoid an O(n log n) sort when you only need k items, and keep memory bounded. Those three moves recur in every lesson of this module.

ML context. kNN is a baseline classifier, the retrieval step behind "find similar items" (lesson 15.2), and the core of embedding-based classifiers: embed with a pretrained model, then vote among nearest labelled examples. The same distance-matrix trick computes RBF kernels, k-means assignments (lesson 15.4) and contrastive-loss similarity matrices.


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "k-nearest neighbours classifier"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee