Fork me on GitHub

Project Notes


A demonstration of the k-nearest neighbors algorithm implemented in javascript in a browser.


This is my working of an example from the Machine Learning with Javascript course.

Plinko is a demonstration of the k-nearest neighbors algorithm implemented in javascript in a browser.


Plinko is a simple game (like a pachinko machine). The problem to be solved: Given where ball is dropped from, can we predict which bucket it will land in?

The dependent variable (label) is the bucket. This is a classification problem as label values belong to discrete set.

The independent variables (features) that may be relevant include:

  • drop position
  • ball bounciness
  • ball size

How K-Nearest Neighbor Works

  • given a prediction point (set of features to predict label for)
  • collect sample data
  • for each observation, calculate the feature distance from prediction point
  • sort and take top ‘k’ records
  • select the most common label
  • that is the prediction

Multi-Dimensional KNN

When modeling with multiple features, consider:

  • normalize or standardise the features

Feature Normalization

  • normalize - 0..1
  • standardize - normal standard deviation around 0

Normalising with MinMax

normalized = (value - min) / (max - min)

Gauging Accuracy

Feature Selection

  • change in drop position - has predictable impact on bucket
  • change in bounciness - has impact on bucket, but not predictably

Running the Demonstration

See plinko.html or locally open plinko.html

Drop enough balls to make a sample set

  • optionally set bounciness and size range


Click the analyze button. Results inserted at the bottom of the page:


Credits and References

About LCK#41 javascriptknn
Project Source on GitHub Return to the Project Catalog

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.