nearestneighbour

0.1.1 • Public • Published

Issues Downloads Auto Test Status license Gitter chat

NearestNeighbour is a method of looking up best matches. Best matches of what? Of your data of course, using your distance function. If you have a list of robotic motions , previous limb location and speed along with current limb speed and the torque that was applied then you can use NearestNeighbour to look up the best guess torque needed to make the current state transition into a target state. If you have a list of books and their ranking in several dimensions such as romance level, word count, and popularity. You can use NearestNeighbour to find the best matches for a person based on their preferences of romance level, word count, etc.

You can improve the accuracy of NearestNeighbour by providing it with more data. The data can be anything you want so long as it works with your distance function. Don't have a distance function? No problem, I made a cartisian distance checker for you that works with objects that have one or more key:number entries. If you don't specify a distance function then the cartisian distance checker is used.

Amazon (used to) suggest related books and items to customers using NearestNeighbour (or something very similar to it) and built a huge online empire on the upsales from doing so. Play with NearestNeighbour a bit and you'll quickly see how you could get really accurate upsales lists based on someones previous shopping history and what other people shop for.

Section Links : Construction , Execution , Examples , FAQ , Related , and References

Construction

Create a NearestNeighbour

Create a default NearestNeighbour calculator from the constructor like so:

var nearestNeighbour = require('nearestneighbour')()

Set the configuration options like so:

var config = {
    objects : [ object1 , object2 , object3 ],
    number : 2  // the number of results
}
in two steps
var nearestNeighbour = require('nearestneighbour')()
nearestNeighbour.config(config)
 
# or as a one liner
var nearestNeighbour = require('nearestneighbour')(config)

Update Objects

You can change or add to the list of objects like so

var nearestNeighbour = require('nearestneighbour')(config)
nearestNeighbour.setObjects( [ object1 , object2 , object3 ] )
nearestNeighbour.add( object4 )

Execution

Execution is a one liner and returns a list of nearest neighbours

var resultList = nearestNeighbour.nearest( searchObject )

Configuration

The full configuration is as follows

var config = {
    objects  : [ /* list of objects */ ],
    number : theNumberOfResultsYouWant,
    distanceFunction : yourDistanceFunction
}

You can set the configuration wholely or just change one aspect using the .config function like so:

# change the number of results to 5
nearestNeighbour.config({number:5})

Example

This is a very simple example that finds the object with the closest match.

var neighbour = require('nearestneighbour')({ 
    objects : [ { a : -10 } , { a : 3 } ],
    number : 1
})
console.log( neighbour.nearest({ a : 1 })[0] )
# returns { a : 3 }
neighbour.add( { a : 2 } )
console.log( neighbour.nearest({ a : 1 })[0] )
# returns { a : 2 }

FAQ

Each object in objects can be any object you want. If you are using the default distance function the object must have at a minimum the same keys as the keys in the searchObject. Additionally, the values of the keys in the searchObject and the objects must be Numbers.

Related AI Projects

This is part of a set of related projects.

References

Dependents (0)

Package Sidebar

Install

npm i nearestneighbour

Weekly Downloads

14

Version

0.1.1

License

Unlicense

Unpacked Size

12.3 kB

Total Files

7

Last publish

Collaborators

  • panchishin