
KNearest Neighbors (75 Pages)
You can already ask an AI to write you a k-nearest neighbors classifier. The problem is knowing whether its answer means anything — because k-NN always returns a prediction, confidently, whether k was chosen well or badly, whether the features were scaled or one of them is swamping the distance, and whether the “nearest” neighbors are actually near. Understanding exactly what drives that vote, and how to test it honestly, is the foundation under every similarity-based method you’ll ever use, from recommendation engines to the vector search behind modern AI. This is the course that gives you that judgment.
Five parts, one algorithm, no gaps:
Part 1 — Intro. How the simplest possible learner — store the examples, find the closest ones, let them vote — classifies everything from medical records to failing bearings, and why the same simplicity that needs no training at all is exactly what makes it sensitive to scaling, slow on big data, and weak in many dimensions. The intuition before the notation.
Part 2 — By Hand. A complete worked example, step by step: twelve labeled bearings, every distance computed, every tie broken by an explicit rule, every vote counted. You’ll hide each bearing in turn and predict it from the rest, so you see one get misclassified and understand exactly why. Nothing waved away, nothing left “as an exercise.” You prove the algorithm to yourself.
Part 3 — In Excel. The same example built out in a live spreadsheet you can open and poke at, so you watch the votes change as you change k, move the new reading, or relabel a single bearing — and see for yourself how one bad label can change which k looks best.
Part 4 — In Python. A clean, executable, verified reference implementation — the known-good version you can check any other code against, with a built-in brute-force check that confirms every answer a second way, and the distance-weighted variant written safely.
Part 5 — With AI. A tested prompt that directs an AI assistant to carry this same algorithm to your problem, plus the understanding to catch it when it’s wrong.
By the end you won’t just have working code. You’ll have followed the algorithm from first principles through a hand calculation, a spreadsheet, and production Python — so when you point an AI at your own classification problem, you’ll know why the distance and the choice of k define everything, why training accuracy lies and leave-one-out testing doesn’t, when k-NN is genuinely enough, and when you need to reach for something stronger.
Written for practicing engineers and STEM professionals who’d rather understand a method than trust a black box.