npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sfenbld-alike

v1.0.0

Published

A simple kNN library for Machine Learning, comparing JSON objects using Euclidean distances, returning top k similar objects -- supports normalization, weights, key and filter parameters

Downloads

3

Readme

Alike

Build Status

A simple-but-useful kNN library for NodeJS, comparing JSON Objects using Euclidean distances, returning top k closest objects.

Supports Normalized Weighted Euclidean distances. Normalize attributes by Standard Deviation. See here.

Features key and filter attributes to do the data assembly for you, Lisp style!

k-Nearest Neighbour function

subject:  vantage point object - will consider each attribute present in this object as a feature
objects:  array of objects that should all have at least the attributes of subject
options:
    - k: (default = unlimited) specifies how many objects to return
    - standardize: (default = false) if true, will apply standardization across all attributes using stdvs - set this to true if your attributes do not have the same scale
    - weights: (default = {}) a hash describing the weights of each attribute
    - key: (default = none) a key function to map over objects, to be used if the subject attributes are nested within key
        e.g. if subject is {a:0} and objects are [{x: {a: 0}}, {x: {a: 2}}], then provide key: function(o) {return o.x}
    - filter: (default = none) a filter function that returns true for items to be considered
        e.g. to only consider objects with non-negative a: function(o) {return o.a >= 0})
    - debug: (default = false) if true, for every object will return distances of individual attributes as well as the overall distance from the subject under a property called 'debug'
        e.g. if subject is {a:0, b:0} and object is {a:3, b:4}, the returned object will be {a: 3, b: 4, debug: {distance:25, details: {a: 9, b: 16}}}

Example usage

Given John Foo's taste for movies:

John Foo would like to rent a movie tonight that most closely matches his movie tastes. He collected a DB of movies with numerical values ranging from 1 to 10 for each of the 5 attributes listed above (don't ask how).

John Foo loves his pigeons. It is the most important attribute to him, hence carries 50% of the weight. He does not like romance and wants to make sure that he avoids sappy movies. Even though he likes mid-length movies with explosions and semi-funny movies, he doesn't care as much, as long as the movie features peaceful pigeons.

Perfect case for Alike!

Getting started

To install and add it to your package.json

$ npm install alike --save

Now you can load up the module and use it like so:

knn = require('alike');

options = {
  k: 10,
  weights: {
    explosions: 0.1,
    romance: 0.3,
    length: 0.05,
    humour: 0.05,
    pigeons: 0.5
  }
}

movieTaste = {
  explosions: 8,
  romance: 3,
  length: 5,
  humour: 6,
  pigeons: 10
}

knn(movieTaste, movies, options)

Where movies is an array of objects that have at least those 5 attributes. Returns the top 10 movies from the array. Enjoy! :)

Development

Alike is written in CoffeeScript in the coffee/ folder. You may use make coffee to compile and watch for changes. Unit tests are in the coffee/test/ folder. You can run the tests with npm test or if you are developing, you may use make watch-test to watch while you TDD. :)

Benchmarks

Run it with coffee benchmark/ takes about 1m on a Macbook Air.

The benchmarks are designed to reflect realistically sized sets of data. They don't ship with the npm package to keep things light.

Contributors

flockonus mhahmadi

License

Alike is licensed under the terms of the GNU Lesser General Public License, known as the LGPL.