Skip to content
All work

Handwritten digit recognition with weighted KNN

A KNN classifier for handwritten digits, plus a k-value optimizer that collapses repeated distance calculations into a single pass.

Type
School
Year
2020
Built with
Python · KNN · Optimization

Classify a handwritten digit from a 28×28 grid of pixels, each an integer between 0 and 255. The KNN part is textbook. The part I actually cared about was the cost of choosing k.

The naive approach re-runs the whole distance calculation for every candidate value of k. I wrote an optimizer that reduces this to one instance of the distance computation per unclassified object, then evaluates every candidate k against those already-computed neighbours.

Try it

Draw a digit below and a weighted KNN guesses it as you go. It votes with the k training digits closest to yours, each weighted by how close it is, and shows you which ones they were. Slide k to see the same tradeoff the optimizer was built to search: a small k trusts one lookalike, a large k lets more distant digits outvote it.

Draw a digit

Draw with a mouse, finger or pen. With the keyboard: arrow keys move the pen, Space puts it down or lifts it, Delete clears.

Prediction

–

Waiting for ink

Share of the weighted vote

28×28

Votes

  1. 0
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9

Nearest training digits

    The closest matches from the training set appear here, closest first.

    Loading 750 MNIST training digits… Everything runs in your browser: your drawing is cropped, scaled to 20px and centred by mass on a 28×28 grid, the same way MNIST was prepared, then compared pixel by pixel against every training digit.

    Play it full screen →

    The training set here is a balanced 750-digit slice of MNIST (75 of each digit), stored at 4 bits per pixel so the whole thing is about 300 KB. On held-out MNIST test digits it scores around 86% at k = 1 to 5. Your own handwriting will do a little worse: a mouse stroke isn’t a pen on paper, and 750 examples is a small fraction of MNIST’s 60,000 training digits.

    The full write-up, including the accuracy comparison across k values, is in the research report.

    Let's connect

    Have something hard that has to work?

    Contract work, a full-time role, or a problem you want a second opinion on — I read every message and reply to the ones that aren't a template.

    Email me

    Usually replies within a day or two · St. Louis, Missouri

    ESC

    Home

    Main landing page & highlights

    Page

    Work

    All engineering projects & case studies

    Page

    Writing

    Technical notes & engineering articles

    Page

    Games

    Browser games, several against real models

    Page

    About

    Background, career timeline & skills

    Page

    Contact

    Email, inquiries & consultation

    Page

    Copy Email Address

    davidgraymi@gmail.com

    Action

    Toggle Light / Dark Theme

    Switch color theme

    Action

    Resume

    Web page, with a PDF download

    Read

    Visit GitHub

    github.com/davidgraymi

    External

    Visit LinkedIn

    linkedin.com/in/david-gray-mi

    External

    Beat the bot

    Tetris against the value network from my reinforcement-learning project. Same pieces, two minutes, most lines wins.

    Game

    Draw a digit

    Draw 0–9 and watch a weighted KNN classifier find its nearest MNIST neighbours as you go.

    Game

    Is it poisonous?

    A real mushroom from the UCI dataset. Make your call, then watch an ID3 decision tree make its own.

    Game

    Route the robots

    Draw a warehouse, place robots and goals, and race a LaCAM multi-agent path-finding solver to the shortest plan.

    Game

    Daily Wordlet

    One five-letter word a day, the same for everyone. Six guesses.

    Game

    Bot or human?

    14 rounds. One line from a public-domain classic, one written by a language model. Spot the bot.

    Game

    Reaction test

    Wait for the panel to light up amber, then hit it as fast as you can.

    Game

    Lost signal

    The 404 page's dodge game. Packets rain down; steer the receiver out of the way.

    Game

    neural-cost

    A framework-neutral Python library that tells you how fast a neural network should run, not just how fast it does — FLOPs, tensor traffic and a roofline model across PyTorch, JAX and TensorFlow.

    Personal

    Sign Language Interpretation System

    Control any smart device with American Sign Language — hand tracking, a gesture classifier and grammar correction, wired together over a client-server link.

    Capstone

    Patient Care Portal Triage

    An NLP pipeline that routes patient portal messages to the right team and surfaces relevant evidence-based research while they wait for a human.

    Research

    Threat modeling a zero-trust deployment pipeline

    A continuous deployment pipeline for configuration-as-code and infrastructure-as-code into Kubernetes, designed from its threat model outward so the release process does the security review.

    Professional

    Moving three toolchain apps to Kubernetes on Azure

    Three toolchain applications moved to Kubernetes on Azure, deployed one way with Helm and infrastructure-as-code, and secured with an Istio service mesh.

    Professional

    A processor built from the ground up

    Kernel, task scheduler and communication protocol written from nothing, in C and Ada, now in production and distributed globally.

    Professional

    A system library that runs anywhere

    One portable C++ library targeting any RTOS or bare-metal system, packaged with Docker, Bazel and GitLab CI — cutting porting work by 4×.

    Professional

    LaCam for Multi-Agent Path Finding

    A from-scratch LaCam solver for CS440 — agents searching a space of joint configurations, each expanded by a constraint tree that rules out collisions and swaps.

    School

    Bindersnap

    GitHub for everyone who doesn't use GitHub — document management, review lifecycle, team collaboration, search and an in-browser editor.

    Personal

    Wordlet

    A word guessing game built in Flutter and compiled to the web — playable right here, no install, no account.

    Personal

    Reinforcement Learning applied to Tetris

    An agent that learns to play Tetris — a genuinely fun excuse to get hands-on with reward shaping.

    School

    Handwritten digit recognition with weighted KNN

    A KNN classifier for handwritten digits, plus a k-value optimizer that collapses repeated distance calculations into a single pass.

    School

    ID3 mushroom classification

    A from-scratch ID3 decision tree that predicts whether a mushroom is edible or poisonous from its attributes.

    School

    Healthcare length-of-stay estimator

    A data mining project predicting how long a patient will stay, from admission-time features.

    School

    This website

    A static site built with Astro, hosted free on GitHub Pages, designed so writing a post costs one markdown file and nothing else.

    Personal

    An Unexpected Email, REPRO-SIGN, and 11 Citations on Google Scholar

    Years after our college capstone, an out-of-the-blue email from European AI researchers wanting to reproduce our sign language paper sent me to Google Scholar, where we've been cited 11 times.

    Post

    Connecting the Dots: How a Sign Language Capstone Got Me Hooked on AI

    Looking back at my senior capstone project at Missouri State — building a real-time American Sign Language smart home controller with MediaPipe, custom CNNs, and networking — and how it sparked my path in machine learning.

    Post

    GPU vs. CPU: Roofline Benchmarks on Apple Silicon MPS with neural-cost v0.2.0

    The neural-cost v0.2.0 GPU benchmark moves from CPU roofline analysis to Apple Silicon's MPS GPU backend — measuring PyTorch and JAX across five architectures, four batch sizes, and both eager and compiled execution modes.

    Post

    PyTorch vs. JAX vs. TensorFlow: A Roofline Benchmark Across 5 Architectures

    An empirical investigation comparing PyTorch 2.14, JAX 0.11, and TensorFlow across five model architectures and four batch sizes, measuring compiler speedups, kernel overheads, and hardware limits on Apple Silicon.

    Post

    neural-cost: Roofline Analysis Across PyTorch, JAX, and TensorFlow

    A framework-neutral Python library that estimates a neural network's FLOPs and tensor traffic, measures its runtime, and uses a roofline model to tell you exactly where performance is being left on the table.

    Post

    I rebuilt this site so that publishing costs one file

    The old site was a single 41,000-character index.html. Every change was archaeology. Here is what I replaced it with and why the constraint was maintenance, not design.

    Post
    ↑ ↓ to navigate↵ to select
    esc to close

    Terminal