Skip to content

Interactive way to understand the k-nearest neighbors

License

Notifications You must be signed in to change notification settings

ai-pal/k-NN-playground

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Learn K-NN with JS 😎

This repository focuses on developing a interactive ground to understand K-NN algorithm with the ❤️ of JS.

For this mission we use a popular game knows as Plinko.

How we Progress

Initially we will collect the following data of balls.

  • Drop position
  • Bounciness
  • Ball Size
  • Bucket ball fall into

Then we will use our knn algorithm to predict the bucket of a ball drop from determined position.

K-NN Algorithm using Lodash

function knn(data, point, k) {
    return _.chain(data)
        .map(row => {
            return [distance(_.initial(row), point), _.last(row)]
        })
        .sortBy(row => row[0])
        .slice(0, k)
        .countBy(row => row[1])
        .toPairs()
        .sortBy(row => row[1])
        .last()
        .first()
        .parseInt()
        .value();
}